很繁琐的题目,难其实不难。
#include<bits/stdc++.h>
using namespace std;
const int maxn=205;
const int INF = 0x3fffffff;
int G[maxn][maxn];
int in[maxn],arr[maxn];
int opti=0,optd=INF;
int main(){
int n,m,u,v,d;
fill(G[0],G[0]+maxn*maxn,INF);
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++){
scanf("%d%d%d",&u,&v,&d);
G[u][v]=d;
G[v][u]=d;
}
int k,t,x;
scanf("%d",&k);
for(int i=0;i<k;i++){
scanf("%d",&t);
int flag=0,last,tag=0,na=0,label=0,sum=0;
memset(in,0,sizeof(in));
for(int j=0;j<t;j++){
scanf("%d",&x);
if(j!=0 &&G[last][x]!=INF) sum+=G[last][x];
if(j!=0 &&G[last][x]==INF) na=1;
last=x;
arr[j]=x;
in[x]++;
}
for(int j=1;j<=n;j++){
if(j!=last && in[j]!=1){
label=1;
break;
}
}
for(int j=1;j<=n;j++){
if(in[j]<1){
tag=1;
break;
}
}
if(arr[0]==arr[t-1]&&label==0&&na==0){
printf("Path %d: %d (TS simple cycle)\n",i+1,sum);
if(sum < optd){
optd= sum;
opti = i+1;
}
}else if(in[arr[0]]>=2 && tag==0&&na==0){
printf("Path %d: %d (TS cycle)\n",i+1,sum);
if(sum < optd){
optd= sum;
opti = i+1;
}
}else{
if(na==1)
printf("Path %d: NA (Not a TS cycle)\n",i+1);
else
printf("Path %d: %d (Not a TS cycle)\n",i+1,sum);
}
}
printf("Shortest Dist(%d) = %d\n",opti,optd);
return 0;
}