#include <iostream>
#include <unordered_set>
#include <climits>
using namespace std;
int G[210][210];
unordered_set<int> nodes;
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i = 0;i < 210;i++)
for(int j = 0;j < 210;j++)
G[i][j] = INT_MAX;
for(int i = 0;i < m;i++){
int u,v,dis;
scanf("%d%d%d",&u,&v,&dis);
G[u][v] = G[v][u] = dis;
}
int k;
scanf("%d",&k);
int minDis = INT_MAX;
int minIndex = -1;
for(int i = 1;i <= k;i++){
int N;
int sum = 0;
nodes.clear();
scanf("%d",&N);
int begin;
scanf("%d",&begin);
nodes.insert(begin);
bool b = false;
int first,last;
first = begin;
for(int j = 1;j < N;j++){
int temp;
scanf("%d",&temp);
nodes.insert(temp);
if(b == false && G[begin][temp] != INT_MAX)
sum += G[begin][temp];
else{
sum = -1;
b = true;
}
begin = temp;
if(j == N - 1)
last = temp;
}
printf("Path %d: ",i);
if(sum == -1)
printf("NA");
else
printf("%d",sum);
// TS cycle : 经过所有点,且第一个点和最后一个点相等
// TS simple cycle : 在TS cycle基础上,要求N == n + 1
// else : not a TS cycle
if(nodes.size() == n && sum != -1 && first == last){
if(minDis > sum){
minDis = sum;
minIndex = i;
}
if(N == n + 1)
printf(" (TS simple cycle)\n");
else
printf(" (TS cycle)\n");
}
else
printf(" (Not a TS cycle)\n");
}
printf("Shortest Dist(%d) = %d",minIndex,minDis);
system("pause");
return 0;
}
PAT | A1150 Travelling Salesman Problem
最新推荐文章于 2024-09-02 23:09:18 发布