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.

题解:
用邻接矩阵存储图信息。再用一个vector来存储路径信息,通过循环来一步步遍历图,同时用一个变量存储路径长度和最小路径长度。用set数组存储遍历过的点,若最后遍历的点数目不对则输出错误。
刚开始我的代码和后面一致,只是把vector数组放在了函数外:
输出一致但答案全错。

后来我将vector数组放进了函数里:
#include<iostream>
#include<set>
#include<vector>
using namespace std;
const int maxv=209,INF=1000000000;
int G[maxv][maxv];
int mindis=INF,minindex=-1;
int n;
void travel(int index){
int num;
cin>>num;
int nowdis=0;
set<int> s;
vector<int> dis(num);
for(int i=0;i<num;i++) cin>>dis[i];
s.insert(dis[0]);
int i;
for(i=1;i<dis.size();i++){
if(G[dis[i-1]][dis[i]]==0){
printf("Path %d: NA (Not a TS cycle)\n",index);
return;
}
else{
nowdis+=G[dis[i-1]][dis[i]];
s.insert(dis[i]);
}
}
if(s.size()==n&&dis[0]==dis[dis.size()-1]){
if(nowdis<mindis){
mindis=nowdis;
minindex=index;
}
if(dis.size()==n+1){
printf("Path %d: %d (TS simple cycle)\n",index,nowdis);
return;
}
else if(dis.size()>n+1){
printf("Path %d: %d (TS cycle)\n",index,nowdis);
return;
}
}
else{
printf("Path %d: %d (Not a TS cycle)\n",index,nowdis);
return;
}
}
int main(){
int num,k,m;
cin>>n>>m;
int v1,v2,w;
while(m--){
cin>>v1>>v2>>w;
G[v1][v2]=G[v2][v1]=w;
}
cin>>k;
for(int i=1;i<=k;i++) travel(i);
printf("Shortest Dist(%d) = %d\n",minindex,mindis);
return 0;
}

但是这样又是可以的,暂时没找到问题的原因,但估计是和vector存储方式等相关
本文介绍了一种解决旅行商问题的方法,通过邻接矩阵存储城市间的距离,并利用vector存储路径信息,实现对给出路径集合中接近最优解的简单回路或回路的查找。
482

被折叠的 条评论
为什么被折叠?



