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 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
结尾无空行
本题一点也不难,就是一些小细节比较难注意,而且英文的题目比较难理解,需要读懂题,题目大致意思是:给定一个图,并且给定一个制定的路径,如果此路径经过有所有点一次,并且最终回到起点,说明是 simple path,如果经过某个点两次,说明只是一个 path,如果不能到达某点或者重点不是起点,说明 not a path,最后计算最短的那条路即可。
坑点分析:
1、起点要等于终点,但是起点要访问两次,所以最开始起点不要设置 true
2、有可能存在路径不存在的问题,这里我们设置无穷大,类似 迪杰斯特拉,如果存在路径不存在,直接输出 NA
3、建议编写代码编写注解,调试的时候方便理解
4、记录起点的位置,当输入终点的时候,判断是否相等
本题基本上他给的样例全过了,就是 25 分了,加油
#include<iostream>
#include<string>
#include<algorithm>
#include<bits/stdc++.h>
#include<stack>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<deque>
#include<cctype>
#include<unordered_set>
#include<unordered_map>
#include<fstream>
#include<cstring>
using namespace std;
const int N = 201;
int G[N][N];
int shortest = 1e9;
int shortestId = 0;
int main() {
int n, m;
cin >> n >> m;
fill(G[0], G[0] + N * N, 1e9);
while (m--) {
int x, y, d;
cin >> x >> y >> d;
G[x][y] = d;
G[y][x] = d;
}
int K;
cin >> K;
for (int t = 1; t <= K; t++) {
int a[n + 1] = {0};
int k;
cin >> k;
int path = 0;
int u, uu;
cin >> u;
uu = u;
bool flagA = true;// 判断是否重复参观
bool flagB = true;// 判断是否全参观过
bool flagC = true;// 判断是否存在路径
for (int i = 1; i < k; i++) {
int v;
cin >> v;
if (a[v]) {// 已经参观过
flagA = false;
}
if (G[u][v] == 1e9) {// 路径不存在
flagC = false;
flagB = false;// 也就没全参加
} else {
path += G[u][v];
}
a[v] = 1;
u = v;
if (i == k - 1) {// 起点与终点对比
if (v != uu) {
flagB = false;
}
}
}
if (!flagC) {// 路径不存在
printf("Path %d: NA ", t);
} else {
printf("Path %d: %d ", t, path);
}
for (int i = 1; i <= n; i++) {// 判断是否全参加
if (!a[i]) {
flagB = false;
break;
}
}
if (!flagB) {
printf("(Not a TS cycle)\n");
} else {
if (shortest > path) {// 记录最短距离和编号
shortest = path;
shortestId = t;
}
if (flagA) {
printf("(TS simple cycle)\n");
} else {
printf("(TS cycle)\n");
}
}
}
printf("Shortest Dist(%d) = %d", shortestId, shortest);
return 0;
}