首页 > 代码库 > Floyd

Floyd

代码(自用):

#include<cstdio>
#include<cstring>
#define INF 0x3f3f3f3f
#define M 200
int main()
{
	int i,j,k,n,c,m,cost[M][M],hotel[M];
	int x,y,z;
	while(scanf("%d%d",&n,&c)!=EOF)
	{
		for(i = 1; i <= c ; i++ )
		{
			scanf("%d",&hotel[i]);
		}
		for( i = 1 ; i <= n ; i++ )
		{
			for( j = 1 ; j <= n ; j++ )
			{
				cost[i][j]=INF;
			}
		}
		scanf("%d",&m);
		for( i = 1 ; i <= m ; i++ )
		{
			scanf("%d%d%d",&x,&y,&z);
			cost[x][y]=z;
		}
		for( k = 1 ; k <= n ; k++ )
		{
			for(  i = 1 ; i <= n ; i++ )
				for( j = 1 ; j <= n ; j++ )
					if(cost[i][j] > cost[i][k] + cost[k][j])
						cost[i][j]=cost[i][k]+cost[k][j];
		}
		int cnt=-1;
		int min = INF;
		for( i = 1; i <= c ; i++ )
		{
			if(cost[hotel[i]][hotel[i]] < min)
			{
				cnt = hotel[i];
				min = cost[hotel[i]][hotel[i]];
			}
		}
		if(cnt == -1 )
			printf("I will nerver go to that city!\n");
		else
			printf("%d\n",cnt);
	}
	return 0;
}



Floyd