• PAT A1150 Travelling Salesman Problem


    1150 Travelling Salesman Problem

    分数 25

    作者 CHEN, Yue

    单位 浙江大学

    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 (2City1 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:

    1. 6 10
    2. 6 2 1
    3. 3 4 1
    4. 1 5 1
    5. 2 5 1
    6. 3 1 8
    7. 4 1 6
    8. 1 6 1
    9. 6 3 1
    10. 1 2 1
    11. 4 5 1
    12. 7
    13. 7 5 1 4 3 6 2 5
    14. 7 6 1 3 4 5 2 6
    15. 6 5 1 4 3 6 2
    16. 9 6 2 1 6 3 4 5 2 6
    17. 4 1 2 5 1
    18. 7 6 1 2 5 4 3 1
    19. 7 6 3 2 5 4 1 6

    Sample Output:

    1. Path 1: 11 (TS simple cycle)
    2. Path 2: 13 (TS simple cycle)
    3. Path 3: 10 (Not a TS cycle)
    4. Path 4: 8 (TS cycle)
    5. Path 5: 3 (Not a TS cycle)
    6. Path 6: 13 (Not a TS cycle)
    7. Path 7: NA (Not a TS cycle)
    8. Shortest Dist(4) = 8

     

    * 是旅行商问题的必要条件是所有顶点都要被访问过至少一次,至于是简单回路
     * 还是复杂回路,就看路径回到起点的回路条数;
     * 如果路径不存在或者有顶点没有被访问到,那么就不是旅行商回路;

    1. /**
    2. * 是旅行商问题的必要条件是所有顶点都要被访问过至少一次,至于是简单回路
    3. * 还是复杂回路,就看路径回到起点的回路条数;
    4. * 如果路径不存在或者有顶点没有被访问到,那么就不是旅行商回路;
    5. */
    6. #include <iostream>
    7. #include <algorithm>
    8. using namespace std;
    9. const int N = 210, INF = 1e9;
    10. int g[N][N]; //存储图
    11. bool hs[N];
    12. int Nv, Ne; //顶点数,边数
    13. void Read()
    14. {
    15. fill(*g, *g + N * N, INF); //初始化
    16. cin >> Nv >> Ne;
    17. for(int i=0; i<Ne; ++i)
    18. {
    19. int u, v, w;
    20. cin >> u >> v >> w;
    21. g[u][v] = g[v][u] = min(g[u][v], w);
    22. }
    23. int k;
    24. cin >> k;
    25. int MIN = INF, idx = -1;
    26. for(int i=1; i<=k; ++i)
    27. {
    28. fill(hs, hs+N, 0); //初始化,统计已被访问过的顶点编号
    29. //pre记录上一次的顶点编号,ori记录起点,dis记录路径长度
    30. int pre, ori = -1, dis = 0;
    31. int cnt = 0; //cnt记录回路条数
    32. int m;
    33. cin >> m;
    34. for(int j=0; j<m; ++j)
    35. {
    36. int u;
    37. cin >> u;
    38. if(j != 0)
    39. {
    40. if(g[pre][u] != INF)
    41. dis += g[pre][u];
    42. else
    43. dis = INF; //路径不存在
    44. }
    45. else
    46. ori = u;
    47. hs[u] = 1; //记录u点已被访问过
    48. //如果形成一条回路,cnt加一
    49. if(j != 0 && u == ori) ++cnt;
    50. pre = u;
    51. }
    52. for(int j=1; j<=Nv; ++j)
    53. if(hs[j] == 0)
    54. cnt = 0; //如果有顶点没有被访问过
    55. printf("Path %d: ", i);
    56. if(dis >= INF)
    57. {
    58. printf("NA ");
    59. cnt = 0; //如果dis >= INF, 那么一定是不存在路径的情况
    60. }
    61. else printf("%d ", dis);
    62. if(cnt == 0) puts("(Not a TS cycle)");
    63. else if(cnt == 1) puts("(TS simple cycle)");
    64. else puts("(TS cycle)");
    65. if(cnt && dis < MIN)
    66. {
    67. MIN = dis;
    68. idx = i;
    69. }
    70. }
    71. printf("Shortest Dist(%d) = %d\n", idx, MIN);
    72. }
    73. int main()
    74. {
    75. Read();
    76. return 0;
    77. }

  • 相关阅读:
    37 年来首次 FSF 允许非正式会员提名董事会候选人;Linux RamFS 文件系统移植到 Rust;Git 2.35 发布 | 开源日报
    C# 基于腾讯云人脸核身和百度云证件识别技术相结合的 API 实现
    linux安装配置 kafka并简单使用
    Spring MVC源码详解
    Docker下载Elasticsearch镜像并运行Elasticsearch容器
    微服务架构
    加载页面前执行js脚本,实现浏览器指纹变更
    神经网络 炒股,神经网络 软件
    mysql启动报错The server quit without updating PID file几种解决办法
    【658. 找到 K 个最接近的元素】
  • 原文地址:https://blog.csdn.net/qq_51825761/article/details/130907753