A1150 Travelling Salesman Problem (25)

写的有些复杂,后续会修改

#include
#include
using namespace std;
const int maxn=210;
const int inf=0x3fffffff;
int G[maxn][maxn],a[maxn];
bool vis[maxn]={false};
int main()
{
	int N,m;
	cin>>N>>m;
	fill(G[0],G[0]+maxn*maxn,inf);
	for(int i=0;i<m;i++)
	{
		int a,b,d;
		scanf("%d %d %d",&a,&b,&d);
		G[a][b]=G[b][a]=d;
	}
	int k,n,minsum=inf,mini=-1;
	cin>>k;
	for(int i=1;i<=k;i++)
	{
		bool  flag1=false,flag2=false,flag3=false;
		fill(vis,vis+maxn,false);
		int sum=0;
		scanf("%d %d",&n,&a[0]);
		for(int j=1;j<n;j++)
		{
			scanf("%d",&a[j]);
			if(vis[a[j]]==false)
			{
				vis[a[j]]=true;
			}
			else
			{
				flag2=true;
			}
			if(G[a[j]][a[j-1]]==inf)
			{
				flag3=true;
			}
			else
			{
	            sum+=G[a[j]][a[j-1]];
			}
		}
		for(int j=1;j<=N;j++)
		{
			if(vis[j]==false)
			{
				flag1=true;
				break;
			}
		}
		printf("Path %d: ",i);
		if(flag3)
		{
			printf("NA (Not a TS cycle)\n");
		}
		else if(flag1)
		{
			printf("%d (Not a TS cycle)\n",sum);
		}
		else
		{
			if(flag2)
			{
				printf("%d (TS cycle)\n",sum);
			}
			else
			{
				printf("%d (TS simple cycle)\n",sum);
			}
			if(sum<minsum)
			{
				minsum=sum;
				mini=i;
			}
		    
		}
	}
	printf("Shortest Dist(%d) = %d",mini,minsum);
	
}

你可能感兴趣的:(pat刷题)