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. ucenter 整合同步登录的内部实现原理

    1.用户登录discuz,通过logging.php文件中的函数uc_user_login对post过来的数据进行验证,也就是对username和password进行验证. 2.如果验证成功,将调用位 ...

  2. webstorm启动vue项目配置

    使用命令窗口运行 1. npm run mock 2.npm run dev 每次都打开命令窗口比较麻烦,可以在webstorm内进行配置,从webstorm内启动 选中run下面的edit conf ...

  3. GET和POST的数据传递到底有何区别?

    1. GET和POST与数据如何传递没有关系 GET和POST是由HTTP协议定义的.在HTTP协议中,Method和Data(URL, Body, Header)是正交的两个概念,也就是说,使用哪个 ...

  4. 有根树的表达 Aizu - ALDS1_7_A: Rooted Trees

    有根树的表达 题目:Rooted Trees Aizu - ALDS1_7_A  A graph G = (V, E) is a data structure where V is a finite ...

  5. 使用UI Automation实现自动化测试--5-7

    使用UI Automation实现自动化测试--5 (Winfrom和WPF中弹出和关闭对话框的不同处理方式) 在使用UI Automation对Winform和WPF的程序测试中发现有一些不同的地方 ...

  6. 两种图片延迟加载的方法总结jquery.scrollLoading.js与jquery.lazyload.js---转载

    jquery.scrollLoading方法 html <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml& ...

  7. JS的部分部分疑问和小结

    2015/9/1 1.在字符串中没有可以所需要查找的"X"的时候,返回的值  java:lastIndexof -1  js: IndexOf undefined... 2015/ ...

  8. 项目使用gulp的配置编译sass笔记

    Node环境 通过 node.js 网站下载了安装包进行安装 node.js, npm也会一起安装 node --version # 查看node.js版本 npm --version #查看npm版 ...

  9. 1、selenium 8大元素定位方式

    元素定位方式: id name css class_name tag_name partial_link link_text : driver. find_element_by_link_text(& ...

  10. git注意事项

    1,在github中新建空的工程,第一次提交代码的时候 使用命令  $ git push -u origin master -f 后面就直接push就行了