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. pip安装第三方库报错Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None))...

    pip安装第三方库时报错Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None))...,详细报错见下 ...

  2. selenium,webdriver,xpath获取全国各地的邮编

    代码要多敲 注释要清晰 其中区号没有拿取出来 看到的朋友可以作为练习 ,有好的方法可以在下面留言 from selenium import webdriver from lxml import etr ...

  3. ss证书问题

    #SSL--校验网站证书 #一.什么是SSL证书 from urllib import request #ssl免验证 import ssl ssl._create_default_https_con ...

  4. 区间节点的lca

    题目hdu5266 分析:多节点的LCA就是dfs序中最大最小两个节点的LCA.所以只要每次维持给出节点的dfs序的最大最小,然后就是两点的LCA 代码: rmq的st+lca的倍增 #include ...

  5. 排列组合( Lindström–Gessel–Viennot lemma 定理)

    链接:https://www.nowcoder.com/acm/contest/139/A来源:牛客网 Monotonic Matrix 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ ...

  6. hive数据库导入与导出

    原文连接:https://www.cnblogs.com/654wangzai321/p/9970321.html 把Hive表数据导入到本地 table->local file insert ...

  7. unity3D中的Input按键方法检测

    一,按键的按下抬起等识别方法 void Update () { ; ; if (Input.GetKeyDown (KeyCode.A)) { Debug.Log("A按下一次") ...

  8. 关于清除浮动的n中方式

    我们在对页面进行布局的时候经常会用到浮动布局,浮动布局能够很好的实现我们想要的布局效果,同时兼容方面也是很好的,但是当我们在用左右浮动进行页面布局的时候,由于元素浮动脱了了文档流导致浮动元素的父级高度 ...

  9. [Js代码风格]浅析模块模式

    1.实例解释模块模式 简明扼要的说,经典的模块模式指的定义一个立即执行的匿名函数.在函数中定义私有函数和私有变量并且返回一个包含公共变量和公共函数作为属性和方法的匿名对象. var classicMo ...

  10. 如何限制只有某些IP才能使用Tomcat Manager

    只有指定的主机或IP地址才可以访问部署在Tomcat下的应用.Tomcat提供了两个参数供你配置:RemoteHostValve 和RemoteAddrValve,前者用于限制主机名,后者用于限制IP ...