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. openstack ha 部署

    一.控制节点架构如下图: 二.初始化环境: 1.配置IP地址: 1.节点1:ip addr add dev eth0 192.168.142.110/24 echo 'ip addr add dev ...

  2. iOS代码瘦身实践

    1 分析当前ipa的组成 一般一个ipa会包含: 1) 资源文件 本地文件:数据.配置.数据库等等 字体文件 图片资源 2)  源代码 通过生成linkmap文件,分析源代码生成的编译文件的大小.在B ...

  3. 添加code到github上

    第一步:github上新建远程仓库 1. 在 https://github.com/  注册账号 2. new 一个新仓库 (1) 点击加号下的`New repository` (2)在Reposit ...

  4. CSS实现三角形图标的原理!!!!今天总算弄懂了。

    网页中经常有一种三角形的图标,鼠标点一下会弹出一个下拉菜单之类的(之前淘宝也有,不过现在改版好像没有了) 之前以为是个png图标背景,后来在bootstrap中看到有一个图标样式叫做caret的用来实 ...

  5. ARC 与非ARC 之间的转换,以及如何使一个项目中,ARC与非ARC共存

    1,非ARC 转 ARC的操作 XCode 的 Edit -- Refactor -- Convert to Object-C ARC (注意,一般在一个大项目中,很少直接使用此方法,其正确率有待考虑 ...

  6. iOS_Quartz 2D绘图

    目  录: 一.基础知识掌握 二.Quartz 2D绘图基础:CGContextRef实现简单地绘制图形 三.CGContextRef实现文字.图片.基于路径的图形绘制 四.在内存中绘制位图 五.添加 ...

  7. matlab第一个小应用

    今天安装了matlab,以前还是上线性代数的时候,老师让用过,以及水了一次数模的时候玩了一下.以前太年轻,总觉得这个用处不大虽然别人一直强调这个神器... 到了自己要用的时候才会意识到.大家可能也都听 ...

  8. springmvc时间类型值传输映射

    背景:springmvc4.3.2+spring4.3.2+mybatis3.4.1 当前台传递的参数有时间类型时,封装的vo对象也有对应的时间类型与之对象, 但是如果此时用对象去接收后台会报错,类型 ...

  9. eval 加密 js,把js代码重新编续成新的代码,然后eval运行

    eval( function(p, a, c, k, e, r) { e = function(c) { return c.toString(a) //35 }; if (!''.replace(/^ ...

  10. DataX-MySQL(读写)

    DataX操作MySQL 一. 从MySQL读取 介绍 MysqlReader插件实现了从Mysql读取数据.在底层实现上,MysqlReader通过JDBC连接远程Mysql数据库,并执行相应的sq ...