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
Solution:
  这道题是水题,就是一个简单的计算路程和判断的过程,不用大脑
 #include <iostream>
#include <vector>
using namespace std;
int n, m, k, x;
int dis[][] = { };
int main()
{
cin >> n >> m;
for (int i = ; i < m; ++i)
{
int a, b, c;
cin >> a >> b >> c;
dis[a][b] = dis[b][a] = c;
}
cin >> k;
int minDis = INT32_MAX, minIdex = ;
for (int t = ; t <= k; ++t)
{
int calDis = ;
bool isCycle = true;
vector<bool>visit(n + , true);
cin >> x;
vector<int>path(x);
for (int i = ; i < x; ++i)
{
cin >> path[i];
visit[path[i]] = false;
}
for (int i = ; i < x; ++i)
{
if (dis[path[i - ]][path[i]] > )
calDis += dis[path[i - ]][path[i]];
else//此路不通
{
isCycle = false;
calDis = -;//没有结果。输出为NA
break;
}
}
if (path[] != path[x - ])isCycle = false;//不是回路
for (int i = ; i <= n && isCycle; ++i)
if (visit[i] == true)
isCycle = false;
if(calDis<)
printf("Path %d: NA (Not a TS cycle)\n", t);
else if (!isCycle)
printf("Path %d: %d (Not a TS cycle)\n", t, calDis);
else if(x==n+)
printf("Path %d: %d (TS simple cycle)\n", t, calDis);
else
printf("Path %d: %d (TS cycle)\n", t, calDis);
if (isCycle && minDis > calDis)
{
minDis = calDis;
minIdex = t;
}
}
printf("Shortest Dist(%d) = %d", minIdex, minDis);
return ;
}
 

PAT甲级——A1150 TravellingSalesmanProblem【25】的更多相关文章

  1. PAT 甲级 1010 Radix (25)(25 分)进制匹配(听说要用二分,历经坎坷,终于AC)

    1010 Radix (25)(25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 ...

  2. PAT 甲级1003 Emergency (25)(25 分)(Dikjstra,也可以自己到自己!)

    As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...

  3. pat 甲级 1010. Radix (25)

    1010. Radix (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a pair of ...

  4. pat 甲级 1078. Hashing (25)

    1078. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of t ...

  5. PAT 甲级 1003. Emergency (25)

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  6. PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)

    1078 Hashing (25 分)   The task of this problem is simple: insert a sequence of distinct positive int ...

  7. PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)

    1070 Mooncake (25 分)   Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...

  8. PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)

    1032 Sharing (25 分)   To store English words, one method is to use linked lists and store a word let ...

  9. PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*

    1029 Median (25 分)   Given an increasing sequence S of N integers, the median is the number at the m ...

随机推荐

  1. UVA12589_Learning Vector

    大致题意: 有n个向量要你选k个,把这k个向量连起来,画出来的与x轴围成的面积最大 思路: 这个是斜率dp,让斜率大的排在前面,记忆化搜索的时候要加入一个当前高的信息,因为这个向量形成面积不仅和斜率有 ...

  2. 转 LoadRunner错误处理函数

    在脚本的Run-time Settings中,可以设置在脚本运行过程中发生错误的处理方式.进入到Run-time Settings中,切换到Miscellaneous标签页,可以看到Error Han ...

  3. JVM系列(二) — Java垃圾收集介绍

    这篇文章主要从以下几个方面介绍垃圾收集的相关知识 一.判断对象是否已死 二.主流垃圾收集算法 三.内存分配与回收策略 本章节主要从以下几个思考点着手介绍垃圾回收的相关知识:哪些内存需要回收?什么时候回 ...

  4. java多态的实现机制

    Java提供了编译时多态和运行时多态两种多态机制.前者是通过方法重载实现的,后者是通过方法的覆盖实现的. 在方法覆盖中,子类可以覆盖父类的方法,因此同类的方法会在父类与子类中有着不同的表现形式. 在J ...

  5. python字符串有多少字节

    是否有一些函数可以告诉我字符串在内存中占用多少字节? 我需要设置套接字缓冲区的大小,以便一次传输整个字符串. 解决方案 import sys sys.getsizeof(s) # getsizeof( ...

  6. 数的直径(两次DFS)

    题目传送门 桃花 题目描述 桃花一簇开无主,可爱深红映浅红.                                         ——<题百叶桃花> 桃花长在桃树上,树的每个节 ...

  7. ZOJ 3681E - Cup 2(记忆化dfs)不好读

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/opm777/article/details/25726221 E - Cup 2 Time Limi ...

  8. python基础篇(文件操作)

    Python基础篇(文件操作) 一.初始文件操作 使用python来读写文件是非常简单的操作. 我们使用open()函数来打开一个文件, 获取到文件句柄. 然后通过文件句柄就可以进行各种各样的操作了. ...

  9. 图片查看器(类似于QQ,另外又加了JARA的下方的图片缩略导航图)

    源码地址:https://gitee.com/yolanda624/coffer/tree/master/src/components/a-photo-view

  10. K8S操作

    一.K8Spods操作 kubectl delete all --all //删除 所有pods