IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> PTA甲级 1150 Travelling Salesman Problem (C++) -> 正文阅读

[C++知识库]PTA甲级 1150 Travelling Salesman Problem (C++)

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 ) N (2<N≤200) N(2<N200), the number of cities, and M M M, the number of edges in an undirected graph. Then M M M lines follow, each describes an edge in the format City1 City2 Dist, where the cities are numbered from 1 to N N N and the distance Dist is positive and is no more than 100. The next line gives a positive integer K K K which is the number of paths, followed by K K K lines of paths, each in the format:
n n n C 1 C_1 C1? C 2 C_2 C2? C n C_n Cn?
where n n n is the number of cities in the list, and C i C_i 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

Caution:

主要就是判断逻辑,NA最好判断,如果碰到不存在的路径就直接可以判为NA,并且后面不可能改变,这个最简单的判断就直接在输入路径的同时进行;
然后就看是不是 TS Cycle —— 如果首尾节点不一样的话肯定不是 TS Cycle,退出判断;
如果路径里面有节点没有被访问的话也不可能是 TS Cycle,退出判断;
接下来就好办了,直接看路径里面的节点个数,需要注意的是如果判断进行到了这一步那么肯定这个路径是一个 TS Cycle 并且访问到了所有的节点,那么我们可以直接从路径的节点个数来判断这个路径是不是一个 simple cycle

Solution:

// Talk is cheap, show me the code
// Created by Misdirection 2021-08-31 21:33:23
// All rights reserved.

#include <iostream>
#include <vector>
#include <unordered_map>

using namespace std;

int graph[205][205] = {0};
int path[500];

int main(){
    int n, m;
    scanf("%d %d", &n, &m);
    
    int id1, id2, dis;
    for(int i = 0; i < m; ++i){
        scanf("%d %d %d", &id1, &id2, &dis);

        graph[id1][id2] = dis;
        graph[id2][id1] = dis;
    }

    int k;
    scanf("%d", &k);
    int shortest = 2147483647;
    int ansId = -1;
    for(int i = 0; i < k; ++i){
        int num;
        scanf("%d", &num);

        int flag = -2;
        // flag = -2 表示 undecided
        // flag = -1 表示 NA:只有遇到不存在的路径才会 NA
        // flag = 0 表示 Not a TS Cycle:【不是一个 cycle】 或者 【没有遇到所有的 cities】

        // flag = 1 表示 TS cycle:是一个 cycle,并且访问了每一个 city,但是除了首尾节点有重复访问的;
        // flag = 2 表示 TS simple cycle:是一个 cycle 并且访问了每一个 city,并且只访问一次;

        // 只要是 TS cycle 就会参与最优路线比较

        if(num <= n) flag = 0;
        int pathLen = 0;

        unordered_map<int, int> times;
        for(int j = 0; j < num; ++j){
            scanf("%d", &path[j]);

            if(j != 0 && graph[path[j - 1]][path[j]] == 0) flag = -1;
            
            pathLen += graph[path[j - 1]][path[j]];
            if(j != 0) times[path[j]]++;
        }

        if(flag != -1){
            if(path[0] != path[num - 1]) flag = 0;
            else{
                for(int j = 1; j <= n; ++j){
                    if(times[j] == 0){
                        flag = 0;
                        break;
                    }
                }

                if(flag != 0) flag = (num == n + 1 ? 2 : 1);
            }
        }

        if(flag == -1) printf("Path %d: NA (Not a TS cycle)\n", i + 1);
        else if(flag == 0) printf("Path %d: %d (Not a TS cycle)\n", i + 1, pathLen);
        else{
            if(pathLen < shortest){
                shortest = pathLen;
                ansId = i + 1;
            }

            if(flag == 1) printf("Path %d: %d (TS cycle)\n", i + 1, pathLen);
            else printf("Path %d: %d (TS simple cycle)\n", i + 1, pathLen);
        }
    }

    printf("Shortest Dist(%d) = %d\n", ansId, shortest);

    return 0;
}

在这里插入图片描述

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-09-01 11:44:21  更:2021-09-01 11:45:49 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2025年2日历 -2025/2/25 10:24:37-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码