码迷,mamicode.com
首页 > 其他好文 > 详细

UVALive 4256 Salesmen

时间:2016-12-03 01:55:19      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:air   c++   second   bsp   post   ons   oal   live   include   

题解:

一道很基础的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 4256 Salesmen

标签:air   c++   second   bsp   post   ons   oal   live   include   

原文地址:http://www.cnblogs.com/byene/p/6127564.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!