UVALive 4256Salesmen

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVALive 4256Salesmen相关的知识,希望对你有一定的参考价值。

题解:

一道很基础的DP题目。想清楚状态的表示就可以直接写了

代码:

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define se second
#define fs first
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pii pair<int,int>
const int INF = 1000000000;
const int maxn = 1050;
const int M = 4000;

int a[ 210 ], dp[ 210 ][ 105 ];
set< int > s[ 105 ];

int main()
{
    int T, n, m, num;
    scanf( "%d", &T );
    while( T -- )
    {
        scanf( "%d%d", &n, &m );
        for( int i = 1; i <=n; i ++ ) s[ i ].clear();
        for( int i = 1; i <=m; i ++ )
        {
            int x, y;
            scanf( "%d%d", &x, &y );
            s[ x ].insert( y );
            s[ y ].insert( x );
        }
        scanf( "%d", &num );
        for( int i = 1; i <= num; i ++ ) scanf( "%d", &a[ i ] );
        for( int i = 1; i <= n; i ++ ) dp[ 1 ][ i ] = ( a[ 1 ] == i ? 0 : 1 );
        int goal = INF;
        for( int i = 2; i <= num; i ++ )
        for( int j = 1; j <= n; j ++ )
        {
            dp[ i ][ j ] = INF;
            for( int k = 1; k <= n; k ++ )
            {
                if( k == j || s[ j ].count( k ) ) dp[ i ][ j ] = min( dp[ i ][ j ], dp[ i - 1 ][ k ] );
                else dp[ i ][ j ] = min( dp[ i ][ j ], dp[ i - 1 ][ k ] + 1 );
            }
            if( j != a[ i ] ) dp[ i ][ j ] ++;
            if( i == num ) goal = min( goal, dp[ i ][ j ] );
        }
        printf( "%d\n", goal );
    }
    return 0;
}

 

 

 
 

以上是关于UVALive 4256Salesmen的主要内容,如果未能解决你的问题,请参考以下文章

HDU 4256

HDU 4256 The Famous Clock

Jzoj 4256NOIP2015模拟10.20二分贪心平均数

[贪心][前缀和] Jzoj P4256 平均数

洛谷 P4256 公主の#19准备月考

UVALive 3971 Assemble(模拟 + 二分)