poj 2253 Frogger (kruskal+dijkstra+bellman-ford)

Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists’ sunscreen, he wants to avoid swimming and instead reach her by jumping.
Unfortunately Fiona’s stone is out of his jump range. Therefore Freddy considers to use other stones as intermediate stops and reach her by a sequence of several small jumps.
To execute a given sequence of jumps, a frog’s jump range obviously must be at least as long as the longest jump occuring in the sequence.
The frog distance (humans also call it minimax distance) between two stones therefore is defined as the minimum necessary jump range over all possible paths between the two stones.

You are given the coordinates of Freddy’s stone, Fiona’s stone and all other stones in the lake. Your job is to compute the frog distance between Freddy’s and Fiona’s stone.

Input The input will contain one or more test cases. The first line of each test case will contain the number of stones n (2<=n<=200). The next n lines each contain two integers xi,yi (0 <= xi,yi <= 1000) representing the coordinates of stone #i. Stone #1 is Freddy’s stone, stone #2 is Fiona’s stone, the other n-2 stones are unoccupied. There’s a blank line following each test case. Input is terminated by a value of zero (0) for n. Output For each test case, print a line saying “Scenario #x” and a line saying “Frog Distance = y” where x is replaced by the test case number (they are numbered from 1) and y is replaced by the appropriate real number, printed to three decimals. Put a blank line after each test case, even after the last one. Sample Input

2
0 0
3 4

3
17 4
19 4
18 5

0

Sample Output

Scenario #1
Frog Distance = 5.000

Scenario #2
Frog Distance = 1.414

题意:输入n代表n个点,之后的n行,为 1~n 的坐标,求1到2的最优路径(1到2经过的所有权值中的最大值min,求最小的min);

//diskstra 解法,0ms;
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
double graph[1010][1010];
double dijkstra(int n)//dijkstra 模板;
{
	double lowcost[1010],dis=0.000;
	int mst[1010];
	int i,j,minid;
	memset(mst,0,sizeof(mst));
	mst[1]=1;
	for(i=1;i<=n;i++)
		lowcost[i]=graph[1][i];
	for(i=1;i<=n;i++)
	{
		double min=99999999.999;
		for(j=1;j<=n;j++)
		{
			if(!mst[j] && lowcost[j]<min)
			{
				min=lowcost[j];
				minid=j;
			}
		}
		if(min>dis)//改一下松弛条件
			dis=min;
		mst[minid]=1;
		for(j=1;j<=n;j++)
		{
			if(!mst[j] && lowcost[j]>max(graph[minid][j],lowcost[minid]))
				lowcost[j]=max(graph[minid][j],lowcost[minid]);
		}
	}
	return lowcost[2]; //终点是2;
}
int main()
{
	int m,n,i,j,k=0;
	double x[210],y[210];
	while(scanf("%d",&n) && n)
	{
		for(i=1;i<=n;i++)
		{
			scanf("%lf %lf",&x[i],&y[i]);
			for(j=1;j<i;j++)
			{
				graph[i][j]=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[j]-y[i])*(y[j]-y[i]));
				graph[j][i]=graph[i][j];
			}
		}
		printf("Scenario #%d\n",++k);
		printf("Frog Distance = %.3f\n",dijkstra(n));
		printf("\n");
	}
	return 0;
}
//MST的 Kruskal 解法; 32ms
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int pre[40000];
struct node
{
	int a;
	int b;
	double c;
}s[40000];
bool cmp(node x,node y)
{
	return x.c<y.c;
}
int find(int x)
{
	if(pre[x]==-1)
		return x;
	return pre[x]=find(pre[x]);
}
int main()
{
	int n,k=0,i,j;
	double x[210],y[210];
	while(scanf("%d",&n) && n)
	{
		int p=0;
		memset(pre,-1,sizeof(pre));
		for(i=1;i<=n;i++)
		{
			scanf("%lf %lf",&x[i],&y[i]);
			for(j=1;j<i;j++)
			{
				double dis=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[j]-y[i])*(y[j]-y[i]));
				s[p].a=i,s[p].b=j,s[p].c=dis;
				p++;
			}
		}
		sort(s,s+p,cmp);
		double dis=0.000;
		for(i=0;i<p;i++)
		{
			int fx=find(s[i].a);
			int fy=find(s[i].b);
			if(fx!=fy)
			{
				pre[fy]=fx;
				if(find(1)==find(2))
				{
					dis=s[i].c;
					break;
				}
			}
		}
		printf("Scenario #%d\n",++k);
		printf("Frog Distance = %.3f\n",dis);
		printf("\n");
	}
	return 0;
}

#include<cstdio>//bellman-ford;47ms(此种方法较慢,但空间复杂度低)
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
double mst[40000];
struct node
{
	int a;
	int b;
	double c;
}s[40000];
void bellman_ford(int n,int m)
{
	int i,j;
	mst[1]=0.000;
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=m;j++)//双向路径;
		{
			if(mst[s[j].a]>max(s[j].c,mst[s[j].b]))//类似于dijkstra,选取最大值,并非求和; 
				mst[s[j].a]=max(s[j].c,mst[s[j].b]);//只求最大边权,而不求边权和;(一定是最短路径的最大某一个边权)
			if(mst[s[j].b]>max(s[j].c,mst[s[j].a]))
				mst[s[j].b]=max(s[j].c,mst[s[j].a]);
		}
	}
	printf("Frog Distance = %.3f\n\n",mst[2]);
}
int main()
{
	int n,i,j,v=0;
	double x[210],y[210],k;
	while(scanf("%d",&n) && n)
	{
		int p=0;
		for(i=1;i<=n;i++)
			mst[i]=9999999.999;
		for(i=1;i<=n;i++)
		{
			scanf("%lf %lf",&x[i],&y[i]);
			for(j=1;j<i;j++)
			{
				k=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
				p++;
				s[p].a=i,s[p].b=j,s[p].c=k;
			}
		}
		printf("Scenario #%d\n",++v);
		bellman_ford(n,p);
	}
	return 0;}

    原文作者:Bellman - ford算法
    原文地址: https://blog.csdn.net/qq_36368339/article/details/55050108
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞