PAT A1150 Travelling Salesman Problem (25 分)——图的遍历
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 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) = 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 <stdio.h>
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
#include <set>
using namespace std;
int n,m,k;
int dis[][];
int path[],vis[];
int main(){
scanf("%d %d",&n,&m);
for(int i=;i<m;i++){
int c1,c2,d;
scanf("%d %d %d",&c1,&c2,&d);
dis[c1][c2]=d;
dis[c2][c1]=d;
}
scanf("%d",&k);
int min=,mini=;
for(int i=;i<=k;i++){
int flag=;
int total=;
int nn;
fill(vis,vis+,);
scanf("%d",&nn);
for(int j=;j<nn;j++){
scanf("%d",&path[j]);
vis[path[j]]++;
}
for(int j=;j<=n;j++){
if(vis[j]==) flag=;
}
for(int j=;j<nn;j++){
if(dis[path[j]][path[j-]]==){
total=-;
flag=;
break;
}
else{
total+=dis[path[j]][path[j-]];
}
}
printf("Path %d: ",i);
if(total==-) printf("NA ");
else printf("%d ",total);
if(flag== || path[]!=path[nn-]) printf("(Not a TS cycle)\n");
else{
if(total<min){
min=total;
mini=i;
}
if(nn==n+) printf("(TS simple cycle)\n");
else printf("(TS cycle)\n");
}
}
printf("Shortest Dist(%d) = %d\n",mini,min);
}
注意点:看到题目一直不知道怎么做,这似乎是一个从一个点出发,找到最短的回到原点的路径,又不是最小生成树,也不是全源最短路径。没有一个已知算法适合做这个。没办法只好看大神思路,看了以后发现什么鬼,
TS simple cycle 居然是判断给定路径是不是都遍历了所有城市,并且起点和终点相同,只有起点重复了一次,只是看是否是最简单的环,并不管路径长度
TS cycle 就是判断给定路径是不是遍历了所有城市,但不是最简单的环,即有城市访问太多遍了
Not a TS cycle 是看给定路径有没有到所有城市,起点终点一不一样,有没有路走不通的
PAT A1150 Travelling Salesman Problem (25 分)——图的遍历的更多相关文章
- PAT A1121 Damn Single (25 分)——set遍历
"Damn Single (单身狗)" is the Chinese nickname for someone who is being single. You are suppo ...
- PAT_A1150#Travelling Salesman Problem
Source: PAT A1150 Travelling Salesman Problem (25 分) Description: The "travelling salesman prob ...
- 1150 Travelling Salesman Problem(25 分)
The "travelling salesman problem" asks the following question: "Given a list of citie ...
- PAT A1142 Maximal Clique (25 分)——图
A clique is a subset of vertices of an undirected graph such that every two distinct vertices in the ...
- PAT A1122 Hamiltonian Cycle (25 分)——图遍历
The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...
- PAT 甲级 1150 Travelling Salesman Problem
https://pintia.cn/problem-sets/994805342720868352/problems/1038430013544464384 The "travelling ...
- PAT-1150(Travelling Salesman Problem)旅行商问题简化+模拟图+简单回路判断
Travelling Salesman Problem PAT-1150 #include<iostream> #include<cstring> #include<st ...
- 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 ...
随机推荐
- Java-关于类
java-关于类-成员初始化问题 成员初始化出现错误“ - Syntax error on token ";", , expected” java类不允许对成员进行操作,但可 ...
- 有道云笔记链接——JAVA面向对象的学习
http://note.youdao.com/noteshare?id=cf39a0e493a6b3c7ad5d22204a7e7843
- 【Java深入研究】8、Java中Unsafe类详解
java不能直接访问操作系统底层,而是通过本地方法来访问.Unsafe类提供了硬件级别的原子操作,主要提供了以下功能: 1.通过Unsafe类可以分配内存,可以释放内存: 类中提供的3个本地方法all ...
- Maven+Mybatis一些简单例子
一.创建maven工程 把依赖的包写在pom.xml中.保存后,工程会有错,需要在工程上右键选择“Maven-->Update Project” pom.xml内容为 <project x ...
- 关于Ajax的get与post浅分析,同步请求与异步请求,跨域请求;
Ajax局部异步刷新全称ASynchronous JavaScript And XML.使用Javascript代码获取服务器的数据,Ajax当中有两个请求方法,一个是get方法,一个是post请求方 ...
- agc023C - Painting Machines(组合数)
题意 题目链接 有\(n\)个位置,每次你需要以\(1 \sim n-1\)的一个排列的顺序去染每一个颜色,第\(i\)个数可以把\(i\)和\(i+1\)位置染成黑色.一个排列的价值为最早把所有位置 ...
- 洛谷P2447 [SDOI2010]外星千足虫(异或方程组)
题意 题目链接 Sol 异或高斯消元的板子题. bitset优化一下,复杂度\(O(\frac{nm}{32})\) 找最优解可以考虑高斯消元的过程,因为异或的特殊性质,每次向下找的时候找到第一个1然 ...
- Sql Server 向上取整、向下取整、四舍五入取整
==================================================== [四舍五入取整截取] select round(55.56,0) ============== ...
- Clumsy 利用无线网卡结合Clumsy软件模拟弱网络测试
利用无线网卡结合Clumsy软件模拟弱网络测试 by:授客 QQ:1033553122 实践环境 Clumsy 0.2 下载地址:http://jagt.github.io/clumsy/downlo ...
- 《Inside C#》笔记(七) Attribute
Attribute特性可以说是具有开创新的意义,因为一般的语言在被设计出来后,它所具有的能力就已经固定了.而借助Attribute特性,我们可以为C#已有的类型附加信息,既可以在编程时(design- ...