文章目录

  • 题目
  • 代码

题目

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

PAT1150 Travelling Salesman Problem相关推荐

  1. PAT甲级1150 Travelling Salesman Problem:[C++题解]旅行商问题、图论

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 旅行商问题:访问每个城市并回到原城市的最短路. 思路: 1)判断相邻两点有无距离(NA):2)每个点是否都能到:3)是否是回路:4) ...

  2. cf1504. Travelling Salesman Problem

    cf1504. Travelling Salesman Problem 题意: n个城市,编号1~n,每个城市有美丽值a[i],现在要从城市1出发,其他所有城市走一遍,最后回到城市1,城市i到j的花费 ...

  3. PAT 1150 Travelling Salesman Problem(25 分)- 甲级

    The "travelling salesman problem" asks the following question: "Given a list of citie ...

  4. 单目标应用:求解单仓库多旅行商问题(Single-Depot Multiple Travelling Salesman Problem, SD-MTSP)的人工兔优化算法ARO

    一.算法简介 人工兔优化算法(Artificial Rabbits Optimization ,ARO)由Liying Wang等人于2022年提出,该算法模拟了兔子的生存策略,包括绕道觅食和随机躲藏 ...

  5. 【HDU 5402】Travelling Salesman Problem(构造)

    被某题卡SB了,结果这题也没读好...以为每一个格子能够有负数就当搜索做了.怎么想也搜只是去,后来发现每一个格子是非负数,那么肯定就是构造题. 题解例如以下: 首先假设nn为奇数或者mm为奇数,那么显 ...

  6. 1150 Travelling Salesman Problem (25 分)【难度: 难 / 知识点: 图 模拟 未完成】

    https://pintia.cn/problem-sets/994805342720868352/problems/1038430013544464384

  7. 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开始经过每个点再 ...

  8. 旅行商问题(Travelling salesman problem, TSP)

    旅行商问题建模与证明 – 个人学习记录

  9. 旅行商问题(travelling salesman problem, TSP) 解题报告

    旅行商问题是个熟知的问题.这次是因为coursera上面选的算法课而写代码实现.这里做个简单总结. 测试程序: 25 20833.3333 17100.0000 20900.0000 17066.66 ...

  10. JavaScript实现Travelling Salesman算法(附完整源码)

    JavaScript实现Travelling Salesman算法(附完整源码) bfTravellingSalesman.js完整源代码 bfTravellingSalesman.js完整源代码 f ...

最新文章

  1. JavaScript DOM介绍
  2. python爬虫之路--准备环境
  3. C#面向对象系列(2):构造函数的用法
  4. php 中 sql 查询语句怎么写,sql查询语句有哪些
  5. 论一只爬虫的自我修养(第二天)
  6. linux时间和win10差8小时,[转载]Ubuntu16.04与Win10时间差8个小时问题解决方案
  7. 高并发的常见策略--大型web项目
  8. 种子文件转成为磁力链接 下载BT磁力转换小工具
  9. c语言编写ocr软件,开源OCR引擎Tesseract
  10. WebView·开车指南
  11. Mac软件下载提示:“已损坏,无法打开”解决办法
  12. ZigBee-CC2530单片机 - 4路硬件定时器PWM输出
  13. vue组件之间的数据通信
  14. 【深度学习】Yolo记录
  15. 树莓派3B+安装官方原版系统
  16. 【PM学习笔记】酸梅干超人 - 零基础学Figma学习笔记
  17. Mybatis-01-配置详解
  18. 基于Python统计红楼梦中人物信息
  19. C#使用随机数模拟器来模拟世界杯排名(二)
  20. 计算机图像学基础课程设计,计算机图形学课程设计

热门文章

  1. 微型计算机升级换代的两种,嵌入式的LED点阵显示屏的研究与实现
  2. c语言 类型、运算符、表达式
  3. 第六章:Reminders实验:第二部分[Learn Android Studio 汉化教程]
  4. 算法基础知识——贪心策略
  5. ept技术_速懂X86虚拟化关键概念 - Intel EPT
  6. 如何搭建移动数据安全体系?
  7. android 铃音制作工具,来电铃声大全制作软件
  8. PDF编辑方法,怎么给PDF添加页码
  9. 开源社区ECE:Elastic认证考试复盘总结134贴
  10. 数学模型——基于差分方程的减肥模型(基于python)