PAT 甲级 1150 Travelling Salesman Problem
https://pintia.cn/problem-sets/994805342720868352/problems/1038430013544464384
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 <bits/stdc++.h>
using namespace std; #define inf 0x3f3f3f3f
int N, M, K;
int dis[220][220];
int vis[220], go[220]; int main() {
scanf("%d%d", &N, &M);
memset(dis, inf, sizeof(dis));
while(M --) {
int st, en, cost;
scanf("%d%d%d", &st, &en, &cost);
if(cost < dis[st][en]) {
dis[st][en] = cost;
dis[en][st] = dis[st][en];
}
} scanf("%d", &K);
int temp = 0, ans = INT_MAX;
for(int k = 1; k <= K; k ++) {
int T;
bool can = false;
int cnt1 = 0, cnt2 = 0;
memset(vis, 0, sizeof(vis));
bool flag = true;
int sum = 0;
scanf("%d", &T);
for(int i = 1; i <= T; i ++) {
scanf("%d", &go[i]);
vis[go[i]] ++;
if(i > 1) {
if(dis[go[i]][go[i - 1]] != inf) {
sum += dis[go[i]][go[i - 1]];
}
else flag = false;
}
} printf("Path %d: ", k);
if(!flag)
printf("NA (Not a TS cycle)\n");
else {
int iscycle = 0;
for(int i = 1; i <= N; i ++) {
if(vis[i] == 0)
iscycle = 1;
if(vis[i] == 1) cnt1 ++;
if(vis[i] > 1) cnt2 ++;
} if(iscycle == 1) printf("%d (Not a TS cycle)\n", sum);
else if(cnt2 == 1 && vis[go[1]] == 2) {
can = true;
printf("%d (TS simple cycle)\n", sum);
}
else if(cnt2 >= 1 && vis[go[1]] >= 2) {
can = true;
printf("%d (TS cycle)\n", sum);
}
else if(cnt2 >= 1 && vis[go[1]] < 2)
printf("%d (Not a TS cycle)\n", sum);
else printf("%d (Not a TS cycle)\n", sum); if(can && sum < ans) {
ans = sum;
temp = k;
} } } printf("Shortest Dist(%d) = %d\n", temp, ans);
return 0;
}
被图论支配的上午 暴躁 Be 主 在线编程
一会有牛客的比赛 哭咧咧
PAT 甲级 1150 Travelling Salesman Problem的更多相关文章
- 1150 Travelling Salesman Problem(25 分)
The "travelling salesman problem" asks the following question: "Given a list of citie ...
- 1150 Travelling Salesman Problem
The "travelling salesman problem" asks the following question: "Given a list of citie ...
- PAT A1150 Travelling Salesman Problem (25 分)——图的遍历
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 ...
- 构造 - HDU 5402 Travelling Salesman Problem
Travelling Salesman Problem Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5402 Mean: 现有一 ...
- HDU 5402 Travelling Salesman Problem (构造)(好题)
大致题意:n*m的非负数矩阵,从(1,1) 仅仅能向四面走,一直走到(n,m)为终点.路径的权就是数的和.输出一条权值最大的路径方案 思路:因为这是非负数,要是有负数就是神题了,要是n,m中有一个是奇 ...
- HDOJ 5402 Travelling Salesman Problem 模拟
行数或列数为奇数就能够所有走完. 行数和列数都是偶数,能够选择空出一个(x+y)为奇数的点. 假设要空出一个(x+y)为偶数的点,则必须空出其它(x+y)为奇数的点 Travelling Salesm ...
- HDU 5402 Travelling Salesman Problem (模拟 有规律)(左上角到右下角路径权值最大,输出路径)
Travelling Salesman Problem Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (J ...
- PAT-1150(Travelling Salesman Problem)旅行商问题简化+模拟图+简单回路判断
Travelling Salesman Problem PAT-1150 #include<iostream> #include<cstring> #include<st ...
随机推荐
- mfc 异常机制
异常 抛出异常 捕获异常 一.异常 迄今为止,我们处理程序中的错误一般都是用if语句测试某个表达式,然后处理错误的特定义代码. C++异常机制使用了三个新的关键字 (SEH(结构化异常处理)) try ...
- PyQt5 笔记(05):信号/槽
PyQt 的很多类都内置了信号和槽.下图是 Qt 官方文档对 QThread 类中包含的信号/槽的描述: 一.信号/槽 都是内置的 请看一个最简单的程序: 按钮点击后,窗口关闭 代码: class T ...
- LBP人脸识别的python实现
这几天看了看LBP及其人脸识别的流程,并在网络上搜相应的python代码,有,但代码质量不好,于是自己就重新写了下,对于att_faces数据集的识别率能达到95.0%~99.0%(40种类型,每种随 ...
- iOS开发-通过正则表达式进行各种判断银行卡,车牌号,邮箱地址,QQ,身份证,全字母,仅输入字母或数字同时包含大小写字母和数字,仅能输入中文等
/* * 验证银行卡号是否正确 * 车牌号验证 * 检验邮箱地址是否正确 * 手机号中间四位密文显示 * 判断QQ号是否正确(5-11位) * 判断身份证号是否正确(如末位为字母请用“x” ...
- loj2230 「BJOI2014」大融合
LCT裸题 我LCT学傻了这题明显可以树剖我不会树剖了 本来的siz是Splay上的子树和,并没有什么用. 所以每个点维护虚子树和和子树和 虚子树和即虚边连接的子树和,且只有在access和link操 ...
- springmvc controller转发setViewName时找不到路径的问题以及转发视图时出现找不到样式的问题
注释掉的部分是错误的写法,@RequestMapping 需要将方法放置在要转发的视图所在目录下,不然视图会找不到样式(无法正确的加载css文件), 如果将方法放在了视图所在目录下,那么 setUie ...
- 最具有性价比的语言javascript之介绍篇
虽然最近几年javascript很火.但很多程序员对javascript重视程度不够,所以对javascript的高级应用不甚了解.认为javascript仅仅只是一门脚本语言,作用就是表单验证,网页 ...
- $.post(url,[data],[callback],'json')
$.post(url,[data],[callback],'json')这个写法来做到用post方法传递数据,并取加回json型数据.如果我要取回的数据类型是xml的,就可以写成$.post(url, ...
- 【轮子狂魔】WeChatAPI 开源系统架构详解
如果使用WeChatAPI,它扮演着什么样的角色? 从图中我们可以看到主要分为3个部分: 1.业务系统 2.WeChatAPI: WeChatWebAPI,主要是接收微信服务器请求: WeChatAP ...
- Altium中Logo的导入方法及大小调整
Altium中Logo的导入方法及大小调整 LOGO识别性是企业标志的重要功能之一,特点鲜明.容易辨认,很多客户需要在PCB设计阶段导入LOGO标示归属特性.如果LOGO是CAD图纸,可以直接按照 ...