码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 1424 二 Salesmen

时间:2015-08-30 21:12:31      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:

Salesmen

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

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,

 

技术分享<tex2html_verbatim_mark>

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 pathsA = a1a2...an <tex2html_verbatim_mark>and B = b1b2...bn <tex2html_verbatim_mark>of the same length n <tex2html_verbatim_mark>as

 

 

dist(AB) = 技术分享d (aibi)

<tex2html_verbatim_mark>

where

 

d (ab) = 技术分享技术分享

<tex2html_verbatim_mark>

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

 

Input 

The program is to read the input from standard input. The input consists of T <tex2html_verbatim_mark>test cases. The number of test cases (T) <tex2html_verbatim_mark>is given in the first line of the input. The first line of each test case contains two integers n1<tex2html_verbatim_mark>, n2 <tex2html_verbatim_mark>(3技术分享n1技术分享100, 2技术分享n2技术分享4, 950) <tex2html_verbatim_mark>where n1 <tex2html_verbatim_mark>is the number of vertices of the graph representing the working map of a salesman and n2 <tex2html_verbatim_mark>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 <tex2html_verbatim_mark>. In the following n2 <tex2html_verbatim_mark>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 <tex2html_verbatim_mark>(2技术分享n技术分享200) <tex2html_verbatim_mark>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
技术分享
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<math.h>
 4 #include<vector>
 5 #include<algorithm>
 6 using namespace std;
 7 
 8 vector<int>edg[1005];
 9 int dp[1005][1005],path[2005];
10 
11 
12 int main()
13 {
14     int T;
15     int i,j,k;
16     int n,m,l;
17     int x,y;
18     scanf("%d",&T);
19     while(T--)
20     {
21         scanf("%d %d",&n,&m);
22 
23         for(j=1;j<=n;j++)
24         {
25             dp[1][j]=1;
26         }
27         for(i=1;i<=n;i++)
28         {
29             edg[i].clear();
30             edg[i].push_back(i);
31         }
32 
33         for(i=1;i<=m;i++)
34         {
35             scanf("%d %d",&x,&y);
36             edg[x].push_back(y);
37             edg[y].push_back(x);
38         }
39         scanf("%d",&l);
40         for(i=1;i<=l;i++)
41         {
42             scanf("%d",&path[i]);
43         }
44 
45         dp[1][path[1]]=0;
46         for(i=2;i<=l;i++)
47         {
48             for(j=1;j<=n;j++)
49             {
50                 int v=edg[j][0];
51                 dp[i][j]=dp[i-1][v]+1;
52                 for(k=1;k<edg[j].size();k++)
53                 {
54                     v=edg[j][k];
55                     dp[i][j]=min(dp[i][j],dp[i-1][v]+1);
56                 }
57             }
58             dp[i][path[i]]--;
59         }
60         int ans=999;
61         for(i=1;i<=n;i++)
62         {
63             //printf("%d\n",dp[2][i]);
64             if(dp[l][i]<ans)
65                 ans=dp[l][i];
66         }
67         printf("%d\n",ans);
68     }
69     return 0;
70 }
View Code

 

UVA 1424 二 Salesmen

标签:

原文地址:http://www.cnblogs.com/cyd308/p/4771571.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!