A1150 Travelling Salesman Problem (25 point(s))

简单图判断

1. 原文

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<N≤200), 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:

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) 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:

Sample output:

2. 解析

  • TS simple cycle 简单 每个点仅访问一次
  • TS cycle
  • Not a TS cycle 不简单|不是环|开始点不等于结束点

用graph记录路径信息,初始值为Inf;用visit记录访问信息

对于每个点:

如果当前visit[i]==true,则说明之前有访问过该点,就不是简单路径

如果 graph 为Inf, 则说明路径不可达

3. AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=210;
const int Inf=312312312;
int graph[maxn][maxn];
bool visit[maxn]={false};
int main()
{
fill(graph[0],graph[0]+maxn*maxn,Inf);
int n,m;
scanf("%d%d",&n,&m);
int a,b;
for (int i = 0; i < m; ++i)
{
scanf("%d%d",&a,&b);
scanf("%d",&graph[a][b]);
graph[b][a]=graph[a][b];
}
int k;
scanf("%d",&k);
int cnt; int min=Inf; int idx=0;
for (int i = 1; i <= k; ++i)
{
printf("Path %d: ",i);
//init
int s,start,t,sum;
bool cycle=true;
bool simple=true;
bool na=false;
bool connected=true;
fill(visit,visit+maxn,false);

scanf("%d%d",&cnt,&s);
start=s; sum=0;
for (int j = 1; j < cnt; ++j)
{
scanf("%d",&t);
if (graph[s][t]==Inf)
{
na=true;
connected=false;
}else{
sum+=graph[s][t];
}
if (visit[t]==true)
{
simple=false;
}
visit[t]=true;
s=t;
}
for (int i = 1; i <= n; ++i)
{
if (visit[i]==false)
{
cycle=false;
}
}
if (start!=t||!connected||!cycle)
{
if (na)
{
printf("NA (Not a TS cycle)\n");
}else{
printf("%d (Not a TS cycle)\n",sum);
}
continue;
}else{
if (simple)
{
printf("%d (TS simple cycle)\n",sum);
}else{
printf("%d (TS cycle)\n",sum);
}
if (sum<min)
{
min=sum;
idx=i;
}
}
}
printf("Shortest Dist(%d) = %d\n",idx,min);
return 0;
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/02/A1150/
  • 发布时间: 2019-09-02 11:01
  • 更新时间: 2021-10-29 14:11
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!