UVA 1424 二 Salesmen

Salesmen

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

Submit Status Practice UVA 1424

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>(3n1100, 2n24, 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>(2n200) <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 }

时间: 2024-10-13 21:30:36

UVA 1424 二 Salesmen的相关文章

递推DP UVA 1424 Salesmen

题目传送门 1 /* 2 题意:给定包含n个点的无向图和一个长度为L的序列,修改尽量少的点使得相邻的数字相同或连通 3 DP:状态转移方程:dp[i][j] = min (dp[i][j], dp[i-1][k] + (j != a[i])); 4 dp[i][j]表示前i个数字以j结尾的最小花费.我自己写了很长时间,很复杂,状态转移的不好. 5 应该能知道前一个状态的所有情况,每一维数组记录的就是一个状态 6 */ 7 /************************************

uva 1424 dp

UVA 1424 - Salesmen 给出一副图,并且给出nhn走过的路径记入,路径可能是错的,问最少修改几个地方可以使得路径是正确的. dp[i][j] 表示修改第i个位置为j点的前i个位置的最小修改次数. dp[i][j] = min(dp[i-1][k] + (j == a[i])); {w[k][j] == true 即存在路径k~j} 然后再最后一个点找一个最小值. #include <cstdio> #include <cstring> #include <io

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

UVA 590 二十一 Always on the run

Always on the run Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 590 Appoint description:  System Crawler  (2015-08-26) Description Screeching tires. Searching lights. Wailing sirens. Police cars eve

UVA 442 二十 Matrix Chain Multiplication

Matrix Chain Multiplication Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 442 Appoint description:  System Crawler  (2015-08-25) Description Suppose you have to evaluate an expression like A*B*C*D*E

Problem W UVA 662 二十三 Fast Food

Fast Food Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 662 Appoint description:  System Crawler  (2015-08-27) Description The fastfood chain McBurger owns several restaurants along a highway. Recen

UVa 10004 二染色

题意:给定一个无向图,是强连通的,而且无自回路.对顶点进行染色,相邻的顶点需要用不同的颜色,但总共只有两种颜色,是否可行. 思路:二部图的判定.其实通过题意思考,也可以发现,如果没有回路是可以的,如果有回路,而回路的顶点个数是偶数个也是可以的,是奇数个则不行.而这正是二部图的充要条件:无向图的所有回路长度都为偶数.  但这里如何判断回路长度不好实现.  这里的思路是,对图进行遍历,dfs或bfs都可以,在遍历的过程中进行染色,当发现冲突时即不可行. Code: #include<stdio.h>

AOAPC-I: 算法竞赛入门经典 UVa 习题集分类

数据结构基础 UVa 10004 二染色:二部图的判定.(bfs或dfs遍历的过程进行染色,看是否有冲突) UVa 10129 单词:有向图的欧拉道路. UVa 10054 项链:无向图的欧拉回路,首尾相接输出路径. UVa 10596 清晨漫步:无向图的欧拉回路. (对于欧拉道路或回路,在判断连通性等时注意先 if 下要访问的顶点是否出现.)

dp题目列表

10271 - Chopsticks 10739 - String to Palindrome 10453 - Make Palindrome 10401 - Injured Queen Problem 825 - Walking on the Safe Side 10617 - Again Palindrome 10201 - Adventures in Moving - Part IV 11258 - String Partition 10564 - Paths through the Ho