PAT 1150 Travelling Salesman Problem (25)

1150 Travelling Salesman Problem (25) (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≤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​​ ... 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

思路:

按照所给出的路径走一遍,用dis记录距离(初始化为0),用type记录该条路的类型(1为TS简单环,2为TS环,3为非TS环,初始化为3)。如果走不通就将dis记为-1,输出NA,否则每次走完就和mind比较,记录最短距离和其编号。每走一个节点就用vis数组标记一次,如果走到最后只有终点和起点重复则type为1,如果在中间就有重复则type为2。还需注意的是要检查是否经过了所有节点,如果没有就还需将type修改为3。

代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>  
#include <set>
using namespace std;

#define INF 0x3f3f3f3f

vector<int> path;
int g[205][205], vis[205];
int mind = INF, minid = 0;

void solve(int n, int id, int &dis, int &type)
{
	memset(vis, 0, sizeof(vis));
	int k = path[0];
	vis[k] = 1;
	for (int i = 1; i < path.size(); i++)
	{
		int t = path[i];
		if (g[k][t] != INF)
		{
			dis += g[k][t];
			if (vis[t] && type == 3)
			{
				if (i == path.size() - 1)
					type = 1;
				else
					type = 2;
			}
			vis[t] = 1;
			k = t;
		}
		else
		{
			dis = -1;
			return;
		}
	}
	if (type != 3)
	{
		for (int i = 1; i <= n; i++)
		{
			if (!vis[i])
			{
				type = 3;
				return;
			}
		}
		if (path[0] != path[path.size() - 1])
		{
			type = 3;
			return;
		}
	}
	if (dis < mind)
	{
		mind = dis;
		minid = id;
	}
}

int main()
{
	int n, m, k, id = 0;
	scanf("%d%d", &n, &m);
	memset(g, INF, sizeof(g));
	for (int i = 0; i < m; i++)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		g[a][b] = g[b][a] = c;
	}
	scanf("%d", &m);
	for (int i = 0; i < m; i++)
	{
		id++;
		scanf("%d", &k);
		path.clear();
		path.resize(k);
		for (int j = 0; j < k; j++)
			scanf("%d", &path[j]);
		int dis = 0, type = 3;
		solve(n, id, dis, type);
		printf("Path %d: ", id);
		if (dis == -1)
			printf("NA ");
		else
			printf("%d ", dis);
		if (type == 1)
			printf("(TS simple cycle)\n");
		else if (type == 2)
			printf("(TS cycle)\n");
		else
			printf("(Not a TS cycle)\n");
	}
	printf("Shortest Dist(%d) = %d\n", minid, mind);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/ryo_218/article/details/82562137