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 C​1​​ C​2​​ ... C​n​​

where n is the number of cities in the list, and C​i​​'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

给出一个无向有权图,然后给出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 分)的更多相关文章

  1. PAT 甲级 1150 Travelling Salesman Problem

    https://pintia.cn/problem-sets/994805342720868352/problems/1038430013544464384 The "travelling ...

  2. 1150 Travelling Salesman Problem

    The "travelling salesman problem" asks the following question: "Given a list of citie ...

  3. PAT_A1150#Travelling Salesman Problem

    Source: PAT A1150 Travelling Salesman Problem (25 分) Description: The "travelling salesman prob ...

  4. PAT A1150 Travelling Salesman Problem (25 分)——图的遍历

    The "travelling salesman problem" asks the following question: "Given a list of citie ...

  5. HDOJ 5402 Travelling Salesman Problem 模拟

    行数或列数为奇数就能够所有走完. 行数和列数都是偶数,能够选择空出一个(x+y)为奇数的点. 假设要空出一个(x+y)为偶数的点,则必须空出其它(x+y)为奇数的点 Travelling Salesm ...

  6. HDU 5402 Travelling Salesman Problem (构造)(好题)

    大致题意:n*m的非负数矩阵,从(1,1) 仅仅能向四面走,一直走到(n,m)为终点.路径的权就是数的和.输出一条权值最大的路径方案 思路:因为这是非负数,要是有负数就是神题了,要是n,m中有一个是奇 ...

  7. HDU 5402 Travelling Salesman Problem (模拟 有规律)(左上角到右下角路径权值最大,输出路径)

    Travelling Salesman Problem Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (J ...

  8. 构造 - HDU 5402 Travelling Salesman Problem

    Travelling Salesman Problem Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5402 Mean: 现有一 ...

  9. PAT-1150(Travelling Salesman Problem)旅行商问题简化+模拟图+简单回路判断

    Travelling Salesman Problem PAT-1150 #include<iostream> #include<cstring> #include<st ...

随机推荐

  1. Web安全相关资料

    Asp.net安全架构: http://www.cnblogs.com/luminji/category/381486.html

  2. php异常处理类

    <?php header('content-type:text/html;charset=UTF-8'); // 创建email异常处理类 class emailException extend ...

  3. Ajax在jQuery中的应用---加载异步数据

    Ajax是Asynchronous JavaScript and XML的缩写,其核心是通过XMLHttpRequest对象,以一种异步的方式,向服务器发送数据请求,并通过该对象接收请求返回的数据,从 ...

  4. 财经世界(2)A股B股和H股

    在发行过程中,公司通过章程给不同股份形式赋予不同的权益,使公司股份出现A股.B股等形式.我国上市公司的股票有A股.B股.H股.N股.S股等的区分.这一区分的主要依据股票的上市地点和所面对的投资者而定. ...

  5. Redis集群环境搭建

    Redis集群cluster环境搭建 描述:本章节主要单服务器搭建集群,在一个服务器上启动多个不同端口的redis服务,非真实环境. 真实环境下redis集群会搭建在多个物理服务器上,并非单一的服务器 ...

  6. Android BlueDroid(蓝牙协议栈)

    Android BlueDroid(一):BlueDroid概述 Android BlueDroid(二):BlueDroid蓝牙开启过程init Android BlueDroid(三):BlueD ...

  7. Linux下解压分包文件zip(zip/z01/z02)

    分包压缩的zip文件不能被7z解压,且这种格式是Windows才能创建出来,在Linux下不会以这种方式去压包.下面是在Linux下处理这种文件的做法: 方法一: cat xx.z01 xx.zip ...

  8. Python 列表List的定义及操作

    # 列表概念:有序的可变的元素集合 # 定义 # 直接定义 nums = [1,2,3,4,5] # 通过range函数构造,python2 和python3 版本之间的差异: # python3 用 ...

  9. JMeter学习(七)聚合报告之 90% Line 正确理解

    90% Line 参数正确的含义: 虽然,我的上面理解有一定的道理,显然它是错误的.那看看JMeter 官网是怎么说的? 90% Line - 90% of the samples took no m ...

  10. 找回J2EE 之再学习打卡记录

    由于之前准备专心搞前端,就把一些java知识闲置了很久.导致...现在有点艰难. 所以!我决定,要找回他. 这是篇打卡记录.(每天一小时.监督自己!) Day1(2017-10-8.)