PAT_A1150#Travelling Salesman Problem
Source:
Description:
The "travelling salesman problem" asks the following question: "Given a list of cities and the distances between each pair of cities, what is the shortest possible route that visits each city and returns to the origin city?" It is an NP-hard problem in combinatorial optimization, important in operations research and theoretical computer science. (Quoted from "https://en.wikipedia.org/wiki/Travelling_salesman_problem".)
In this problem, you are supposed to find, from a given list of cycles, the one that is the closest to the solution of a travelling salesman problem.
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 positive integers N (2), the number of cities, and M, the number of edges in an undirected graph. Then Mlines follow, each describes an edge in the format
City1 City2 Dist
, where the cities are numbered from 1 to N and the distanceDist
is positive and is no more than 100. The next line gives a positive integer K which is the number of paths, followed by K lines of paths, each in the format:n C1 C2 ... Cn
where n is the number of cities in the list, and Ci's are the cities on a path.
Output Specification:
For each path, print in a line
Path X: TotalDist (Description)
whereX
is the index (starting from 1) of that path,TotalDist
its total distance (if this distance does not exist, outputNA
instead), andDescription
is one of the following:
TS simple cycle
if it is a simple cycle that visits every city;TS cycle
if it is a cycle that visits every city, but not a simple cycle;Not a TS cycle
if it is NOT a cycle that visits every city.Finally print in a line
Shortest Dist(X) = TotalDist
whereX
is the index of the cycle that is the closest to the solution of a travelling salesman problem, andTotalDist
is its total distance. It is guaranteed that such a solution is unique.
Sample Input:
6 10 6 2 1 3 4 1 1 5 1 2 5 1 3 1 8 4 1 6 1 6 1 6 3 1 1 2 1 4 5 1 7 7 5 1 4 3 6 2 5 7 6 1 3 4 5 2 6 6 5 1 4 3 6 2 9 6 2 1 6 3 4 5 2 6 4 1 2 5 1 7 6 1 2 5 4 3 1 7 6 3 2 5 4 1 6
Sample Output:
Path 1: 11 (TS simple cycle) Path 2: 13 (TS simple cycle) Path 3: 10 (Not a TS cycle) Path 4: 8 (TS cycle) Path 5: 3 (Not a TS cycle) Path 6: 13 (Not a TS cycle) Path 7: NA (Not a TS cycle) Shortest Dist(4) = 8
Keys:
Attention:
- 注意检查是否遍历了所有结点
Code:
1 /* 2 Data: 2019-08-04 17:16:14 3 Problem: PAT_A1150#Travelling Salesman Problem 4 AC: 20:24 5 6 题目大意: 7 给出城市结点列表,及其路径,问遍历所有结点并返回初始结点的最短路径 8 现在给出一系列路径,找出能够遍历所有结点的最短回路 9 输入: 10 第一行给出,结点数2<N<=200,边数M 11 接下来M行, City1 City2 Distance, 1<=City<=N, 0<Dis<=100; 12 接下来一行,给出查询数K 13 接下来K行,首先给出城市数目N,接着依次给出N个城市 14 输出: 15 Path 1~K: 总距离/NA(不可达) 16 描述: 17 简单回路,TS simple cycle 18 非简单回路,TS cycle 19 非回路,Not a TS cycle(未回到起点或未遍历所有结点) 20 最后一行,输出所给回路中最短的一条 21 */ 22 #include<cstdio> 23 #include<set> 24 #include<algorithm> 25 using namespace std; 26 const int M=1e3,INF=1e9; 27 int grap[M][M],path[M]; 28 29 int main() 30 { 31 #ifdef ONLINE_JUDGE 32 #else 33 freopen("Test.txt", "r", stdin); 34 #endif // ONLINE_JUDGE 35 36 fill(grap[0],grap[0]+M*M,INF); 37 int n,m,k,v1,v2; 38 scanf("%d%d", &n,&m); 39 for(int i=0; i<m; i++) 40 { 41 scanf("%d%d", &v1,&v2); 42 scanf("%d", &grap[v1][v2]); 43 grap[v2][v1]=grap[v1][v2]; 44 } 45 scanf("%d", &m); 46 int optJ,optValue=INF; 47 for(int j=1; j<=m; j++) 48 { 49 scanf("%d", &k); 50 set<int> ver; 51 for(int i=0; i<k; i++) 52 { 53 scanf("%d", &path[i]); 54 ver.insert(path[i]); 55 } 56 int reach=1,value=0; 57 for(int i=0; i<k-1; i++){ 58 if(grap[path[i]][path[i+1]] != INF) 59 value += grap[path[i]][path[i+1]]; 60 else 61 k=0; 62 } 63 if(k==0) 64 printf("Path %d: NA (Not a TS cycle)\n", j); 65 else 66 { 67 if(path[0]!=path[k-1] || ver.size()<n) 68 printf("Path %d: %d (Not a TS cycle)\n",j,value); 69 else 70 { 71 if(value < optValue) 72 { 73 optValue = value; 74 optJ = j; 75 } 76 if(k==n+1) 77 printf("Path %d: %d (TS simple cycle)\n",j,value); 78 else 79 printf("Path %d: %d (TS cycle)\n",j,value); 80 } 81 } 82 } 83 printf("Shortest Dist(%d) = %d", optJ,optValue); 84 }