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_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 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

题目大意:
  旅行商问题,但是简化了,给了路径判断是否是旅行商路径,有三种情况,每个城市只访问一次的简单路径,有重复访问的路径,和不存在的路径。最后输出时旅行商路径的最小长度。

思路:

  1. 定义一个struct,里面用map保存相邻两个结点的路径长度,如果map没定义则默认为0
  2. 用set来查看是否访问的所有城市
  3. x>N,则为有重复访问的路径

代码:(C++)(代码比较冗余,以后优化)

#include
#include
#include
#include

using namespace std;

struct node
{
	//第一个为结点,第二个为距离
	map<int,int> e;
};

int main()
{
	int N,M;
	cin>>N>>M;
	vector<node> v(N+1);
	int short_i, short_r = 999999999;
	for(int i=1; i<=M; i++)
	{
		int x,y,z;
		cin>>x>>y>>z;
		v[x].e[y] = z;
		v[y].e[x] = z;
	}
	int K;
	cin>>K;
	for(int i=1; i<=K; i++)
	{
		int x;
		cin>>x;
		set<int> s;
		vector<int> r(x);
		for(int j=0; j<x; j++)
		{
			int y;
			cin>>y;
			r[j] = y;
			s.insert(y);
		}
		int c = r[0]; //当前结点
		int sum = 0;
		int flag = 0;
		for(int j=1; j<x; j++)
		{
			if(v[c].e[r[j]] != 0)
			{
				sum+=v[c].e[r[j]];
				c = r[j];
			}
			else
			{
				cout<<"Path "<<i<<": NA (Not a TS cycle)\n";
				flag = 1;
				break;
			}
		}
		if(flag == 1)
			continue;
		if(r[0] != r[x-1] || s.size() != N)
			cout<<"Path "<<i<<": "<<sum<<" (Not a TS cycle)\n";
		else if(x > N+1 && s.size() == N)
		{
			if(short_r > sum)
			{
				short_i = i;
				short_r = sum;
			}
			cout<<"Path "<<i<<": "<<sum<<" (TS cycle)\n";
		}
			
		else if(x == N+1 && s.size() == N)
		{
			if(short_r > sum)
			{
				short_i = i;
				short_r = sum;
			}
			cout<<"Path "<<i<<": "<<sum<<" (TS simple cycle)\n";
		}
			
	}
	cout<<"Shortest Dist("<<short_i<<") = "<<short_r;
	
	return 0;
}

你可能感兴趣的:(PAT,PAT)