LA 4256

本文介绍了一种针对旅行推销员报告的工作路径可能存在的错误进行校正的算法。通过定义路径正确性和距离,该算法旨在寻找一条正确的路径,使得其与原始路径之间的距离最小。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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}<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) = $\displaystyle \sum^{​{n}}_{​{i=1}}$d (aibi)

<tex2html_verbatim_mark>

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}$

<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>(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

设dp[i][j]是当前序列第i个数选择j的最小dis所以 dp[i][j] = min(dp[i][j],dp[i - 1][k] + (j != a[i])) k j 连通
 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

 


转载于:https://www.cnblogs.com/hyxsolitude/p/3695172.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值