PAT_A1150#Travelling Salesman Problem
Source:
Description:
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 Mlines follow, each describes an edge in the format
City1 City2 Dist
, where the cities are numbered from 1 to N and the distanceDist
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)
whereX
is the index (starting from 1) of that path,TotalDist
its total distance (if this distance does not exist, outputNA
instead), andDescription
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
whereX
is the index of the cycle that is the closest to the solution of a travelling salesman problem, andTotalDist
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
Keys:
Attention:
- 注意检查是否遍历了所有结点
Code:
/*
Data: 2019-08-04 17:16:14
Problem: PAT_A1150#Travelling Salesman Problem
AC: 20:24 题目大意:
给出城市结点列表,及其路径,问遍历所有结点并返回初始结点的最短路径
现在给出一系列路径,找出能够遍历所有结点的最短回路
输入:
第一行给出,结点数2<N<=200,边数M
接下来M行, City1 City2 Distance, 1<=City<=N, 0<Dis<=100;
接下来一行,给出查询数K
接下来K行,首先给出城市数目N,接着依次给出N个城市
输出:
Path 1~K: 总距离/NA(不可达)
描述:
简单回路,TS simple cycle
非简单回路,TS cycle
非回路,Not a TS cycle(未回到起点或未遍历所有结点)
最后一行,输出所给回路中最短的一条
*/
#include<cstdio>
#include<set>
#include<algorithm>
using namespace std;
const int M=1e3,INF=1e9;
int grap[M][M],path[M]; int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE fill(grap[],grap[]+M*M,INF);
int n,m,k,v1,v2;
scanf("%d%d", &n,&m);
for(int i=; i<m; i++)
{
scanf("%d%d", &v1,&v2);
scanf("%d", &grap[v1][v2]);
grap[v2][v1]=grap[v1][v2];
}
scanf("%d", &m);
int optJ,optValue=INF;
for(int j=; j<=m; j++)
{
scanf("%d", &k);
set<int> ver;
for(int i=; i<k; i++)
{
scanf("%d", &path[i]);
ver.insert(path[i]);
}
int reach=,value=;
for(int i=; i<k-; i++){
if(grap[path[i]][path[i+]] != INF)
value += grap[path[i]][path[i+]];
else
k=;
}
if(k==)
printf("Path %d: NA (Not a TS cycle)\n", j);
else
{
if(path[]!=path[k-] || ver.size()<n)
printf("Path %d: %d (Not a TS cycle)\n",j,value);
else
{
if(value < optValue)
{
optValue = value;
optJ = j;
}
if(k==n+)
printf("Path %d: %d (TS simple cycle)\n",j,value);
else
printf("Path %d: %d (TS cycle)\n",j,value);
}
}
}
printf("Shortest Dist(%d) = %d", optJ,optValue);
}
PAT_A1150#Travelling Salesman Problem的更多相关文章
- PAT A1150 Travelling Salesman Problem (25 分)——图的遍历
The "travelling salesman problem" asks the following question: "Given a list of citie ...
- PAT 甲级 1150 Travelling Salesman Problem
https://pintia.cn/problem-sets/994805342720868352/problems/1038430013544464384 The "travelling ...
- 构造 - HDU 5402 Travelling Salesman Problem
Travelling Salesman Problem Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5402 Mean: 现有一 ...
- 1150 Travelling Salesman Problem(25 分)
The "travelling salesman problem" asks the following question: "Given a list of citie ...
- 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 ...
- 1150 Travelling Salesman Problem
The "travelling salesman problem" asks the following question: "Given a list of citie ...
- PAT-1150(Travelling Salesman Problem)旅行商问题简化+模拟图+简单回路判断
Travelling Salesman Problem PAT-1150 #include<iostream> #include<cstring> #include<st ...
随机推荐
- H - Seek the Name, Seek the Fame
The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the l ...
- BlockQueue中ArrayBlockingQueue和LinkedBlockingQueue比较
LinkedBlockingQueue是BlockingQueue的一种使用Link List的实现,它对头和尾(取和添加操作)采用两把不同的锁,相对于ArrayBlockingQueue提高了吞吐量 ...
- log_archive_dest_1设置报错
DG搭建完之后,又报错: Tue Dec 22 16:24:33 2015 Errors in file /u01/app/oracle/admin/orcl/bdump/orcl_arc1_2994 ...
- 权限问题导致无法删除ftp文件
首先吐槽一下,使用新版编辑器,发了两遍愣是time out,果断放弃 这个文章也是一件小事,大致说一下: 有一个java操作ftp文件的程序,运行删除时,总是返回false,也没有报错.開始考虑是没有 ...
- php require和include差别
require 的用法如 require("MyRequireFile.php"); .这个函数通常放在 PHP 程序的最前面.PHP 程序在运行前,就会先读入 require 所 ...
- GDUT Krito的讨伐(bfs&&优先队列)
题意 Description Krito最终干掉了99层的boss,来到了第100层. 第100层能够表示成一颗树.这棵树有n个节点(编号从0到n-1),树上每个节点可能有非常多仅仅怪物. Krito ...
- 【Ubuntu】基本操作 (条目=11)
定义 NAME 为要操作的对象名 定义 DIR 为文件所在的绝对路径 所有操作默认在普通用户下进行 所有软件包默认是指Debian包(deb包) 1.查看进程 top 2.强制结束进程 PID由top ...
- C++ Web 编程(菜鸟教程)
C++ Web 编程(菜鸟教程) C++ Web 编程 什么是 CGI? 公共网关接口(CGI),是一套标准,定义了信息是如何在 Web 服务器和客户端脚本之间进行交换的. CGI 规范目前是由 NC ...
- B1922 [Sdoi2010]大陆争霸 最短路
我一直都不会dij的堆优化,今天搞了一下...就是先弄一个优先队列,存每个点的数据,然后这个题就加了一点不一样的东西,每次的最短路算两次,一次是自己的最短路,另一次是机关的最短路,两者取最大值才是该点 ...
- iOS手势识别
一.手势识别与触摸事件 1.如果想监听一个view上面的触摸事件,可选的做法是: (1)自定义一个view (2)实现view的touches方法,在方法内部实现具体处理代码 2.通过touches方 ...