当前位置:网站首页>Pat class a 1150 traveling salesman problem
Pat class a 1150 traveling salesman problem
2022-07-29 07:31:00 【Clavier sonata 】
“ Travel agent problem ” It's such a question :“ Give a list of cities and the distance between each pair of cities , What is the shortest way to visit each city and return to the original city ?”
This is one of combinatorial optimization NP problem , It is very important in operations research and theoretical computer science .
In this question , Please find the path closest to the solution of the traveling salesman problem from the given path list .
Input format
The first line contains two integers N and M, Respectively represent the number of cities and Undirected graph The number of middle edges .Next M That's ok , Every line is marked with City1 City2 Dist Format describes an edge , The city number is from 1 To N,Dist Positive and no more than 100.
The next line contains an integer K, Indicates the number of given paths .
Next K Line description path , The format is :
n C1 C2 … Cn
n Indicates the number of cities that a given path passes through ,Ci Is the number of the city in the path .Output format
For each path , Output in one line Path X: TotalDist (Description).among X Is the path number ( from 1 Start ),TotalDist Indicates the total distance of the path ( If the distance does not exist , The output NA),Description Is one of the following :
TS simple cycle, If this is a simple loop to visit every city .
TS cycle, If this is a loop to visit every city , But it's not a simple circuit .
Not a TS cycle, If this is not a circuit that visits every city .
The last line , Output Shortest Dist(X) = TotalDist,X It is the circuit number closest to the solution of the traveling salesman problem ,TotalDist Is its total distance .There is guaranteed to be a unique solution .
Data range
2<N≤200,
N−1≤M≤N(N−1)2,
1≤K≤1000,
1≤n≤300
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
My solution :
#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;
}
边栏推荐
- Reflect reflect
- Clock tree synthesis (I)
- Levelfilter introduction
- 受欢迎的牛 G
- Other basic monitoring items of ZABBIX
- 【Unity实战100例】Unity万能答题系统之单选多选判断题全部通用
- Gin routing, parameters, output
- How to establish EDI connection with Scania in Scania?
- JS day 4 process control (if statement and switch statement)
- 新生代公链再攻「不可能三角」
猜你喜欢
Leetcode buckle classic problem -- 4. Find the median of two positively ordered arrays
2-unified return class dto object
Docker最新超详细教程——Docker创建运行MySQL并挂载
WPF simple login page completion case
分析25个主要DeFi协议的路线图 预见DeFi未来的七大趋势
Meeting notice of OA project (Query & whether to attend the meeting & feedback details)
如何与斯堪尼亚SCANIA建立EDI连接?
Scala higher order (IX): pattern matching in Scala
在线问题反馈模块实战(十七):实现excel模板在线下载功能
A long article --- in-depth understanding of synchronized
随机推荐
Does Flink support sqlserver databases? Get the changes of SQLSERVER database
【暑期每日一题】洛谷 P7760 [COCI2016-2017#5] Tuna
【暑期每日一题】洛谷 P4413 [COCI2006-2007#2] R2
基于高阶无六环的LDPC最小和译码matlab仿真
Gin Middleware
Android面试题 | 怎么写一个又好又快的日志库?
Job 7.28 file IO and standard IO
新生代公链再攻「不可能三角」
LevelFilter简介说明
08 dynamic programming
1 - background project construction
OA项目之会议通知(查询&是否参会&反馈详情)
Full process flow of CMOS chip manufacturing
UPC 小C的王者峡谷
halcon的安装以及在vs2017中测试,vs2017中dll的配置
QT连接两个qslite数据库报错QSqlQuery::exec: database not open
09 bloom filter
[summer daily question] Luogu p6461 [coci2006-2007 5] trik
[daily question in summer] Luogu p6408 [coci2008-2009 3] pet
What is the function of fileappender in logback?