1150 Travelling Salesman Problem(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), 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 cycleif it is a simple cycle that visits every city;TS cycleif it is a cycle that visits every city, but not a simple cycle;Not a TS cycleif 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
给出一个无向有权图,然后给出k个路径,进行判断,是否是能访问所有城市的简单环,显然需要记录访问了几个城市,以及路径是否通,如果路径不通直接是NA,然后考虑其他的,要形成环,路径最少得有n + 1个点,且首尾要相同,而且路径要访问所有点,如果都满足了,要判断是不是简单环,简单环必须是n + 1个点。
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <set>
#define inf 0x3f3f3f3f
#define MAX
using namespace std;
int mp[][];
int path[];
int n,m,k;
int u,v,w,kk,mint,mind = inf;
int main() {
scanf("%d%d",&n,&m);
for(int i = ;i < m;i ++) {
scanf("%d%d%d",&u,&v,&w);
mp[u][v] = mp[v][u] = w;
}
scanf("%d",&k);
for(int i = ;i <= k;i ++) {
scanf("%d",&kk);
int vis[] = {},c = ,d = ;
for(int j = ;j < kk;j ++) {
scanf("%d",&path[j]);
if(!vis[path[j]]) c ++;
vis[path[j]] ++;
}
for(int j = ;j < kk;j ++) {
if(mp[path[j]][path[j - ]]) {
d += mp[path[j]][path[j - ]];
}
else {
c = -;
break;
}
}
if(c == -) printf("Path %d: NA (Not a TS cycle)\n",i);
else if(kk <= n || c < n || path[] != path[kk - ]) printf("Path %d: %d (Not a TS cycle)\n",i,d);
else {
if(kk == n + ) printf("Path %d: %d (TS simple cycle)\n",i,d);
else printf("Path %d: %d (TS cycle)\n",i,d);
if(mind > d) {
mint = i;
mind = d;
}
}
}
printf("Shortest Dist(%d) = %d",mint,mind);
}
1150 Travelling Salesman Problem(25 分)的更多相关文章
- PAT 甲级 1150 Travelling Salesman Problem
https://pintia.cn/problem-sets/994805342720868352/problems/1038430013544464384 The "travelling ...
- 1150 Travelling Salesman Problem
The "travelling salesman problem" asks the following question: "Given a list of citie ...
- PAT_A1150#Travelling Salesman Problem
Source: PAT A1150 Travelling Salesman Problem (25 分) Description: The "travelling salesman prob ...
- PAT A1150 Travelling Salesman Problem (25 分)——图的遍历
The "travelling salesman problem" asks the following question: "Given a list of citie ...
- HDOJ 5402 Travelling Salesman Problem 模拟
行数或列数为奇数就能够所有走完. 行数和列数都是偶数,能够选择空出一个(x+y)为奇数的点. 假设要空出一个(x+y)为偶数的点,则必须空出其它(x+y)为奇数的点 Travelling Salesm ...
- HDU 5402 Travelling Salesman Problem (构造)(好题)
大致题意:n*m的非负数矩阵,从(1,1) 仅仅能向四面走,一直走到(n,m)为终点.路径的权就是数的和.输出一条权值最大的路径方案 思路:因为这是非负数,要是有负数就是神题了,要是n,m中有一个是奇 ...
- HDU 5402 Travelling Salesman Problem (模拟 有规律)(左上角到右下角路径权值最大,输出路径)
Travelling Salesman Problem Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (J ...
- 构造 - HDU 5402 Travelling Salesman Problem
Travelling Salesman Problem Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5402 Mean: 现有一 ...
- PAT-1150(Travelling Salesman Problem)旅行商问题简化+模拟图+简单回路判断
Travelling Salesman Problem PAT-1150 #include<iostream> #include<cstring> #include<st ...
随机推荐
- docker helloworld
阿里云镜像: docker官网 docker官方安装文档 配置阿里云镜像后,重启服务,以及检测服务是否正确启动 docker version.info.--help linux帮助命令man ls 镜 ...
- webbrowser控件——Windows下的开发利器
首先说明,本人比较菜,做C++没多长时间. 刚开始用MFC写程序时,连个基本的字体都不会变(颜色.大小等), 索性干脆就啥也不改了,直接默认,界面就那样了,老子不管了. 心想这C++做个界面咋就那么麻 ...
- Python学习进程(13)文件与IO
本节介绍基本的IO函数和文件的读写操作. (1)读取键盘输入: Python用于读取键盘输入的函数有两个:raw_input与input. 1)raw_input函数 从标准输入读取一 ...
- MATLAB画图设置长宽。并高清复制
- Qt配置USBCAN通信
周立功为CAN通信提供了动态库:官方提供了很多相关动态库和lib等,如图 ,其中kerneldlls里还有很多动态库,还有一个配置文件.其实这么多的文件,如果我们只用到USBCAN2通信,只需要ker ...
- INSPIRED启示录 读书笔记 - 第26章 合理运用敏捷方法
十大秘诀 1.产品经理即是产品负责人,他代表了客户的需求,因而需要与产品开发团队保持密切的联系,协助督促开发进程,及时解决出现的问题 2.使用敏捷方法绝不等于省略产品规划.规划周期应该适度缩短,反复迭 ...
- INSPIRED启示录 读书笔记 - 第24章 平滑部署
避免更新产品导致用户反感 毫无征兆地更新不必要的版本会令用户产生反感.不是所有用户都喜欢新版本的产品.用户产生反感主要有几个原因 1.事前没有收到更新通知,用户觉得措手不及 2.用户没时间学习.适应新 ...
- 把已安装的wampserver移动到不同目录使用应注意的问题
很多时候需要把已安装的wampserver移动到不同目录使用,此时应注意几个问题: 1.修改D:\wamp64\bin\apache\apache2.4.9\conf目录下的httpd.conf文件( ...
- vs 2010 mvc 3.0安装软件
下载链接如下:MVC 3安装包:http://www.microsoft.com/downloads/zh-cn/details.aspx?familyid=d2928bc1-f48c-4e95-a0 ...
- psd文件中截取固定大小的图片
1.选择需要操作的图层 使用选框工具, 设置固定大小和固定大小的值,在图层上拉取选区 2.使用移动工具 使用垂直.水平居中 使选择的icon在选区块中间 3.再选择好块区域调整好位置后 使用截取工具 ...