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

PAT甲级——A1150 TravellingSalesmanProblem【25】

时间:2019-11-23 23:35:34      阅读:72      评论:0      收藏:0      [点我收藏+]

标签:return   imp   import   fonts   dir   from   math   sel   follow   

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 M lines follow, each describes an edge in the format City1 City2 Dist, where the cities are numbered from 1 to N and the distance Dist 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:

C?1?? C?2?? ... C?n??

where n is the number of cities in the list, and C?i??‘s are the cities on a path.

Output Specification:

For each path, print in a line Path X: TotalDist (Description) where X is the index (starting from 1) of that path, TotalDist its total distance (if this distance does not exist, output NA instead), and Description 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 where X is the index of the cycle that is the closest to the solution of a travelling salesman problem, and TotalDist 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

Solution:
  这道题是水题,就是一个简单的计算路程和判断的过程,不用大脑
 1 #include <iostream>
 2 #include <vector>
 3 using namespace std;
 4 int n, m, k, x;
 5 int dis[205][205] = { 0 };
 6 int main()
 7 {
 8     cin >> n >> m;
 9     for (int i = 0; i < m; ++i)
10     {
11         int a, b, c;
12         cin >> a >> b >> c;
13         dis[a][b] = dis[b][a] = c;
14     }
15     cin >> k;
16     int minDis = INT32_MAX, minIdex = 0;
17     for (int t = 1; t <= k; ++t)
18     {
19         int calDis = 0;
20         bool isCycle = true;
21         vector<bool>visit(n + 1, true);
22         cin >> x;
23         vector<int>path(x);
24         for (int i = 0; i < x; ++i)
25         {
26             cin >> path[i];
27             visit[path[i]] = false;
28         }
29         for (int i = 1; i < x; ++i)
30         {
31             if (dis[path[i - 1]][path[i]] > 0)
32                 calDis += dis[path[i - 1]][path[i]];
33             else//此路不通
34             {
35                 isCycle = false;
36                 calDis = -1;//没有结果。输出为NA
37                 break;
38             }
39         }
40         if (path[0] != path[x - 1])isCycle = false;//不是回路
41         for (int i = 1; i <= n && isCycle; ++i)
42             if (visit[i] == true)
43                 isCycle = false;
44         if(calDis<0)
45             printf("Path %d: NA (Not a TS cycle)\n", t);
46         else if (!isCycle)
47             printf("Path %d: %d (Not a TS cycle)\n", t, calDis);
48         else if(x==n+1)
49             printf("Path %d: %d (TS simple cycle)\n", t, calDis);
50         else 
51             printf("Path %d: %d (TS cycle)\n", t, calDis);
52         if (isCycle && minDis > calDis)
53         {
54             minDis = calDis;
55             minIdex = t;
56         }
57     }
58     printf("Shortest Dist(%d) = %d", minIdex, minDis);
59     return 0;
60 }

 

 

PAT甲级——A1150 TravellingSalesmanProblem【25】

标签:return   imp   import   fonts   dir   from   math   sel   follow   

原文地址:https://www.cnblogs.com/zzw1024/p/11920574.html

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