UVaLive LA 4256 | UVa 1424 - Salesmen(简单DP)

来源:互联网 发布:洪玄公捏脸数据 编辑:程序博客网 时间:2024/04/30 16:19

Salesmen

Time Limit:3000MS Memory Limit:Unknown 64bit IO Format:%lld & %llu

[Submit]  [Go Back]  [Status]  

Description

Download as PDF

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 each salesman's working path on a map of his working area and uses this path information for the planning of the next work of the salesman. The map of a salesman's working area is represented as a connected and undirected graph, where vertices represent the possible locations of the salesman an edges correspond to the possible movements between locations. Therefore the salesman's working path can be denoted by a sequence of vertices in the graph. Since each salesman reports his position regularly an he can stay at some place for a very long time, the same vertices of the graph can appear consecutively in his working path. Let a salesman's working path becorrect if two consecutive vertices correspond either the same vertex or two adjacent vertices in the graph.

For example on the following graph representing the working area of a salesman,

\epsfbox{p4256.eps}

a reported working path [1 2 2 6 5 5 5 7 4] is a correct path. But a reported working path [1 2 2 7 5 5 5 7 4] is not a correct path since there is no edge in the graph between vertices 2 a 7. If we assume that the salesman reports his location every time when he has to report his location (but possibly incorrectly), then the correct path could be [1 2 2 4 5 5 5 7 4], [1 24 7 5 5 5 7 4], or [1 2 26 5 5 5 7 4].

The length of a working path is the number of vertices in the path. We define thedistance between two pathsA =a1a2...an andB = b1b2...bn of the same lengthn as

dist(A, B) = $\displaystyle \sum^{{n}}_{{i=1}}$d (ai,bi)

where

d (a, b) = $\displaystyle \left\{\vphantom{ \begin{array}{cc} 0 & \mbox{if } a=b \\  1 & \mbox{otherwise} \end{array} }\right.$$\displaystyle \begin{array}{cc} 0 & \mbox{if } a=b \\  1 & \mbox{otherwise} \end{array}$

Given a graph representing the working area of a salesman and a working path (possible not a correct path),A, of a salesman, write a program to compute a correct working path,B, of the same length where the distance dist(A, B) is minimized.

Input

The program is to read the input from standard input. The input consists of T test cases. The number of test cases (T) is given in the first line of the input. The first line of each test case contains two integersn1,n2(3$ \le$n1$ \le$100, 2$ \le$n2$ \le$4, 950) where n1 is the number of vertices of the graph representing the working map of a salesman andn2 is the number of edges in the graph. The input graph is a connected graph. Each vertex of the graph is numbered from 1 ton1. In the followingn2 lines, each line contains a pair of vertices which represent an edge of the graph. The last line of each test case contains information on a working path of the salesman. The first integern(2$ \le$n$ \le$200) in the line is the length of the path and the following n integers represent the sequence of vertices in the working path.

Output

Your program is to write to standard output. Print one line for each test case. The line should contain the minimum distance of the input path to a correct path of the same length.

Sample Input

2 7 9 1 2 2 3 2 4 2 6 3 4 4 5 5 6 7 4 7 5 9 1 2 2 7 5 5 5 7 4 7 9 1 2 2 3 2 4 2 6 3 4 4 5 5 6 7 4 7 5 9 1 2 2 6 5 5 5 7 4

Sample Output

1 0


题意:

给定一个包含n(n<=100)个点的无向连通图和一个长为L的序列A(L<=200)任务是修改尽量少的数,使得序列中的任意两个相邻数相同或者对应于图中的两个相邻顶点


思路:

简单DP

dp[i][j] 表示使前i个数满足条件并且第i个数为j最少需要的修改次数




#include <cstdio>#include <iostream>#include <vector>#include <algorithm>#include <cstring>#include <string>#include <map>#include <cmath>#include <queue>#include <set>using namespace std;//#define WIN#ifdef WINtypedef __int64 LL;#define iform "%I64d"#define oform "%I64d\n"#define oform1 "%I64d"#elsetypedef long long LL;#define iform "%lld"#define oform "%lld\n"#define oform1 "%lld"#endif#define S64I(a) scanf(iform, &(a))#define P64I(a) printf(oform, (a))#define P64I1(a) printf(oform1, (a))#define REP(i, n) for(int (i)=0; (i)<n; (i)++)#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)const int INF = 0x3f3f3f3f;const double eps = 10e-9;const double PI = (4.0*atan(1.0));const int maxn = 100 + 20;const int maxl = 200 + 20;int G[maxn][maxn];int A[maxl];int dp[maxl][maxn];int main() {    int T;    scanf("%d", &T);    while(T--) {        int n1, n2;        scanf("%d%d", &n1, &n2);        memset(G, 0, sizeof(G));        for(int i=0; i<n2; i++) {            int u, v;            scanf("%d%d", &u, &v);            G[u][v] = G[v][u] = 1;        }        int L, t;        scanf("%d%d", &L, &t);        for(int i=0; i<=n1; i++) dp[0][i] = 1;        dp[0][t] = 0;        for(int i=1; i<L; i++) {            scanf("%d", &t);            for(int j=1; j<=n1; j++) {                dp[i][j] = INF;                int cast = j == t ? 0 : 1;                for(int k=1; k<=n1; k++) if(G[j][k] || j==k) {                    int td = dp[i-1][k] + cast;                    dp[i][j] = min(dp[i][j], td);                }            }        }        int ans = INF;        for(int i=1; i<=n1; i++) ans = min(ans, dp[L-1][i]);        printf("%d\n", ans);    }    return 0;}


0 0