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

LA 4256

时间:2014-04-28 01:25:55      阅读:582      评论:0      收藏:0      [点我收藏+]

标签:com   class   blog   http   div   code   img   style   size   string   tar   

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,

 

bubuko.com,布布扣<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) = bubuko.com,布布扣d (aibi)

<tex2html_verbatim_mark>

where

 

d (ab) = bubuko.com,布布扣bubuko.com,布布扣

<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 distancedist(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>(3bubuko.com,布布扣n1bubuko.com,布布扣100, 2bubuko.com,布布扣n2bubuko.com,布布扣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>(2bubuko.com,布布扣nbubuko.com,布布扣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

设dp[i][j]是当前序列第i个数选择j的最小dis所以 dp[i][j] = min(dp[i][j],dp[i - 1][k] + (j != a[i])) k j 连通
bubuko.com,布布扣
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 
 6 using namespace std;
 7 
 8 const int MAX_N = 205;
 9 const int edge = 5000;
10 int N,M;
11 int a[MAX_N];
12 int dp1[MAX_N],dp2[MAX_N];
13 bool f[MAX_N][MAX_N];
14 int n;
15 
16 void solve() {
17         int *now = dp2,*last = dp1;
18         for(int i = 1; i <= n; ++i) {
19                 fill(now + 1,now + N + 1,n + 1);
20                 for(int j = 1; j <= N; ++j) {
21                         int v = a[i] != j;
22                         for(int k = 1; k <= N; ++k) {
23                                 if(!f[j][k]) continue;
24                                 if(last[k] != n + 1)
25                                 now[j] = min(now[j],last[k] + v);
26                         }
27 
28                 }
29                 swap(now,last);
30         }
31 
32         int ans = n + 1;
33         //for(int i = 1; i  <= N; ++i) printf("%d",last[i]);
34         //printf("\n");
35         for(int i = 1; i <= N; ++i) ans = min(ans,last[i]);
36         printf("%d\n",ans);
37 }
38 
39 int main()
40 {
41    // freopen("sw.in","r",stdin);
42     int t;
43     scanf("%d",&t);
44     while(t--) {
45             scanf("%d%d",&N,&M);
46 
47             memset(dp1,0,sizeof(dp1));
48             memset(dp2,0,sizeof(dp2));
49             memset(f,0,sizeof(f));
50             for(int i = 1; i <= N; ++i) f[i][i] = 1;
51 
52             for(int i = 0; i <  M; ++i) {
53                     int u,v;
54                     scanf("%d%d",&u,&v);
55                     f[u][v] = f[v][u] = 1;
56 
57             }
58             scanf("%d",&n);
59             for(int i = 1; i <= n; ++i) {
60                     scanf("%d",&a[i]);
61             }
62 
63             solve();
64 
65     }
66 
67     return 0;
68 }
View Code

 


LA 4256,布布扣,bubuko.com

LA 4256

标签:com   class   blog   http   div   code   img   style   size   string   tar   

原文地址:http://www.cnblogs.com/hyxsolitude/p/3695172.html

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