PAT甲级——A1150 TravellingSalesmanProblem【25】
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 C1 C2 ... Cn
where n is the number of cities in the list, and Ci'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 cycleif it is a simple cycle that visits every city;TS cycleif it is a cycle that visits every city, but not a simple cycle;Not a TS cycleif 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)Solution:
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
这道题是水题,就是一个简单的计算路程和判断的过程,不用大脑
#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】的更多相关文章
- 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 ...
- 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 ...
- pat 甲级 1010. Radix (25)
1010. Radix (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a pair of ...
- pat 甲级 1078. Hashing (25)
1078. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of t ...
- PAT 甲级 1003. Emergency (25)
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive int ...
- PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)
1070 Mooncake (25 分) Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...
- PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)
1032 Sharing (25 分) To store English words, one method is to use linked lists and store a word let ...
- PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*
1029 Median (25 分) Given an increasing sequence S of N integers, the median is the number at the m ...
随机推荐
- 在BUG分支下创建分支,开发后合并到bus分支
在BUG分支下创建分支 1.切换到bus分支 2,创建新分支 git checkout -b bugfix/fix_vedio_0627 3,把创建的分支push到远程分支 git push orig ...
- 嵌入式C语言3.5 关键字---运算符
1. 算数运算符 + - A +/- B 要求A,B数据类型一致 * 乘法 / 除法 %取模 乘法CPU可能需要多个周期,甚至需要利用软件的模拟方法来实 ...
- python内存管理及垃圾回收
一.python的内存管理 python内部将所有类型分成两种,一种由单个元素组成,一种由多个元素组成.利用不同结构体进行区分 /* Nothing is actually declared to b ...
- python3 requests库学习笔记(MOOC网)
奏:HTTP协议对资源的操作 方法说明:GET 请求获取URL位置的资源HEAD 请求获取URL位置资源的响应消息报告,即获得该资源的头部信息POST 请求向URL位置的资源后附加新的数据PUT 请求 ...
- ng2-file-upload插件在ionic3中的使用方法
本文主要说明在ionic3中使用ng2-file-upload插件,上传多种类型文件(图片.word.excel.ppt.mp4等)的使用方法. 1.html代码: <button ion-bu ...
- sql 根据身份证判断年龄是否小于18岁
SELECT *, Age= datediff(yy,cast(case when substring(PersonalId,,) ') /*若第7位不是'1'或'2'则表示是15位身份证编码规则*/ ...
- 利用Kubernetes(K8S)部署JAVA项目
一.jar包和war包区别 首先简单介绍一下jar包和war包区别,当时就没分清,导致部署总是傻傻分不清楚. jar包:jar包就是java的类进行编译生成的class文件就行打包的压缩包.里面是一些 ...
- linux make: *** No targets specified and no makefile found. Stop.
[root@localhost Python-]# ./configure checking build system type... x86_64-unknown-linux-gnu checkin ...
- EL属性范围用法sessionScope等(转)
EL 全名为Expression Language EL 语法很简单,它最大的特点就是使用上很方便.接下来介绍EL主要的语法结构: ${sessionScope.user.sex} 所有EL都是以${ ...
- JavaSE---多线程---集合类
1.概述 1.1 Java提供的ArrayList.LinkedList.HashMap等都是线程不安全的类,如何解决: 1.1 使用Collections类提供的方法将 其 包装成线程安全类: ...