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

题目大意:给定一个n个点,m条边的无向图。之后给出k次查询,每次给出一条路径,判断这条路径是这个图的旅行商环路、简单旅行商环路还是非旅行商环路,并给出所有旅行商环路(包括简单旅行商环路)的最短路径。

如果这个路径的起点终点相同,且访问到了所有顶点,则它是一个旅行商环路,否则不是;如果这个旅行商环路只有一个环,则是简单旅行商环路,否则是旅行商环路。

分析:在读入查询路径时,检查当前读入的顶点是否能到达上一个顶点,如果不能到达,说明不是旅行商环路。读入最后一个顶点时,要检查是否与第一个顶点相同,如果不是,则不是旅行商环路。读完查询路径之后,要检查是否所有顶点都访问过,且除了第一个顶点之外是否都只访问过一次,如果是,则是简单旅行商环路,否则是旅行商环路。同时记录所有旅行商环路的最小值和是第几次查询。

cpp 复制代码
#include<algorithm>
#include <iostream>
#include  <cstdlib>
#include  <cstring>
#include   <string>
#include   <vector>
#include   <cstdio>
#include    <queue>
#include    <stack>
#include    <ctime>
#include    <cmath>
#include      <map>
#include      <set>
#define INF 0xffffffff
#define db1(x) cout<<#x<<"="<<(x)<<endl
#define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
#define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
#define db4(x,y,z,r) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<endl
#define db5(x,y,z,r,w) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<", "<<#w<<"="<<(w)<<endl
using namespace std;

int main(void)
{
    #ifdef test
    freopen("in.txt","r",stdin);
    //freopen("in.txt","w",stdout);
    clock_t start=clock();
    #endif //test

    int n,m;scanf("%d%d",&n,&m);
    int num[n+5][n+5];
    for(int i=1;i<=n;++i)
        for(int j=1;j<=n;++j)
            num[i][j]=0;
    for(int i=0;i<m;++i)
    {
        int a,b,c;scanf("%d%d%d",&a,&b,&c);
        num[a][b]=num[b][a]=c;
    }
    int cnt,ans=1000000000,index=-1;scanf("%d",&cnt);
    for(int times=1;times<=cnt;++times)
    {
        printf("Path %d: ",times);
        int k,f=1,sum=0,nums=0;scanf("%d",&k);
        int flag[n+5]={0},path[k+5]={0};
        for(int i=0;i<k;++i)
        {
            scanf("%d",&path[i]);
            if(i!=k-1)
            {
                flag[path[i]]++;
                if(flag[path[i]]==1)nums++;
            }
            else if(i==k-1)
            {
                if(path[i]==path[0]);
            }

            if(flag[path[i]]>1&&path[i]!=path[0]&&f)f=2;
            if(i&&f)
            {
                if(!num[path[i]][path[i-1]])f=0;
                else sum+=num[path[i]][path[i-1]];
            }
        }

        if(f==0)printf("NA (Not a TS cycle)\n");
        else if(f)
        {
            if(path[k-1]==path[0])
            {
                if(nums==n)
                {
                    if(f==2)printf("%d (TS cycle)\n",sum);
                    else if(f==1)printf("%d (TS simple cycle)\n",sum);
                    if(sum<ans)ans=sum,index=times;
                }
                else printf("%d (Not a TS cycle)\n",sum);
            }
            else printf("%d (Not a TS cycle)\n",sum);
        }
    }
    printf("Shortest Dist(%d) = %d\n",index,ans);

    #ifdef test
    clockid_t end=clock();
    double endtime=(double)(end-start)/CLOCKS_PER_SEC;
    printf("\n\n\n\n\n");
    cout<<"Total time:"<<endtime<<"s"<<endl;        //s为单位
    cout<<"Total time:"<<endtime*1000<<"ms"<<endl;    //ms为单位
    #endif //test
    return 0;
}
相关推荐
圣保罗的大教堂1 个月前
1170 Safari Park (25)
pat考试
圣保罗的大教堂1 个月前
1166 Summit (25)
pat考试
圣保罗的大教堂1 个月前
1164 Good in C (20)
pat考试
圣保罗的大教堂1 个月前
1161 Merging Linked Lists (25)
pat考试
圣保罗的大教堂1 个月前
1156 Sexy Primes (20)
pat考试
圣保罗的大教堂2 个月前
1133 Splitting A Linked List (25)
pat考试
圣保罗的大教堂2 个月前
1097 Deduplication on a Linked List (25)
pat考试
圣保罗的大教堂2 个月前
1089 Insert or Merge (25)
pat考试
圣保罗的大教堂2 个月前
1081 Rational Sum (20)
pat考试