PAT1150 Travelling Salesman Problem

文章目录

题目

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 C1 C2 ……Cn​​

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

代码

#include <iostream>
#include <vector>
#include <algorithm>
#include <set> 
using namespace std;
const int INF = 1e9;
const int MAXV = 205;
int G[MAXV][MAXV];

int travel(vector<int> path, int &flag, int n){
	set<int> travelPath;
	int res = 0;
	int len = path.size();
	int begin = path[0];
	int end = path[len-1];
	if(begin == end)  flag = 0;// 简单圈 
	else flag = 2;
	int pre = path[0];
	travelPath.insert(pre);
	int cur;
	for(int i=1; i<len; i++){
		cur = path[i];
		travelPath.insert(cur);
		if(cur == begin && i < len - 1)  flag = 1;// 不止一个圈 
		if(G[pre][cur] != INF){
			res += G[pre][cur];
			pre = cur;
		}
		else{
			flag = 3;// 不可达 
			return INF;
		}
	}
	
	// 如果没有访问完 
	if(travelPath.size() < n)  flag = 2;
	return res; 	
}
int main(){
	int n;// 城市个数 
	int m;// 无向图的边
	int v1, v2, dis;
	scanf("%d %d", &n, &m);
	fill(G[0], G[0] + MAXV * MAXV, INF);
	for(int i=0; i<m; i++){
		scanf("%d %d %d", &v1, &v2, &dis);
		G[v1][v2] = dis;
		G[v2][v1] = dis;
	}
	int k, num, flag, minDis = INF, minNum = 0;
	scanf("%d", &k);
	vector<int> path;
	set<int> cmPath;
	for(int i=1; i<=k; i++){
		path.clear();
		flag = -1;
		scanf("%d", &num);
		path.resize(num);
		for(int j=0; j<num; j++){
			scanf("%d", &path[j]);
		}
		
		int totalDist = travel(path, flag, n);
		
		if(flag == 0){
			printf("Path %d: %d (TS simple cycle)\n", i, totalDist);
			// 更新最短路径 
			if(totalDist < minDis){
				minDis = totalDist;
				minNum = i;
			}
		}
		else if(flag == 1){
			printf("Path %d: %d (TS cycle)\n", i, totalDist);
			// 更新最短路径 
			if(totalDist < minDis){
				minDis = totalDist;
				minNum = i;
			}
		}
		else if(flag == 2){
			printf("Path %d: %d (Not a TS cycle)\n", i, totalDist);
		}
		else if(flag == 3){
			printf("Path %d: NA (Not a TS cycle)\n", i);
		}
		
	}
	printf("Shortest Dist(%d) = %d\n", minNum, minDis);
	return 0;
}
内容概要:本文介绍了MATLAB实现DBN-RBF深度置信网络结合RBF神经网络多输入单输出回归预测的详细项目实例。项目旨在通过深度置信网络(DBN)和径向基函数神经网络(RBF)的结合,设计出一种高效的回归预测模型,以应对高维数据和非线性关系的挑战。DBN用于无监督特征提取,RBF用于快速回归,两者结合显著提升了预测精度和模型泛化能力。文中详细描述了项目的背景、目标、挑战、解决方案、模型架构、代码实现、GUI设计、性能评估及未来改进方向。 适合人群:具备一定编程基础,对机器学习和深度学习有一定了解的研发人员,尤其是从事金融预测、医疗健康、智能制造等领域的工程师和技术人员。 使用场景及目标:①解决高维数据的特征提取难题,提升非线性回归的拟合精度;②通过无监督学习与快速训练能力的结合,提高模型的预测精度和泛化能力;③应用于金融预测、医疗健康、智能制造等多个领域,提供高效的回归预测工具;④通过实时数据流处理和GPU加速推理,确保系统在实时应用中的快速响应。 其他说明:此项目不仅提供了详细的理论分析和代码实现,还涵盖了系统架构设计、模型部署与应用、安全性与用户隐私保护等方面的全面指导。通过结合其他深度学习模型、多任务学习、增量学习等技术,项目具备广阔的扩展性和应用前景。系统还支持自动化CI/CD管道、API服务与业务集成、前端展示与结果导出等功能,确保了系统的高可用性和易用性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值