UVALIVE 4256 Salesmen

Salesmen

Time Limit: 3000ms
Memory Limit: 131072KB
This problem will be judged on  UVALive. Original ID: 4256
64-bit integer IO format: %lld      Java class name: Main
 

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,

 

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

 

 

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

 

where

 

d ( ab) =  $\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 <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$ \le$n1$ \le$100, 2$ \le$n2$ \le$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$ \le$n$ \le$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

Source

 
解题:动态规划,dp[i][j]表示起始位置到当前位置j需要改变的最小值,dp[i][j] = min(dp[i][j],dp[i-1][k]+cost)
 
情况分析:如果目的点j正式当前输入的,那么cost为0,因为这个字符跟原来的一样,如果当前的态,与输入的不一样,那么,需要改变一次。
 
好吧,说不清了,其实就是将原来的路径,修改尽可能小的次数,使得路径合法。
 
 
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 const int maxn = 110;
18 bool g[maxn][maxn];
19 int dp[5000][maxn];
20 int main() {
21     int t,n,m,p,u,v;
22     scanf("%d",&t);
23     while(t--) {
24         memset(g,false,sizeof(g));
25         scanf("%d %d",&n,&m);
26         for(int i = 0; i < m; ++i) {
27             scanf("%d %d",&u,&v);
28             g[u][v] = g[v][u] = true;
29         }
30         for(int i = 1; i <= n; ++i) dp[0][i] = 1;
31         scanf("%d %d",&p,&u);
32         dp[0][u] = 0;
33         for(int i = 1; i < p; ++i) {
34             scanf("%d",&u);
35             for(int j = 1; j <= n; ++j) {
36                 dp[i][j] = INF;
37                 int cost = u == j?0:1;
38                 for(int k = 1; k <= n; ++k) {
39                     if(g[k][j] || k == j)
40                         dp[i][j] = min(dp[i][j],dp[i-1][k]+cost);
41                 }
42             }
43 
44         }
45         int ans = INF;
46         for(int i = 1; i <= n; ++i)
47             ans = min(ans,dp[p-1][i]);
48         cout<<ans<<endl;
49     }
50     return 0;
51 }
View Code

 

转载于:https://www.cnblogs.com/crackpotisback/p/4037066.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值