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-05-14 21:23:23 3 Problem: PAT_A1150#Travelling Salesman Problem 4 AC: 39:04 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 23 #include<cstdio> 24 #include<algorithm> 25 using namespace std; 26 const int M=220,INF=1e9; 27 int grap[M][M],path[M],vis[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,Min=INF,pos; 38 scanf("%d%d", &n,&m); 39 for(int i=0; i<m; i++) 40 { 41 int v1,v2,w; 42 scanf("%d%d%d", &v1,&v2,&w); 43 grap[v1][v2]=w; 44 grap[v2][v1]=w; 45 } 46 scanf("%d", &m); 47 for(int i=1; i<=m; i++) 48 { 49 fill(vis, vis+M, 0); 50 int k,sum=0,v1,v2,f=0; 51 scanf("%d", &k); 52 for(int j=0; j<k; j++) 53 scanf("%d", &path[j]); 54 v1=path[0];vis[v1]=1; 55 for(int j=1; j<k; j++) 56 { 57 v2=path[j];vis[v2]=1; 58 if(grap[v1][v2]!=INF) 59 sum += grap[v1][v2]; 60 else 61 {f=1;break;} 62 v1 = v2; 63 } 64 for(int j=1; j<=n; j++) 65 if(vis[j]==0 && f!=1) 66 {f=2;break;} 67 if(f==1) 68 printf("Path %d: NA (Not a TS cycle)\n", i); 69 else if(f==2 || path[0]!=path[k-1] || k<n+1) 70 printf("Path %d: %d (Not a TS cycle)\n", i, sum); 71 else 72 { 73 if(sum < Min) 74 { 75 Min = sum; 76 pos = i; 77 } 78 if(k == n+1) 79 printf("Path %d: %d (TS simple cycle)\n", i, sum); 80 else 81 printf("Path %d: %d (TS cycle)\n", i, sum); 82 } 83 } 84 printf("Shortest Dist(%d) = %d\n", pos, Min); 85 86 return 0; 87 }
标签:problem,TS,each,Path,Travelling,cities,Salesman,Problem,cycle 来源: https://www.cnblogs.com/blue-lin/p/10877802.html