首页 > 代码库 > Salesmen - UVa 1424 dp(送给大家)
Salesmen - UVa 1424 dp(送给大家)
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 be correct 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,
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 2 4 7 5 5 5 7 4], or [1 2 2 6 5 5 5 7 4].
The length of a working path is the number of vertices in the path. We define the distance between two paths A= a1a2...an and B = b1b2...bn of the same length n as
where
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 distancedist(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 integers n1 ,n2 (3n1100, 2n24, 950) where n1 is the number of vertices of the graph representing the working map of a salesman and n2 is the number of edges in the graph. The input graph is a connected graph. Each vertex of the graph is numbered from 1 to n1 . In the following n2 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 integer n (2n200) 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
题意:找一个可以在图上前行的序列,使得其与给定序列相差的数字数量最小,输出这个最小的值。
思路:dp[i][j]表示已经匹配好前i个序列并且最后停在j点时的最小权值。
AC代码如下:
- #include<cstdio>
- #include<cstring>
- #include<vector>
- #include<algorithm>
- using namespace std;
- vector<int> vc[110];
- int val[210],len[110],dp[210][110],INF=1e9;
- int main()
- {
- int t,T,n,m,i,j,k,u,v,length,f,ans;
- scanf("%d",&T);
- for(t=1;t<=T;t++)
- {
- scanf("%d%d",&n,&m);
- for(i=1;i<=n;i++)
- vc[i].clear();
- for(i=1;i<=m;i++)
- {
- scanf("%d%d",&u,&v);
- vc[u].push_back(v);
- vc[v].push_back(u);
- }
- for(i=1;i<=n;i++)
- {
- vc[i].push_back(i);
- len[i]=vc[i].size();
- }
- scanf("%d",&length);
- for(i=1;i<=length;i++)
- scanf("%d",&val[i]);
- for(i=1;i<=n;i++)
- if(i==val[1])
- dp[1][i]=0;
- else
- dp[1][i]=1;
- for(i=2;i<=length;i++)
- {
- for(u=1;u<=n;u++)
- {
- dp[i][u]=INF;
- if(u==val[i])
- f=0;
- else
- f=1;
- for(k=0;k<len[u];k++)
- {
- v=vc[u][k];
- dp[i][u]=min(dp[i][u],dp[i-1][v]+f);
- }
- }
- }
- ans=INF;
- for(i=1;i<=n;i++)
- ans=min(ans,dp[length][i]);
- printf("%d\n",ans);
- }
- }
Salesmen - UVa 1424 dp(送给大家)