您的位置:首页 > 其它

zoj 3166 Lazy Tourist(最短路Floyd)

2010-11-28 22:53 330 查看
求出到达hotel的环的最短路。



因为是求最后再到达hotel的最小环, 所以初始化都为INT_MAX



单向的路,我很纠结 = =。。。



其他就很水了。



#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <memory.h>
#include <limits.h>
using namespace std;
int main(void)
{
	int map[110][110];
	int n,c,m,from,to,len;
	int hotel[110];
	while( cin >> n >> c )
	{
		for(int i=0; i<=n; i++)	
			for(int k=0; k<=n; k++)
				map[i][k] = INT_MAX; 
		for(int i=1; i<=c; i++)
			cin >> hotel[i];
		cin >> m;
		int sum = INT_MAX;
		for(int i=0; i<m; i++)
		{
			cin >> from >> to >> len;  
			map[from][to] = len; 		//map[to][from] = len is wrong. Single way. 
		}
		for(int i=1; i<=n; i++)
			for(int j=1; j<=n; j++)
				for(int k=1; k<=n; k++)
					if( map[j][i] != INT_MAX && map[i][k] != INT_MAX && map[j][k] > map[j][i] + map[i][k] )
						map[j][k] = map[j][i] + map[i][k];
		int result = 0;
		for(int i=1; i<=c; i++)
			if( map[hotel[i]][hotel[i]] < sum ) // Find the smallest one.
			{
				sum = map[hotel[i]][hotel[i]];
				result = hotel[i];
			}
		if( result )
			cout << result << endl;
		else
			cout << "I will nerver go to that city!" << endl;		
	}
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: