首页 > 其他 > 详细

PAT Travelling Salesman Problem

时间:2020-09-02 14:59:14      阅读:47      评论:0      收藏:0      [点我收藏+]

我真纳了闷了

为什么我代码不通过

#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

 

PAT Travelling Salesman Problem

原文:https://www.cnblogs.com/szh-blog/p/13601238.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!