码迷,mamicode.com
首页 > 其他好文 > 详细

Floyd

时间:2014-07-21 11:42:15      阅读:180      评论:0      收藏:0      [点我收藏+]

标签: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

标签:floyd

原文地址:http://blog.csdn.net/u012860063/article/details/38013227

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!