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
)
N (2<N≤200)
N(2<N≤200), the number of cities, and
M
M
M, the number of edges in an undirected graph. Then
M
M
M lines follow, each describes an edge in the format City1 City2 Dist
, where the cities are numbered from 1 to
N
N
N and the distance Dist
is positive and is no more than 100. The next line gives a positive integer
K
K
K which is the number of paths, followed by
K
K
K lines of paths, each in the format:
n
n
n
C
1
C_1
C1
C
2
C_2
C2 …
C
n
C_n
Cn
where
n
n
n is the number of cities in the list, and
C
i
C_i
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:
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
Caution:
主要就是判断逻辑,NA
最好判断,如果碰到不存在的路径就直接可以判为NA
,并且后面不可能改变,这个最简单的判断就直接在输入路径的同时进行;
然后就看是不是 TS Cycle
—— 如果首尾节点不一样的话肯定不是 TS Cycle
,退出判断;
如果路径里面有节点没有被访问的话也不可能是 TS Cycle
,退出判断;
接下来就好办了,直接看路径里面的节点个数,需要注意的是如果判断进行到了这一步那么肯定这个路径是一个 TS Cycle
并且访问到了所有的节点,那么我们可以直接从路径的节点个数来判断这个路径是不是一个 simple cycle
。
Solution:
// Talk is cheap, show me the code
// Created by Misdirection 2021-08-31 21:33:23
// All rights reserved.
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
int graph[205][205] = {0};
int path[500];
int main(){
int n, m;
scanf("%d %d", &n, &m);
int id1, id2, dis;
for(int i = 0; i < m; ++i){
scanf("%d %d %d", &id1, &id2, &dis);
graph[id1][id2] = dis;
graph[id2][id1] = dis;
}
int k;
scanf("%d", &k);
int shortest = 2147483647;
int ansId = -1;
for(int i = 0; i < k; ++i){
int num;
scanf("%d", &num);
int flag = -2;
// flag = -2 表示 undecided
// flag = -1 表示 NA:只有遇到不存在的路径才会 NA
// flag = 0 表示 Not a TS Cycle:【不是一个 cycle】 或者 【没有遇到所有的 cities】
// flag = 1 表示 TS cycle:是一个 cycle,并且访问了每一个 city,但是除了首尾节点有重复访问的;
// flag = 2 表示 TS simple cycle:是一个 cycle 并且访问了每一个 city,并且只访问一次;
// 只要是 TS cycle 就会参与最优路线比较
if(num <= n) flag = 0;
int pathLen = 0;
unordered_map<int, int> times;
for(int j = 0; j < num; ++j){
scanf("%d", &path[j]);
if(j != 0 && graph[path[j - 1]][path[j]] == 0) flag = -1;
pathLen += graph[path[j - 1]][path[j]];
if(j != 0) times[path[j]]++;
}
if(flag != -1){
if(path[0] != path[num - 1]) flag = 0;
else{
for(int j = 1; j <= n; ++j){
if(times[j] == 0){
flag = 0;
break;
}
}
if(flag != 0) flag = (num == n + 1 ? 2 : 1);
}
}
if(flag == -1) printf("Path %d: NA (Not a TS cycle)\n", i + 1);
else if(flag == 0) printf("Path %d: %d (Not a TS cycle)\n", i + 1, pathLen);
else{
if(pathLen < shortest){
shortest = pathLen;
ansId = i + 1;
}
if(flag == 1) printf("Path %d: %d (TS cycle)\n", i + 1, pathLen);
else printf("Path %d: %d (TS simple cycle)\n", i + 1, pathLen);
}
}
printf("Shortest Dist(%d) = %d\n", ansId, shortest);
return 0;
}