zoukankan      html  css  js  c++  java
  • 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:

    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

    Solution:
      这道题是水题,就是一个简单的计算路程和判断的过程,不用大脑
     1 #include <iostream>
     2 #include <vector>
     3 using namespace std;
     4 int n, m, k, x;
     5 int dis[205][205] = { 0 };
     6 int main()
     7 {
     8     cin >> n >> m;
     9     for (int i = 0; i < m; ++i)
    10     {
    11         int a, b, c;
    12         cin >> a >> b >> c;
    13         dis[a][b] = dis[b][a] = c;
    14     }
    15     cin >> k;
    16     int minDis = INT32_MAX, minIdex = 0;
    17     for (int t = 1; t <= k; ++t)
    18     {
    19         int calDis = 0;
    20         bool isCycle = true;
    21         vector<bool>visit(n + 1, true);
    22         cin >> x;
    23         vector<int>path(x);
    24         for (int i = 0; i < x; ++i)
    25         {
    26             cin >> path[i];
    27             visit[path[i]] = false;
    28         }
    29         for (int i = 1; i < x; ++i)
    30         {
    31             if (dis[path[i - 1]][path[i]] > 0)
    32                 calDis += dis[path[i - 1]][path[i]];
    33             else//此路不通
    34             {
    35                 isCycle = false;
    36                 calDis = -1;//没有结果。输出为NA
    37                 break;
    38             }
    39         }
    40         if (path[0] != path[x - 1])isCycle = false;//不是回路
    41         for (int i = 1; i <= n && isCycle; ++i)
    42             if (visit[i] == true)
    43                 isCycle = false;
    44         if(calDis<0)
    45             printf("Path %d: NA (Not a TS cycle)
    ", t);
    46         else if (!isCycle)
    47             printf("Path %d: %d (Not a TS cycle)
    ", t, calDis);
    48         else if(x==n+1)
    49             printf("Path %d: %d (TS simple cycle)
    ", t, calDis);
    50         else 
    51             printf("Path %d: %d (TS cycle)
    ", t, calDis);
    52         if (isCycle && minDis > calDis)
    53         {
    54             minDis = calDis;
    55             minIdex = t;
    56         }
    57     }
    58     printf("Shortest Dist(%d) = %d", minIdex, minDis);
    59     return 0;
    60 }
     
  • 相关阅读:
    ES6小点心第二弹——底部浮现弹窗
    ES6小点心之通用弹窗
    从输入URL到页面加载的过程?如何由一道题完善自己的前端知识体系!
    【quickhybrid】如何实现一个Hybrid框架
    【开源】canvas图像裁剪、压缩、旋转
    优雅的H5下拉刷新【minirefresh】
    前端筑基篇(一)->ajax跨域原理以及解决方案
    钉钉开放与商业化团队前端大量招人
    AJAX请求真的不安全么?谈谈Web安全与AJAX的关系。
    【quickhybrid】iOS端的项目实现
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11920574.html
Copyright © 2011-2022 走看看