PAT Advanced 1150 Travelling Salesman Problem (25) [图论-旅行商问题]

题目

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_proble m ”.) 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

题意

旅行商问题:旅行商从一个城市出发,求经过地图上所有城市后回到出发城市的最短路径
本题并不是寻找旅行商问题的答案,而是给出一系列路径,判断是否是解决旅行商问题的环路,如果是,判断是简单环路还是非简单环路

题目分析

已知图、顶点、边,已知一系列路径,判断是否是旅行商环路,再判断是简单环路还是非简单环路
旅行商环路要求:

  1. 必须经过所有顶点
  2. 必须是环

简单环路要求:

  1. 只有一个环(旅行商环路中除了出发顶点外再无重复顶点)

解题思路

  1. 邻接矩阵存储图
  2. 判断是否旅行商环路:路径顶点数==图顶点数&&第一个顶点和最后一个顶点相同
  3. 判断是否简单环路:旅行商环路中有无重复顶点(无重复顶点为简单环路)

Code

#include <iostream>
#include <vector>
#include <set>
#include <cstring>
using namespace std;
const int maxn=220,INF=0x7fffffff;
int e[maxn][maxn];
int main(int argc,char * argv[]) {
	int n,m,a,b,k,c,w;
	scanf("%d %d",&n,&m);
	for(int i=0; i<m; i++) {
		scanf("%d %d %d",&a,&b,&w);
		e[a][b]=e[b][a]=w;
	}
	scanf("%d",&k);
	int mini=-1,minw=INF;
	for(int i=1; i<=k; i++) {
		scanf("%d",&c);
		vector<int> lt(c);
		set<int> st;
		int isTs=1,isSimple=1,aw=0; //aw每条路径总边权
		string des;
		for(int j=0; j<c; j++) {
			scanf("%d",&lt[j]);
			st.insert(lt[j]);
		}
		for(int j=0; j<c-1; j++) {
			if(e[lt[j]][lt[j+1]]==0)isTs=0;
			aw+=e[lt[j]][lt[j+1]];
		}
		if(isTs==0) {
			aw=-1;
			des="Not a TS cycle";
		} else if(st.size()!=n||lt[0]!=lt[c-1]) {
			isTs=0;
			des="Not a TS cycle";
		} else if(c!=n+1) {
			isSimple=0; //若有顶点被访问两次,非简单环
			des="TS cycle";
		} else{
			des="TS simple cycle";
		}
		printf("Path %d: %s (%s)
", i, aw==-1?"NA":to_string(aw).c_str(), des.c_str());
		if(isTs==1&&minw>aw){
			minw=aw;
			mini=i;
		}
	}
	printf("Shortest Dist(%d) = %d",mini,minw);
	return 0;
}

原文地址:https://www.cnblogs.com/houzm/p/12384534.html