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 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
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学习笔记之——类与对象
1.参数的传递方式 1)值传递 2)引用传递 2.类和对象: (1)类的定义: public class 类名{ 类型 属性1: 类型 属性2: ……… public 返回值类型 方法名1(形参){ ...
- java_二进制的前导的零
题目内容: 计算机内部用二进制来表达所有的值.一个十进制的数字,比如18,在一个32位的计算机内部被表达为00000000000000000000000000011000.可以看到,从左边数过来,在第 ...
- 集合类--最详细的面试宝典--看这篇就够用了(java 1.8)
看了一个星期源码,搜索上百篇博文,终于总结出了集合类的所有基础知识点,学集合,看这篇就够用了!!! 篇幅有点长, 如果你能全部理解,java最重要的集合就不怕了,秒过面试!!!(本篇素材来自网络,如有 ...
- python之常用模块补充
namedtuple 命名元组 ->类似创建了一个类 from collections import namedtuple p = namedtuple("point", [ ...
- vue-cli脚手架之webpack.base.conf.js
webpack相关的重要配置文件将在这一节给出.webpack水很深啊^o^,在此先弄清楚原配文件内容的含义,后续可以自己根据实际情况配置. webpack.base.conf.js:配置vue开发环 ...
- TUM数据集rgbd_benchmark工具的使用方法
# 在学习视觉slam过程中,需要对数据集合进行预处理和对slam或者跟踪结果进行评价,TUM提供一组这样的工具,为了自己以后方便查找,于是把它记录下来 一.RGBD_Benchmark工具下载链接: ...
- ScrollView与ListView的事件冲突
布局文件 当ListView嵌套在ScrollView中时,会发生冲突,导致ListView控件的拉动效果消失‘ 解决办法: 重写ListView的onTouchEvent(),并在返回前调用getP ...
- springcloud 入门 8 (config配置中心)
Spring Cloud Config: 配置中心为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件,它就是Spring Cloud Config. 在分布式系统中,由于服务数量巨多, ...
- 数据层的多租户浅谈(SAAS多租户数据库设计)
在上一篇“浅析多租户在 Java 平台和某些 PaaS 上的实现”中我们谈到了应用层面的多租户架构,涉及到 PaaS.JVM.OS 等,与之相应的是数据层也有多租户的支持. 数据层的多租户综述 多租户 ...
- C#微信公众号开发——获取access_token
access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token.正常情况下access_token有效期为7200秒(两个小时),微信获取access_token接 ...