1150 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), 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
给出一个无向有权图,然后给出k个路径,进行判断,是否是能访问所有城市的简单环,显然需要记录访问了几个城市,以及路径是否通,如果路径不通直接是NA,然后考虑其他的,要形成环,路径最少得有n + 1个点,且首尾要相同,而且路径要访问所有点,如果都满足了,要判断是不是简单环,简单环必须是n + 1个点。
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <set>
#define inf 0x3f3f3f3f
#define MAX
using namespace std;
int mp[][];
int path[];
int n,m,k;
int u,v,w,kk,mint,mind = inf;
int main() {
scanf("%d%d",&n,&m);
for(int i = ;i < m;i ++) {
scanf("%d%d%d",&u,&v,&w);
mp[u][v] = mp[v][u] = w;
}
scanf("%d",&k);
for(int i = ;i <= k;i ++) {
scanf("%d",&kk);
int vis[] = {},c = ,d = ;
for(int j = ;j < kk;j ++) {
scanf("%d",&path[j]);
if(!vis[path[j]]) c ++;
vis[path[j]] ++;
}
for(int j = ;j < kk;j ++) {
if(mp[path[j]][path[j - ]]) {
d += mp[path[j]][path[j - ]];
}
else {
c = -;
break;
}
}
if(c == -) printf("Path %d: NA (Not a TS cycle)\n",i);
else if(kk <= n || c < n || path[] != path[kk - ]) printf("Path %d: %d (Not a TS cycle)\n",i,d);
else {
if(kk == n + ) printf("Path %d: %d (TS simple cycle)\n",i,d);
else printf("Path %d: %d (TS cycle)\n",i,d);
if(mind > d) {
mint = i;
mind = d;
}
}
}
printf("Shortest Dist(%d) = %d",mint,mind);
}
1150 Travelling Salesman Problem(25 分)的更多相关文章
- PAT 甲级 1150 Travelling Salesman Problem
https://pintia.cn/problem-sets/994805342720868352/problems/1038430013544464384 The "travelling ...
- 1150 Travelling Salesman Problem
The "travelling salesman problem" asks the following question: "Given a list of citie ...
- PAT_A1150#Travelling Salesman Problem
Source: PAT A1150 Travelling Salesman Problem (25 分) Description: The "travelling salesman prob ...
- PAT A1150 Travelling Salesman Problem (25 分)——图的遍历
The "travelling salesman problem" asks the following question: "Given a list of citie ...
- HDOJ 5402 Travelling Salesman Problem 模拟
行数或列数为奇数就能够所有走完. 行数和列数都是偶数,能够选择空出一个(x+y)为奇数的点. 假设要空出一个(x+y)为偶数的点,则必须空出其它(x+y)为奇数的点 Travelling Salesm ...
- HDU 5402 Travelling Salesman Problem (构造)(好题)
大致题意:n*m的非负数矩阵,从(1,1) 仅仅能向四面走,一直走到(n,m)为终点.路径的权就是数的和.输出一条权值最大的路径方案 思路:因为这是非负数,要是有负数就是神题了,要是n,m中有一个是奇 ...
- HDU 5402 Travelling Salesman Problem (模拟 有规律)(左上角到右下角路径权值最大,输出路径)
Travelling Salesman Problem Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (J ...
- 构造 - HDU 5402 Travelling Salesman Problem
Travelling Salesman Problem Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5402 Mean: 现有一 ...
- PAT-1150(Travelling Salesman Problem)旅行商问题简化+模拟图+简单回路判断
Travelling Salesman Problem PAT-1150 #include<iostream> #include<cstring> #include<st ...
随机推荐
- JavaScript:学习笔记(1)——在HTML中使用JS
在HTML中使用JavaScript <script>元素 1.直接在网页中嵌入JS代码 说明: 请不要在代码的任何地方出现</script>字符串 这是由于解析嵌入式代码的规 ...
- offsetLeft与style.left的区别
参考:http://www.cnblogs.com/woshilee/articles/1951457.html offsetLeft 获取的是相对于父对象的左边距 left 获取或设置相对于 具有定 ...
- [转]u盘读不出来怎么办大汇总
今天遇到的问题 http://www.upantool.com/jiaocheng/xiufu/2016/9958.html u盘读不出来怎么办大汇总 2016-12-14 21:42 来源: 本站整 ...
- grep的若干用法
查找包含server或者client的行 egrep 'server|client' file-name /usr/xpg4/bin/grep -E 'server|client' file-name ...
- canvas 视频音乐播放器
canvas 视频音乐播放器 var play_nor_img_path = 'images/play_btn_n.png'; //播放按钮 正常时 60x60 px var play_sec_img ...
- HMM代码实现
按照网上的代码,自己敲了一下,改了一点点,理解加深了一下. 还有训练HMM的EM算法没看懂,下次接着看: 参考连接:http://www.cnblogs.com/hanahimi/p/4011765. ...
- mysql一次运行多个SQL文件
在文件 batch.sql 中写下多个SQL文件 source file1.SQLsource file2.SQLsource file3.SQL 然后运行 source batch.sql
- java深入探究11-基础加强
1. ? extends String:String 子类;? super String:String 父类 2.反射->参数化类型表示 ParameteredType:参数化类型表示,就是获得 ...
- 【转】jQuery对象与DOM对象之间的转换方法
刚开始学习jquery,可能一时会分不清楚哪些是jQuery对象,哪些是DOM对象.至于DOM对象不多解释,我们接触的太多了,下面重点介绍一下jQuery,以及两者相互间的转换. 什么是jQuery对 ...
- SpringBoot-新建项目
在开发SpringBoot之前,先下载STS开发工具,当然也可以用myeclipse等工具. STS官方下载地址:https://spring.io/tools/sts 下载安装完成后:File--& ...