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:
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
分析
线路为环且经过每个顶点,就是一个TS cycle, 如果刚好经过的定点个数为N+1,那么恰恰是TS simple cyscle。先用vector把路线存放起来,用set记录线路中的所有顶点(并去除重复的顶点),当vector的第一个元素与最后一个元素相等,则是环,如果set的大小刚好等于N,那么是TS cycle,再如果vector的大小为N+1,那么是TS simple cycle。
#include <bits/stdc++.h>
#define maxn 205
#define INF 1000000000
using namespace std;
int G[maxn][maxn];
int main() {
fill(G[0], G[0]+maxn*maxn, 0);
int n, m, u, v, dis, k, nums;
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> u >> v >> dis;
G[u][v] = G[v][u] = dis;
}
cin >> k;
int min_dis = INF, min_idx;
for (int j = 1; j <= k; j++) {
bool iscycle = false, issimple = false;
cin >> nums;
vector<int> temp(nums);
set<int> cnt;
for (int i = 0; i < nums; i++) {
cin >> temp[i];
cnt.insert(temp[i]);
}
if (temp[0] == temp[nums-1] && cnt.size() == n) {
iscycle = true;
}
if (temp.size() == n+1) {
issimple = true;
}
int tot_dis = 0;
for (int i = 0; i < temp.size()-1; i++) {
u = temp[i], v = temp[i+1];
if (G[u][v] != 0) {
tot_dis += G[u][v];
} else {
tot_dis = -1;
break;
}
}
printf("Path %d: ", j);
if (tot_dis != -1) {
if (tot_dis < min_dis && iscycle) {
min_dis = tot_dis;
min_idx = j;
}
cout << tot_dis << " ";
} else {
iscycle = false;
cout << "NA ";
}
if (iscycle && issimple) {
cout << "(TS simple cycle)";
} else if (iscycle && !issimple) {
cout << "(TS cycle)";
} else if (!iscycle) {
cout << "(Not a TS cycle)";
}
cout << endl;
}
printf("Shortest Dist(%d) = %d", min_idx, min_dis);
return 0;
}