UVA 1424-salesmen
Give a picture, and give the path that NHN traversed, the path may be wrong, ask at least modify several places can make the path is correct.
DP[I][J] Indicates the minimum number of modifications to the first I position where the I position is a J point.
Dp[i][j] = min (Dp[i-1][k] + (j = = A[i])); {W[k][j] = = True exists path K~j}
Then find a minimum value at the last point.
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace STD;Const intINF =999999999;intN, M, K;BOOLc[ the][ the];intA, B;ints[205];intdp[205][ the];intMain () {intT for(scanf("%d", &t); T>0; t--) {scanf("%d%d", &n, &m);memsetCfalse,sizeof(c)); for(intI=0; i<m; i++) {scanf("%d%d", &a, &b); C[A][B] = C[b][a] =true; } for(intI=1; i<=n; i++) {C[i][i] =true; }scanf("%d", &k); for(intI=1; i<=k; ++i) {scanf("%d", &s[i]); } for(intI=1; i<=n; i++) {dp[1][i] =1; } dp[1][s[1]] =0; for(intI=2; i<=k; i++) { for(intj=1; j<=n; J + +) {if(j== S[i])Continue; DP[I][J] = INF; for(intv=1; v<=n; v++) {if(C[v][j] = =true) {Dp[i][j] = min (dp[i][j], dp[i-1][V] +1); }}} Dp[i][s[i]] = INF; for(intv=1; v<=n; v++) {if(C[v][s[i]]) dp[i][s[i] [min (Dp[i][s[i]], dp[i-1][V]); } }intans = INF; for(intI=1; i<=n; i++) {ans = min (ans, dp[k][i]); }printf("%d\n", ans); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVA 1424 DP