UVA 1424 DP

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.