PAT Travelling Salesman Problem

我真纳了闷了

为什么我代码不通过

#include <iostream>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
const int N=300;
const int inf=999999;
int G[N][N];
int dist[N];
int n,m,k;
int main() {
    scanf("%d%d",&n,&m);
    int v1,v2,tmp;
    fill(G[0],G[0]+N*N,inf);
    for(int i=0;i<m;i++){
        scanf("%d%d%d",&v1,&v2,&tmp);
        G[v2][v1]=tmp;
        G[v1][v2]=tmp;
    }
    scanf("%d",&k);
    set<int> se;
    for(int i=0;i<k;i++){
        printf("Path %d: ",i+1);
        int num;
        scanf("%d",&num);
        int pre,next,start;
        scanf("%d",&pre);
        start=pre;
        se.insert(pre);
        for(int j=1;j<num;j++){
            scanf("%d",&next);
            se.insert(next);
            if(G[pre][next]==inf){
                dist[i]=inf;
                break;
            }
            else{
                dist[i]+=G[pre][next];
            }
            pre=next;
        }
        if(dist[i]==inf)printf("NA ");
        else printf("%d ",dist[i]);

        if(se.size()==n&&start==next){
            if(num==n+1)printf("(TS simple cycle)\n");
            else printf("(TS cycle)\n");
        }
        else{
            printf("(Not a TS cycle)\n");
            dist[i]=inf;
        }
        se.clear();
    }
    int minnum=inf,ind;
    for(int i=0;i<k;i++){
        if(dist[i]<minnum){
            minnum=dist[i];
            ind=i;
        }
    }
    printf("Shortest Dist(%d) = %d",ind+1,minnum);
    return 0;
}
#这输出哪里和标准答案对不上了?
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

 

posted @ 2020-09-02 13:40  uy9ko  阅读(112)  评论(0)    收藏  举报