UVALive 4256 Salesmen

题解:

一道很基础的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;
}

 

时间: 2025-01-02 01:13:59

UVALive 4256 Salesmen的相关文章

LA 4256 Salesmen 线性dp

// LA 4256 Salesmen 线性dp // // 像LCS和LIS问题类似,因为每次修改一个值,都是根据 // 前一个值决定的,那么最后一个结尾的数字肯定要作为 // 状态,而长度作为状态是一目了然的 // // d[i][j]表示长度为i,最后以j结尾的数组修改的最小次数 // // 则状态转移方程为 // // d[i][j] = min(d[i][j],d[i-1][k]+(j,k是否相同或者相邻?0:1)); // // 个人感觉还是比较明显的,最后的答案就是min(d[L]

uvalive 4256(dp)

题意:有从1到n的数字组成一个无向连通图,给出了连通情况,然后给出一个数字序列,问这个序列要求相邻的点要么相等要么在图中是直接连通的,问最少修改序列中的几个点可以让序列满足要求. 题解:f[i][j]表示前i个数组组成的序列以数字j结尾的最少修改点,那么f[i][j] = min{f[i][j],f[i - 1][k] + (d[i] != j)},此时j==k或g[j][k] == 1.最后f[len][k]所有数字过一遍选出最大值就可以了. #include <stdio.h> #incl

训练指南DP阶段训练1

最近又忙又颓.............时间抓不紧....下学期开始就要准备考研了.......就2个月左右可以做自己喜欢的事了....争取把紫书和白书没做的,做过的..来一次完整的总结 训练指南上面的5个例题+后面15个习题是第一阶段 vjudge训练地址 http://vjudge.net/contest/139533#overview -------------------------------------------------------------------------------

LA 4256 DP Salesmen

d(i, j)表示使前i个数满足要求,而且第i个数值为j的最小改动次数. d(i, j) = min{ d(i-1, k) | k == j | G[j][k] } 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 const int maxn = 200 + 10; 7 8 int n, m, k; 9 int a[maxn]; 10 i

LA 4256

Traveling salesmen of nhn. (the prestigious Korean internet company) report their current location to the company on a regular basis. They also have to report their new location to the company if they are moving to another location. The company keep

UVALive 4848 Tour Belt

F - Tour Belt Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVALive 4848 Description Korea has many tourist attractions. One of them is an archipelago (Dadohae in Korean), a cluster of small islands sca

UVALive 6467 Strahler Order 拓扑排序

这题是今天下午BNU SUMMER TRAINING的C题 是队友给的解题思路,用拓扑排序然后就可以了 最后是3A 其中两次RE竟然是因为: scanf("%d",mm); ORZ 以后能用CIN还是CIN吧 QAQ 贴代码了: 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <math.h> 5 #include <iostre

HDU 4256 The Famous Clock

The Famous Clock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1399    Accepted Submission(s): 940 Problem Description Mr. B, Mr. G and Mr. M are now in Warsaw, Poland, for the 2012's ACM-ICPC

UVALive 7077 Little Zu Chongzhi&#39;s Triangles (有序序列和三角形的关系)

这个题……我上来就给读错了,我以为最后是一个三角形,一条边可以由多个小棒组成,所以想到了状态压缩各种各样的东西,最后成功了……结果发现样例过不了,三条黑线就在我的脑袋上挂着,改正了以后我发现N非常小,想到了回溯每个棍的分组,最多分5组,结果发现超时了……最大是5^12 =  244,140,625,厉害呢…… 后来想贪心,首先想暴力出所有可能的组合,结果发现替换问题是一个难题……最后T T ,我就断片了.. 等看了别人的办法以后,我才发现我忽视了三角形的特性,和把数据排序以后的特点. 如果数据从