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. .NET/VB.NET: solving the error “The system cannot find the file specified.” “\Temp\.NETFramework,Version=v4.0.AssemblyAttributes.vb”

    Source Link Bumped into this error a while ago in Visual Studio 2010: Kind Error Number 80 Descripti ...

  2. 重定向和转向的写法,重定向以post方式提交

    重转向保留跳转过来的Referer,路径不会变1 request.getRequestDispatcher("/eventweb/index.sp?loginId=" + logi ...

  3. FastReport.net 使用 WebForm 实现打印 最简单版

    1.安装demo 2.设计模版 设计器 -->report-->添加数据源-->添加sql查询->起名字(车信息)下一步-->填写sql语句(select top 1 * ...

  4. Java接口自动化测试实战笔记

    综述 代码管理工具Git 测试框架 TestNG 测试报告 Mock 接口框架 HTTP 协议接口 测试框架 HttpClient SprintBoot 自动化测试开发 数据持久层框架 MyBatis ...

  5. Java关于线程池的使用

    一.四种线程池创建的方式 Java通过Executors提供四种线程池,分别为: newCachedThreadPool 创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回 ...

  6. jackson 问题定位

    问题背景: 云计算Pass平台版本升级,导致引用的jackson的包直接由1.*升级为2.* .在版本1.*中对于字段名与实际json不符的直接忽略了,而在2.*中则会报错.诸如此类,有较大差异,需要 ...

  7. jQuery遍历之向下遍历

    html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <ti ...

  8. Foobar 2000增加APE播放支持的方法

    这里说明一下APE,它是一种常用的无损音乐的存储格式,通常会有将原始音乐光盘数字化后存储的APE文件搭配一个CUE文件使用.这个APE存储了音乐的原始数据,而CUE文件则是一个索引文件,用来标记音乐光 ...

  9. HashMap高并发下存在的问题

    原文链接:https://blog.csdn.net/bjwfm2011/article/details/81076736 1.什么是HashMap? HashMap底层原理 HashMap是存储键值 ...

  10. css3 渐变色兼容移动端

    .group_1 background #1a78f3 // 兼容不显示渐变色的浏览器 background: linear-gradient(180deg, #1a78f3 , #fff); bac ...