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. 搭建backup服务器基本流程

    守护进程实现,将daemon配置在backup服务器,因为这样其他服务器就能通过服务推即可. 服务端配置流程:  前提两台服务41为backup服务  31是其他服务器即客户端 在41服务器中配置  ...

  2. grep命令详细解析 --非原创 原作者ggjucheng

    简介 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它 ...

  3. 025_MapReduce样例Hadoop TopKey算法

    1.需求说明

  4. 015_[小插曲]看黄老师《炼数成金Hadoop应用开发实战案例》笔记

    1.大数据金字塔结构 Data Source-->Data Warehouses/Data Marts-->data exploration-->Data Mining-->D ...

  5. VMware安装Centos7过程

    VMware安装Centos7过程 1.打开VMwear选择新建虚拟机 2.典型安装与自定义安装 典型安装:VMwear会将主流的配置应用在虚拟机的操作系统上,对于新手来很友好. 自定义安装:自定义安 ...

  6. 20145222黄亚奇 《网络对抗技术》 MAL_逆向与Bof基础

    学习目的 通过一些方法,使能够运行本不该被运行的代码部分,或得到shell的使用: 将正常运行代码部分某处call后的目标地址,修改为另一部分我们希望执行.却本不应该执行的代码部分首地址(这需要我们有 ...

  7. 新建maven web后controller不能被扫描到

    1.新建maven web 子工程 2.修改web.xml. 3.resources下建立spring.xml与springmvc.xml. 4.spring.xml删除对controller的扫描. ...

  8. Linux FTP 上传一键脚本

    下面来介绍一下这个 FTP 上传一键脚本 ftp_upload.sh. 用途:用于在Linux系统下搭建FTP客户端向FTP服务器端上传文件: 总结一下 ftp_upload.sh 特点:1.支持文件 ...

  9. springmvc-restful

    1.restful概述 REST 仅仅是一种架构的风格,并不是真正的架构,也不是一个软件,而是一种思想. 我们可以基于现有的HTTP.URI.XML.等现有技术来实现REST的风格.而不用去学习任何新 ...

  10. 【P2325】王室联邦(树的遍历+贪心)

    在肖明 #神#的推荐下,我尝试了这个题,一开始想的是暴力枚举所有的点,然后bfs层数,试着和肖明 #神#说了这种方法之后, #神#轻蔑的一笑,说这不就是一个贪心么,你只需要先建树,然后从底下向上遍历, ...