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 distanceDistis 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)whereXis the index (starting from 1) of that path,TotalDistits total distance (if this distance does not exist, outputNAinstead), andDescriptionis 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) = TotalDistwhereXis the index of the cycle that is the closest to the solution of a travelling salesman problem, andTotalDistis 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 ...
随机推荐
- Spring MVC-集成(Integration)-生成XML示例(转载实践)
以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_xml.htm 说明:示例基于Spring MVC 4.1.6. 以下示例说明如何 ...
- UVA 10891 区间DP+博弈思想
很明显带有博弈的味道.让A-B最大,由于双方都采用最佳策略,在博弈中有一个要求时,让一方的值尽量大.而且由于是序列,所以很容易想到状态dp[i][j],表示序列从i到j.结合博弈中的思想,表示初始状态 ...
- 游戏人生(一),我的lua之旅:那些坑爹的CCBReaderLoad
首先,我们说说这个CCBReaderLoad. 这个脚本是cocos2dx自带的一个lua+cocosbuilder 的工具,详细功能呐,往下看. 先来看下我遇到的一个问题: ----美工给了我一个. ...
- android 自己定义ViewGroup实现可记载并呈现选择的ListView
转载请注明出处:王亟亟的大牛之路 之前也做过一些用TextView之类的记录ListView选项的东西.可是总认为好难看.发现个不错的实现就贴给大家. 项目文件夹 执行效果: 自己定义视图: @Tar ...
- Android eclipse导入项目后出现Unable to resolve target 'android-17'解决方法
eclipse导入项目后出现Unable to resolve target 'android-17'解决方法.在最后附带还有一种编译逻辑不成功情况解决方法. 一.问题情况 二.解决的方法 1.改动项 ...
- 2017 Multi-University Training Contest - Team 2&&hdu 6047 Maximum Sequence
Maximum Sequence Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- .NET页面事件执行顺序
摘自:http://www.cnblogs.com/kenkofox/archive/2011/03/18/1987998.html和http://blog.csdn.net/yiruoyun/art ...
- PCB 生产周期计算逻辑与代码实现
PCB生产周期计算逻辑: 代码实现: 调用代码: getWeek(DateTime.Now.Date, ); 周期计算逻辑: /// <summary> /// 获取周期 /// < ...
- PCB MS SQL 小写转大写
由于SQL Server允许为小写进入 ,导致数据库中存在小写,在数据集成到MES或ERP时报错,Oracle要求大写导致, 需转换为大写,可通过以下语句,查询所有小写数据,再更新.
- 【NOIP2018】 游记
All ended? [day 0] 一点感觉没有,不过翘掉了早上的课(当然还有前三周的课),然后刚想睡一会儿,就被通知要上车了/难受 在车上玩了一会儿早上下的Super Mario(主要是早上刷了一 ...