1. 程式人生 > >PAT 1150 Travelling Salesman Problem (25)

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;
}