杭电 1596 find the safest road(Floyd)

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1596

思路:限时5秒,Floyd打表完全没问题,听说Dijkstra也可以。

          最开始还傻乎乎写了一个判零函数,然后拼命用“while(scnaf("%d",&n))”这种错误写法去提交,只能说c++流毒颇深。连OLE都给我整出来了。

AC代码:

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
double a[1001][1001];
int n; 
void Floyd()
{
	for(int k=1;k<=n;k++)
	{
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=n;j++)
			{
				a[i][j]=max(a[i][j],a[i][k]*a[k][j]);
			}
		}
	}
}
int main()
{
	int m;
	while(scanf("%d",&n)!=EOF) 
	{
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
				scanf("%lf",&a[i][j]);
		scanf("%d",&m);
		Floyd();
		while(m--)
		{
			int x,y;
			scanf("%d%d",&x,&y);
			if(a[x][y]) printf("%.3lf\n",a[x][y]);
			else printf("What a pity!\n");
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/jack_jxnu/article/details/81482818