POJ - 2253 Frogger (所有路径中边的权值最大的中的最小值)

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

题意:从1号点能达到2号点所有路径上边的权值的最大值的最小值;

可以用类似于最小生成树做,能用 Kruskal  做,也可用 pime 做,Kruskal 好理解, 因为要把所有的边从小到大排序,而prim 也可以做,(从标记点中有那些边,找一条最小的边,开始走,还不懂就看啊哈算法219页pime的详解)

代码一:

#include<stdio.h>     
#include<string.h>	   
#include<math.h>	 
#include<algorithm>
using namespace std;
#define Max 300
#define INF 0x3f3f3f3f

struct node1
{
	double x,y;
}stu1[Max];
int f[Max],n;

struct node
{
	int a,b;
	double dd;
}stu[Max*Max];

int cmp(node a,node b)
{
	return a.dd<b.dd;
}

void init()  // 初始化; 
{
	int i,j;
	for(i=1;i<=n;i++)
		f[i] = i;
}

int find(int u) // 幷查集查找 
{
	if(u==f[u])
		return f[u];
	else 
	{
		f[u] = find(f[u]);
		return f[u];
	}
}

double read()
{
	 int i,j;
	 for(i=1;i<=n;i++)
	 	scanf("%lf%lf",&stu1[i].x,&stu1[i].y);
	 int tt=0;
	 for(i=1;i<=n;i++)
	 {
	 	for(j=i+1;j<=n;j++)
	 	{
	 		stu[tt].a = i;
	 		stu[tt].b = j;
	 		stu[tt++].dd = sqrt((stu1[i].x-stu1[j].x)*(stu1[i].x-stu1[j].x)+(stu1[i].y-stu1[j].y)*(stu1[i].y-stu1[j].y));
	 	}
	 }
	 
	 sort(stu,stu+tt,cmp); // 排序 
	 
	 for(i=0;i<tt;i++)
	 {
	 	int t1 = find(stu[i].a);
	 	int t2 = find(stu[i].b);
	 	
	 	if(t1!=t2)
	 	{
	 		if(t1<t2)    // 合并 
	 			f[t2] = t1;
			else f[t1] = t2;
			if(find(1)==find(2))
				return stu[i].dd;
	 	}
	 }
}

int main()
{
	int f=0;
	int kk=1;
	while(~scanf("%d",&n)&&n)
	{
		init();
		printf("Scenario #%d\n",kk++);
		printf("Frog Distance = %.3f\n\n",read());
	}
	return 0;
}

代码二: prim算法

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;

const double pi = 99999999.00;
#define Max 300
int n;
struct node
{
	double x,y;
}stu[Max];

double map[Max][Max];
double dis[Max];
int book[Max];

double prim(int u)    // prim算法 
{
	int i,j;
	for(i=1;i<=n;i++)
		dis[i] = map[u][i];
	memset(book,0,sizeof(book));
	double Maxl = -pi;
	book[u] = 1;
	int t=1;
	while(t<n)
	{
		double tt = pi;
		for(i=1;i<=n;i++)
		{
			if(!book[i]&&tt > dis[i])
			{
				u=i;
				tt = dis[i];
			}		
		}
		book[u] = 1;
		t++;
		if(Maxl<tt)
			Maxl=tt;
		if(u==2)
			break;
		for(i=1;i<=n;i++)
		{
			if(!book[i]&&map[u][i]&&dis[i]>map[u][i])
			{
				 dis[i] = map[u][i]; 
			}
		}
	}
	return Maxl;
	
}

double read()
{
	int i,j;
	for(i=1;i<=n;i++)
		scanf("%lf%lf",&stu[i].x,&stu[i].y);
	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
		{
			map[i][j] = sqrt((stu[i].x-stu[j].x)*(stu[i].x-stu[j].x)+(stu[i].y-stu[j].y)*(stu[i].y-stu[j].y)); 
		}
	return prim(1);
}
int main()
{
	int kk=1;
	while(~scanf("%d",&n)&&n)
	{
		printf("Scenario #%d\n",kk++);
		printf("Frog Distance = %.3f\n\n",read());
	}
	return 0;	
} 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值