热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

1150TravellingSalesmanProblem(25分)(分析题目,细节处理)

The“travellingsalesmanproblem”asksthefollowingquestion:“Givenalistofcitiesandthedistancesb

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 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

结尾无空行

本题一点也不难,就是一些小细节比较难注意,而且英文的题目比较难理解,需要读懂题,题目大致意思是:给定一个图,并且给定一个制定的路径,如果此路径经过有所有点一次,并且最终回到起点,说明是 simple path,如果经过某个点两次,说明只是一个 path,如果不能到达某点或者重点不是起点,说明 not a path,最后计算最短的那条路即可。

坑点分析:
1、起点要等于终点,但是起点要访问两次,所以最开始起点不要设置 true
2、有可能存在路径不存在的问题,这里我们设置无穷大,类似 迪杰斯特拉,如果存在路径不存在,直接输出 NA
3、建议编写代码编写注解,调试的时候方便理解
4、记录起点的位置,当输入终点的时候,判断是否相等

本题基本上他给的样例全过了,就是 25 分了,加油

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int N = 201;
int G[N][N];
int shortest = 1e9;
int shortestId = 0;
int main() {int n, m;cin >> n >> m;fill(G[0], G[0] &#43; N * N, 1e9);while (m--) {int x, y, d;cin >> x >> y >> d;G[x][y] &#61; d;G[y][x] &#61; d;}int K;cin >> K;for (int t &#61; 1; t <&#61; K; t&#43;&#43;) {int a[n &#43; 1] &#61; {0};int k;cin >> k;int path &#61; 0;int u, uu;cin >> u;uu &#61; u;bool flagA &#61; true;// 判断是否重复参观bool flagB &#61; true;// 判断是否全参观过bool flagC &#61; true;// 判断是否存在路径for (int i &#61; 1; i < k; i&#43;&#43;) {int v;cin >> v;if (a[v]) {// 已经参观过flagA &#61; false;}if (G[u][v] &#61;&#61; 1e9) {// 路径不存在flagC &#61; false;flagB &#61; false;// 也就没全参加} else {path &#43;&#61; G[u][v];}a[v] &#61; 1;u &#61; v;if (i &#61;&#61; k - 1) {// 起点与终点对比if (v !&#61; uu) {flagB &#61; false; }}}if (!flagC) {// 路径不存在printf("Path %d: NA ", t);} else {printf("Path %d: %d ", t, path);}for (int i &#61; 1; i <&#61; n; i&#43;&#43;) {// 判断是否全参加if (!a[i]) {flagB &#61; false;break;}}if (!flagB) {printf("(Not a TS cycle)\n");} else {if (shortest > path) {// 记录最短距离和编号shortest &#61; path;shortestId &#61; t;}if (flagA) {printf("(TS simple cycle)\n");} else {printf("(TS cycle)\n");}}}printf("Shortest Dist(%d) &#61; %d", shortestId, shortest);return 0;
}


推荐阅读
author-avatar
亲家你要干啥
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有