1150 Travelling Salesman Problem (25 分)
水~。
const int N=210;
int g[N][N];
int n,m,q;
int main()
{
cin>>n>>m;
memset(g,0x3f,sizeof g);
while(m--)
{
int a,b,c;
cin>>a>>b>>c;
g[a][b]=g[b][a]=c;
}
cin>>q;
int kase=1;
int minidx=0,mindist=INF;
while(q--)
{
int k;
cin>>k;
vector<int> v(k);
set<int> node;
for(int i=0;i<k;i++)
{
cin>>v[i];
node.insert(v[i]);
}
bool ok=true;
int res=0;
for(int i=0;i<k-1;i++)
{
int a=v[i],b=v[i+1];
if(g[a][b] == INF)
{
ok=false;
break;
}
res+=g[a][b];
}
string desc;
if(!ok) desc="Not a TS cycle";
else if(v[0] != v[k-1] || node.size() < n) desc="Not a TS cycle";
else if(k != n+1) desc="TS cycle";
else desc="TS simple cycle";
if(!ok)
printf("Path %d: NA (%s)\n",kase++,desc.c_str());
else
{
if(desc != "Not a TS cycle" && res < mindist)
{
mindist=res;
minidx=kase;
}
printf("Path %d: %d (%s)\n",kase++,res,desc.c_str());
}
}
printf("Shortest Dist(%d) = %d\n",minidx,mindist);
//system("pause");
return 0;
}