当前位置:网站首页>PAT甲级 1150 旅行商问题
PAT甲级 1150 旅行商问题
2022-07-29 07:01:00 【键盘奏鸣曲】
“旅行商问题”是这样一个问题:“给出一个城市列表以及每对城市之间的距离,访问每个城市并返回原城市的最短路线是什么?”
这是组合优化中的一个 NP 难题,在运筹学和理论计算机科学中十分重要。
在此问题中,请你从给定的路径列表中找到最接近旅行商问题的解的路径。
输入格式
第一行包含两个整数 N 和 M,分别表示城市数量以及 无向图 中边的数量。接下来 M 行,每行以 City1 City2 Dist 格式描述一条边,其中城市编号从 1 到 N,Dist 为正且不超过 100。
再一行包含一个整数 K,表示给定路径的数量。
接下来 K 行描述路径,格式为:
n C1 C2 … Cn
n 表示给定路径经过的城市的数目,Ci 是路径中经过的城市的编号。输出格式
对于每个路径,在一行中输出 Path X: TotalDist (Description)。其中 X 是路径编号(从 1 开始),TotalDist 表示路径总距离(如果距离不存在,则输出 NA),Description 是下列中的一项:
TS simple cycle,如果这是一个访问每个城市的简单回路。
TS cycle,如果这是一个访问每个城市的回路,但不是简单回路。
Not a TS cycle,如果这不是一个访问了每个城市的回路。
最后一行,输出 Shortest Dist(X) = TotalDist,X 是最接近旅行商问题解决方案的回路编号,TotalDist 是其总距离。保证有唯一解。
数据范围
2<N≤200,
N−1≤M≤N(N−1)2,
1≤K≤1000,
1≤n≤300
输入样例:
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
输出样例:
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;
const int N = 210, INF = 0x3f3f3f3f;
int n, m, k;
int d[N][N], vi[310];
bool st[N];
int main(){
int min_dist = INF;
int min_id;
cin >> n >> m;
memset(d, 0x3f, sizeof d);
for(int i = 0; i < m; i ++ ){
int a, b, c;
cin >> a >> b >> c;
d[a][b] = d[b][a] = c;
}
cin >> k;
for(int T = 1; T <= k; T ++ ){
int sum = 0;
bool success = true;
int v;
cin >> v;
memset(st, 0, sizeof st);
for(int i = 1; i <= v; i ++ ) cin >> vi[i];
for(int i = 1; i + 1 <= v; i ++ ){
int a = vi[i], b = vi[i + 1];
if(d[a][b] == INF){
sum = -1;
success = false;
break;
}
else{
st[a] = true;
sum += d[a][b];
}
}
for(int i = 1; i <= n; i ++ ){
if(!st[i]){
success = false;
break;
}
}
if (vi[1] != vi[v]) success = false;
if(sum == -1){
printf("Path %d: NA (Not a TS cycle)\n", T);
}
else{
if(!success){
printf("Path %d: %d (Not a TS cycle)\n", T, sum);
}
else{
if(v == n + 1) printf("Path %d: %d (TS simple cycle)\n", T, sum);
else printf("Path %d: %d (TS cycle)\n", T, sum);
if(sum < min_dist){
min_dist = sum;
min_id = T;
}
}
}
}
printf("Shortest Dist(%d) = %d\n", min_id, min_dist);
return 0;
}
边栏推荐
- Remote invocation of microservices
- Scala 高阶(九):Scala中的模式匹配
- Excel文件读写(创建与解析)
- I'd like to ask, my flick job writes data in the way of upsert Kafka, but I'm more careful in MySQL
- Zabbix 其他基础监控项
- vue-router路由缓存
- Kubernetes (五) ---------部署 Kubernetes Dashboard
- Fillder use
- 3-global exception handling
- Homebrew brew update doesn't respond for a long time (or stuck in updating homebrew...)
猜你喜欢
Kubernetes (五) ---------部署 Kubernetes Dashboard
QT basic day 2 (2) QT basic components: button class, layout class, output class, input class, container and other individual examples
Spingboot integrates the quartz framework to realize dynamic scheduled tasks (support real-time addition, deletion, modification and query tasks)
[OpenGL] use of shaders
CMOS芯片制造全工艺流程
Explanation of suffix automata (SAM) + Luogu p3804 [template] suffix automata (SAM)
我,28岁,测试员,10月无情被辞:想给还在学测试 的人提个醒......
一篇长文---深入理解synchronized
leetcode力扣经典问题——4.寻找两个正序数组的中位数
gcc/g++的使用
随机推荐
Gin routing, parameters, output
MySQL 使用客户端以及SELECT 方式查看 BLOB 类型字段内容总结
Vmware16 create virtual machine: win11 cannot be installed
力扣(LeetCode)209. 长度最小的子数组(2022.07.28)
logback 中FileAppender具有什么功能呢?
Remote invocation of microservices
Unity sends a post request to the golang server for parsing and returning
log4j Layout简介说明
logback日志级别简介说明
Summer summary (II)
logback简介及引入方法
路由中的生命周期钩子 - activated与deactivated
MySQL----多表查询
WPF simple login page completion case
2-统一返回类DTO对象
【Unity实战100例】Unity万能答题系统之单选多选判断题全部通用
接口测试实战项目03:执行测试用例
js第四天流程控制(if语句和switch语句)
How does MySQL convert rows to columns?
Scala 高阶(九):Scala中的模式匹配