PAT 甲级 1150 Travelling Salesman Problem
https://pintia.cn/problem-sets/994805342720868352/problems/1038430013544464384
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<N≤200), 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 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
代码:
#include <bits/stdc++.h>
using namespace std; #define inf 0x3f3f3f3f
int N, M, K;
int dis[220][220];
int vis[220], go[220]; int main() {
scanf("%d%d", &N, &M);
memset(dis, inf, sizeof(dis));
while(M --) {
int st, en, cost;
scanf("%d%d%d", &st, &en, &cost);
if(cost < dis[st][en]) {
dis[st][en] = cost;
dis[en][st] = dis[st][en];
}
} scanf("%d", &K);
int temp = 0, ans = INT_MAX;
for(int k = 1; k <= K; k ++) {
int T;
bool can = false;
int cnt1 = 0, cnt2 = 0;
memset(vis, 0, sizeof(vis));
bool flag = true;
int sum = 0;
scanf("%d", &T);
for(int i = 1; i <= T; i ++) {
scanf("%d", &go[i]);
vis[go[i]] ++;
if(i > 1) {
if(dis[go[i]][go[i - 1]] != inf) {
sum += dis[go[i]][go[i - 1]];
}
else flag = false;
}
} printf("Path %d: ", k);
if(!flag)
printf("NA (Not a TS cycle)\n");
else {
int iscycle = 0;
for(int i = 1; i <= N; i ++) {
if(vis[i] == 0)
iscycle = 1;
if(vis[i] == 1) cnt1 ++;
if(vis[i] > 1) cnt2 ++;
} if(iscycle == 1) printf("%d (Not a TS cycle)\n", sum);
else if(cnt2 == 1 && vis[go[1]] == 2) {
can = true;
printf("%d (TS simple cycle)\n", sum);
}
else if(cnt2 >= 1 && vis[go[1]] >= 2) {
can = true;
printf("%d (TS cycle)\n", sum);
}
else if(cnt2 >= 1 && vis[go[1]] < 2)
printf("%d (Not a TS cycle)\n", sum);
else printf("%d (Not a TS cycle)\n", sum); if(can && sum < ans) {
ans = sum;
temp = k;
} } } printf("Shortest Dist(%d) = %d\n", temp, ans);
return 0;
}
被图论支配的上午 暴躁 Be 主 在线编程
一会有牛客的比赛 哭咧咧
PAT 甲级 1150 Travelling Salesman Problem的更多相关文章
- 1150 Travelling Salesman Problem(25 分)
The "travelling salesman problem" asks the following question: "Given a list of citie ...
- 1150 Travelling Salesman Problem
The "travelling salesman problem" asks the following question: "Given a list of citie ...
- PAT A1150 Travelling Salesman Problem (25 分)——图的遍历
The "travelling salesman problem" asks the following question: "Given a list of citie ...
- PAT_A1150#Travelling Salesman Problem
Source: PAT A1150 Travelling Salesman Problem (25 分) Description: The "travelling salesman prob ...
- 构造 - HDU 5402 Travelling Salesman Problem
Travelling Salesman Problem Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5402 Mean: 现有一 ...
- HDU 5402 Travelling Salesman Problem (构造)(好题)
大致题意:n*m的非负数矩阵,从(1,1) 仅仅能向四面走,一直走到(n,m)为终点.路径的权就是数的和.输出一条权值最大的路径方案 思路:因为这是非负数,要是有负数就是神题了,要是n,m中有一个是奇 ...
- HDOJ 5402 Travelling Salesman Problem 模拟
行数或列数为奇数就能够所有走完. 行数和列数都是偶数,能够选择空出一个(x+y)为奇数的点. 假设要空出一个(x+y)为偶数的点,则必须空出其它(x+y)为奇数的点 Travelling Salesm ...
- HDU 5402 Travelling Salesman Problem (模拟 有规律)(左上角到右下角路径权值最大,输出路径)
Travelling Salesman Problem Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (J ...
- PAT-1150(Travelling Salesman Problem)旅行商问题简化+模拟图+简单回路判断
Travelling Salesman Problem PAT-1150 #include<iostream> #include<cstring> #include<st ...
随机推荐
- Oracle数据库的非归档模式迁移到归档模式
先观察当前的状态: [root@o_target ~]# su - oracle [oracle@o_target ~]$ sqlplus / as sysdba SQL*Plus ...
- PostgreSQL的psql客户端各种连接错误总结
磨砺技术珠矶,践行数据之道,追求卓越价值回到上一级页面:PostgreSQL基础知识与基本操作索引页 回到顶级页面:PostgreSQL索引页[作者 高健@博客园 luckyjackgao@g ...
- c++ 菜单动态效果
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> ...
- Linux命令学习笔记2(mysql安装和mysql-python安装)
linux下 强制安装 rpm安装包(切换到root用户): rpm -ivh bluefish-shared-data-2.2.7-1.el6.noarch.rpm --nodeps --forc ...
- taglib报错The content of element type "taglib" must match "(tlib-version,...)
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE taglib PUBLIC "-// ...
- Wannafly挑战赛18B 随机数
Wannafly挑战赛18B 随机数 设\(f_i\)表示生成\(i\)个数有奇数个1的概率. 那么显而易见的递推式:\(f_i=p(1-f_{i-1})+(1-p)f_{i-1}=(1-2p)f_{ ...
- SpringBoot日记——日志框架篇
在项目的开发中,日志是必不可少的一个记录事件的组件,所以也会相应的在项目中实现和构建我们所需要的日志框架. 而市面上常见的日志框架有很多,比如:JCL.SLF4J.Jboss-logging.jUL. ...
- 通过Task异步加快对数组的运算
一.介绍 Task是.NetFramework3.0出现的,线程是基于线程池,然后提供了丰富的API. 先用AverageAssign方法把一组数据平均分成n组,再通过遍历n组数据,循环开Task多线 ...
- windows下安装Mongodb_4.0.6最新版及常用命令
今天下载了最新版Mongodb进行安装,发现相比较于以前,方便了很多,直接下载: 一.下载地址:https://www.mongodb.com/download-center/enterprise 二 ...
- linux上的mysql配置过程
自己阿里云上的服务器,记录下mysql的配置过程防止后面忘记 1. 首先用apt-get工具安装mysql sudo apt-get install mysql-server sudo apt-get ...