1150 Travelling Salesman Problem(25 point(s))

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

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

//AC code
/*1:输入数据,用邻接矩阵存储边2:读入一条待测路v[i],并插入 set<int> s;3: 遍历待测路径,sum 统计有效路径长度,并用 flag 标记是否有路径不可达,若不可达,及时跳出 4:ok,现在开始枚举可能出现结果:(1):: falg== 0 ,即路径不合法 (2):: flag== 1 ,存在路径首位不同(有去无回 ) 或者 路径中规定景点没逛到(偷工减料) (3):: 到此步,说明逛了一圈且规定景点都逛了,满足旅行商环路的条件,路径合法 再判断是否走了n+1各节点(是否存在一个景点光了两次)5: 输出最短合法路径长度 6:注意格式输出
*/
#include<iostream>
#include<vector>
#include<set>
#include<climits>
using namespace std;int e[300][300],n, m, k, ans = INT_MAX,ans_id;
vector<int> v;void check(int index){int sum=0,cnt;cin>>cnt;set<int>s;vector<int>v(cnt);for (int i=0;i<cnt;i++){scanf("%d",&v[i]);s.insert(v[i]); //插入自动排序,并删除重复元素 /*set集合容器实现了红黑树的平衡二叉检索树的数据结构,它会自动调整二叉树的排列,把元素放到适当的位置。set容器所包含的元素的值是唯一的,集合中的元素按一定的顺序排列。用迭代器访问测试 : set<int>::iterator it;for(it=s.begin() ;it!=s.end() ;it++)cout<<(*it)<<" ";*/ }bool flag=true; // flag 用于标记是否存在不可达路径  for (int i=0;i<cnt-1;i++){if(e[v[i]][v[i+1]]== 0){flag=false; break; // 跳出节省时间 }sum+=e[v[i]][v[i+1]];}if(flag==false){ cout<<"Path "<<index<<": NA (Not a TS cycle)"<<endl; }else if(v[0]!=v[cnt-1] || s.size()!= n){ cout<<"Path "<<index<<": "<<sum<<" (Not a TS cycle)"<<endl; }else if(cnt!=n+1){  cout<<"Path "<<index<<": "<<sum<<" (TS cycle)"<<endl;if (sum<ans){ans=sum;ans_id=index; }} else{ //源点访问两次,simple cycle cout<<"Path "<<index<<": "<<sum<<" (TS simple cycle)"<<endl;if (sum<ans){ans =sum;ans_id=index;}}
}
int main(){scanf("%d%d",&n,&m);for (int i=0;i<m;i++) {int c1,c2,c;cin>>c1>>c2>>c;e[c1][c2]=e[c2][c1]=c;}cin>>k;for(int i=1;i<=k;i++) check(i);cout<<"Shortest Dist("<<ans_id<<") = "<<ans<<endl;return 0;
}

1150 Travelling Salesman Problem相关推荐

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

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

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

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

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

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

  4. cf1504. Travelling Salesman Problem

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

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

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

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

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

  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. 基于图像的摄像机姿态估计方法评析
  2. R语言dplyr包mutate_at函数通过名称修改指定数据列的内容(使用统一的函数)实战
  3. 不只会卖萌:皮卡丘们真的能改造你的大脑
  4. 机器学习笔记十:各种熵总结
  5. python读取excel部分值存入另一个excel-python3读取excel文件只提取某些行某些列的值方法...
  6. 关于 ES6 的 let ,var和 const
  7. python集合set,frozenset--笔记
  8. ideal 本地jar依赖_通过 YARN 的资源本地化技术减少 Flink 在 YARN 上的部署时间
  9. 乐思启慧教学系列—Bootstrap布局规则
  10. Node.js 中 exports 和 module.exports 的区别
  11. python与施耐德plc通讯_施耐德PLC两种编程通讯控制实例分享
  12. IMX8mp alsa音频调试
  13. 【转】一个时代的剪影----汉
  14. 祥云发卡网站源码带详细图文搭建教程
  15. 1143 Lowest Common Ancestor (30分) 附测试点分析
  16. 量化基金 获取每日基金排行数据和其对应持仓情况;统计持股股票排行
  17. javascript history对象详解
  18. 找到抓手,用对方法,中电金信关于金融机构数据治理建设路径分享
  19. python练习题19:四叶玫瑰数
  20. .net之EF框架学习

热门文章

  1. 用360查看本地dns
  2. 搭建完全分布式HBase
  3. win10文件资源管理器保存搜索记录的操作办法
  4. matlab sym是什么意思,matlab sym什么意思
  5. 传说华为面试爱问正则,这不海外留学生校招面试题来了
  6. xp计算机描述不能修改,如果WinXP无法更改密码怎么办?该怎么解决?
  7. 【论文阅读】Self-Knowledge Distillation with Progressive Refinement of Targets
  8. TDMA噪音产生机制及抑制
  9. mt4和mt5的区别
  10. 【步兵 经验篇】one step