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 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
代码
#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;
}
PAT1150 Travelling Salesman Problem相关推荐
- PAT甲级1150 Travelling Salesman Problem:[C++题解]旅行商问题、图论
文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 旅行商问题:访问每个城市并回到原城市的最短路. 思路: 1)判断相邻两点有无距离(NA):2)每个点是否都能到:3)是否是回路:4) ...
- cf1504. Travelling Salesman Problem
cf1504. Travelling Salesman Problem 题意: n个城市,编号1~n,每个城市有美丽值a[i],现在要从城市1出发,其他所有城市走一遍,最后回到城市1,城市i到j的花费 ...
- PAT 1150 Travelling Salesman Problem(25 分)- 甲级
The "travelling salesman problem" asks the following question: "Given a list of citie ...
- 单目标应用:求解单仓库多旅行商问题(Single-Depot Multiple Travelling Salesman Problem, SD-MTSP)的人工兔优化算法ARO
一.算法简介 人工兔优化算法(Artificial Rabbits Optimization ,ARO)由Liying Wang等人于2022年提出,该算法模拟了兔子的生存策略,包括绕道觅食和随机躲藏 ...
- 【HDU 5402】Travelling Salesman Problem(构造)
被某题卡SB了,结果这题也没读好...以为每一个格子能够有负数就当搜索做了.怎么想也搜只是去,后来发现每一个格子是非负数,那么肯定就是构造题. 题解例如以下: 首先假设nn为奇数或者mm为奇数,那么显 ...
- 1150 Travelling Salesman Problem (25 分)【难度: 难 / 知识点: 图 模拟 未完成】
https://pintia.cn/problem-sets/994805342720868352/problems/1038430013544464384
- Codeforces Round #712 (Div. 2) E. Travelling Salesman Problem 思维转换
传送门 文章目录 题意: 思路: 题意: 给你nnn个点,从iii到jjj的花费是max(ci,aj−ai)max(c_i,a_j-a_i)max(ci,aj−ai),求从111开始经过每个点再 ...
- 旅行商问题(Travelling salesman problem, TSP)
旅行商问题建模与证明 – 个人学习记录
- 旅行商问题(travelling salesman problem, TSP) 解题报告
旅行商问题是个熟知的问题.这次是因为coursera上面选的算法课而写代码实现.这里做个简单总结. 测试程序: 25 20833.3333 17100.0000 20900.0000 17066.66 ...
- JavaScript实现Travelling Salesman算法(附完整源码)
JavaScript实现Travelling Salesman算法(附完整源码) bfTravellingSalesman.js完整源代码 bfTravellingSalesman.js完整源代码 f ...
最新文章
- JavaScript DOM介绍
- python爬虫之路--准备环境
- C#面向对象系列(2):构造函数的用法
- php 中 sql 查询语句怎么写,sql查询语句有哪些
- 论一只爬虫的自我修养(第二天)
- linux时间和win10差8小时,[转载]Ubuntu16.04与Win10时间差8个小时问题解决方案
- 高并发的常见策略--大型web项目
- 种子文件转成为磁力链接 下载BT磁力转换小工具
- c语言编写ocr软件,开源OCR引擎Tesseract
- WebView·开车指南
- Mac软件下载提示:“已损坏,无法打开”解决办法
- ZigBee-CC2530单片机 - 4路硬件定时器PWM输出
- vue组件之间的数据通信
- 【深度学习】Yolo记录
- 树莓派3B+安装官方原版系统
- 【PM学习笔记】酸梅干超人 - 零基础学Figma学习笔记
- Mybatis-01-配置详解
- 基于Python统计红楼梦中人物信息
- C#使用随机数模拟器来模拟世界杯排名(二)
- 计算机图像学基础课程设计,计算机图形学课程设计