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
介绍几个概念:
cycle:环,一个环是一个边的排列 ,并且满足沿着这个排列走一次可以回到起点。
simple cycle:简单环,除了路径的首尾顶点之外,其他顶点都是不同的环,即除了首尾顶点外,其他顶点均不重复出现。
TS simple cycle:遍历了图中所有顶点的简单环。
TS cycle:遍历了图中所有顶点,但不是简单环的环。
Not a TS cycle:不是遍历了图中所有顶点的环,可以认为既不是TS simple cycle,又不是TS cycle的其他情况均为Not a TS cycle。
解题思路:
用邻接矩阵来存储每条边之间的路径,当两个顶点之间没有路径时,便是NA;否则,观察点出现的特点,如果点数少于n+1个,肯定不是环,如果首尾不是同一个点,也不是环;如果满足首尾相同,但有结点没有出现过,也不是环;否则,记录每个顶点出现的次数判断其是简单环还是环
满分代码如下:
#include<bits/stdc++.h>
using namespace std;
const int N=205;
const int inf=0x3f3f3f3f;
int graph[N][N];
int n,m,k;
int path[N];
int main(){
scanf("%d%d",&n,&m);
//路径的初始化
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(i==j) graph[i][j]=0;
else graph[i][j]=inf;//表示两者之间没有路径存在
}
}
int u,v,w;
for(int i=1;i<=m;i++){
scanf("%d%d%d",&u,&v,&w);
graph[u][v]=graph[v][u]=min(graph[u][v],w);
}
scanf("%d",&k);
int min_dist=0x3f3f3f3f,index=-1;
for(int i=1;i<=k;i++){
int num,first,second;
scanf("%d",&num);
if(num==0){
printf("Path %d: NA (Not a TS cycle)\n",i);
}else if(num==1){
scanf("%d",&first);
printf("Path %d: 0 (Not a TS cycle)\n",i);
}else{
int flag=1;//flag用来标志回路
map<int,int>mp;
int sum=0;//记录该条路径的距离
for(int j=1;j<=num;j++){
scanf("%d",&path[j]);
mp[path[j]]++;
if(j>1){
if(graph[path[j-1]][path[j]]==inf){
flag=0;
}else{
sum+=graph[path[j-1]][path[j]];
}
}
}
if(flag==0){
printf("Path %d: NA (Not a TS cycle)\n",i);
continue;
}
if(num<=n){
printf("Path %d: %d (Not a TS cycle)\n",i,sum);
}else if(num==n+1){
if(path[1]!=path[num]){
printf("Path %d: %d (Not a TS cycle)\n",i,sum);
}else{
int res=1;
for(int j=1;j<=n;j++){
if(j==path[1]) continue;
if(mp[j]!=1){
printf("Path %d: %d (Not a TS cycle)\n",i,sum);
res=0;
break;
}
}
if(res){
printf("Path %d: %d (TS simple cycle)\n",i,sum);
if(sum<min_dist){
min_dist=sum;
index=i;
}
}
}
}else{
if(path[1]==path[num]){
printf("Path %d: %d (TS cycle)\n",i,sum);
if(sum<min_dist){
min_dist=sum;
index=i;
}
}else{
printf("Path %d: %d (Not a TS cycle)\n",i,sum);
}
}
}
}
printf("Shortest Dist(%d) = %d\n",index,min_dist);
return 0;
}