hdu1596 find the safest road

//最大安全系数;dijkstra由最小路径改成最大路径
#include <stdio.h>
#define INF -99.0
#define MAXN 1002

double map[MAXN][MAXN],safe[MAXN];
int visited[MAXN];

void Dijkstra(int s,int t,int n)
{
	int i,j,u;
	double max;
	for (i=1;i<=n;i++)
	{
		safe[i]=map[s][i];
		visited[i]=0;
	}
	visited[s]=1;
	safe[s]=1;
	for (i=1;i<=n;i++)
	{
		max=INF;
		for (j=1;j<=n;j++)
		{
			if(!visited[j]&&max<safe[j])
			{
				max=safe[j];
				u=j;
			}
		}
		visited[u]=1;
		if(max==INF)
			break;
		for(j=1;j<=n;j++)
			if(!visited[j]&&safe[j]<map[u][j]*safe[u])
				safe[j]=map[u][j]*safe[u];
	}
}

int main()
{
	int n,i,j,Q,st,end;
	while(scanf("%d",&n)!=EOF)
	{
		for (i=1;i<=n;i++)
		{
			for(j=1;j<=n;j++)
				scanf("%lf",&map[i][j]);
		}
		scanf("%d",&Q);
	    while(Q--)
		{
			scanf("%d %d",&st,&end);
			Dijkstra(st,end,n);
			if(safe[end])
				printf("%.3lf\n",safe[end]);
			else
				printf("What a pity!\n");
		}
	}
	return 0;
}

你可能感兴趣的:(hdu1596 find the safest road)