PAT甲级1150 Travelling Salesman Problem (25分) 模拟的简单题

1150 Travelling Salesman Problem (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<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 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

其实就这三句话,转换成对应的判断条件就好啦

  • TS simple cycle if it is a simple cycle that visits every city; 如果是经过了所有结点并且是个简单圆,经过所有结点好判断,简单圆判断起来需要 路径里面的结点个数是所有结点+1,并且收尾结点一样
  • 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. 剩余的情况

AC代码


#include <iostream>
#include <string>
#include <vector>
#include <climits>
using namespace std;

struct node
{
    string name;
    int high;

};
int  sort2(node a,node b )
{
    if(a.high!=b.high)
        return a.high>b.high;
    else
        return a.name<b.name;
}
int main()
{
    int N,Ne;
    cin>>N>>Ne;
    int edge[N+1][N+1];
    fill(edge[0],edge[0]+(N+1)*(N+1),-1);
    for(int i=0; i<Ne; i++)
    {
        int a,b,c;
        cin>>a>>b>>c;
        edge[a][b]=c;
        edge[b][a]=c;
    }
    int num;
    cin>>num;
    int cycleShort=INT_MAX;
    int shortID;
    for(int i=0; i<num; i++)
    {
        int pathLong;//记录路径个数
        cin>>pathLong;
        int sn[pathLong];
        for(int i=0; i<pathLong; i++)
        {
            cin>>sn[i];
        }
        int nodeSign[N+1]= {0};

        int pathOK=1;

        int path=0;
        for(int i=0; i<pathLong-1; i++)
        {
            if(edge[sn[i]][sn[i+1]]==-1)
            {
                pathOK=0;
                break;
            }
            path+=edge[sn[i]][sn[i+1]];
            nodeSign[sn[i]]=1;
            nodeSign[sn[i+1]]=1;
        }
        if(pathOK==0)
        {
            printf("Path %d: NA (Not a TS cycle)",i+1);
        }
        else
        {
            //路径肯定是没问题了
            //检测是否访问了所有结点
            int allNodeSign=1;
            for(int i=1; i<=N; i++)
            {
                if(nodeSign[i]==0)
                {
                    allNodeSign=0;
                    break;
                }
            }
            if(pathLong==N+1&&allNodeSign&&sn[0]==sn[pathLong-1]){
                //简单环并且访问了所有结点
                 printf("Path %d: %d (TS simple cycle)",i+1,path);
                  if(path<cycleShort){
                    cycleShort=path;
                    shortID=i+1;
                 }
            }
            else if(allNodeSign&&sn[0]==sn[pathLong-1]){
                 printf("Path %d: %d (TS cycle)",i+1,path);
                 if(path<cycleShort){
                    cycleShort=path;
                    shortID=i+1;
                 }

            }
            else
                 printf("Path %d: %d (Not a TS cycle)",i+1,path);


        }
cout<<endl;
    }
    printf("Shortest Dist(%d) = %d",shortID,cycleShort);
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章