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.

翻译

弗莱迪青蛙坐在湖中央的一块石头上。突然,他注意到坐在另一块石头上的菲奥娜-青蛙。他打算去看她,但由于水很脏,而且充满了游客的防晒霜,他想避免游泳,而是通过跳跃来接近她。
不幸的是,菲奥娜的石头在他的跳跃范围之外。因此,弗莱迪考虑使用其他石头作为中间站,通过一连串的小跳到达她身边。
为了执行一个给定的跳跃序列,青蛙的跳跃范围显然必须至少与该序列中发生的最长的跳跃一样长。
因此,两块石头之间的青蛙距离(人类也称之为最小距离)被定义为两块石头之间所有可能路径的最小必要跳跃范围。

你得到了Freddy的石头、Fiona的石头和湖中所有其他石头的坐标。你的任务是计算Freddy的石头和Fiona的石头之间的青蛙距离。
输入
输入将包含一个或多个测试案例。每个测试案例的第一行将包含石头的数量n(2<=n<=200)。接下来的n行各包含两个整数xi,yi (0 <= xi,yi <= 1000)代表石头#i的坐标。1号石头是Freddy的石头,2号石头是Fiona的石头,其他n-2个石头都没有被占用。每个测试案例后面都有一个空行。输入以n的值为零(0)结束。
输出
对于每个测试案例,打印一行 "Scenario #x "和一行 "Frog Distance = y",其中x由测试案例编号代替(它们从1开始编号),y由适当的实数代替,打印到三位小数。在每个测试案例后放一行空白,甚至在最后一个测试案例后也放一行。

思路

把每两个点的坐标存进数组里, 进行排序, 遍历直到两只青蛙能连在一起, 此时的两点之间距离就是题中要的最短的最长跳跃距离(另外在输出的时候一定注意两个测试用例之间留空行)

代码

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define ll long long
const ll N=1e6+5;
struct node
{
	ll a,b;
	double dis;
}p[N];
ll pre[N];
ll x[N],y[N];
ll find(ll x)
{
	if(x==pre[x])return x;
	return pre[x]=find(pre[x]);
}

bool cmp(node a,node b)
{
	return a.dis<b.dis;
}

double q(ll a,ll b,ll c,ll d)
{
	return sqrt((a-c)*(a-c)+(b-d)*(b-d));
}

int main()
{
	int n;
	int o=1;
	while(cin>>n&&n)
	{
		int ant=0;
		for(int i=1;i<=n;i++)cin>>x[i]>>y[i];
		for(int i=1;i<=n;i++)
		{
			for(int j=i+1;j<=n;j++)
			{
				ant++;
				p[ant].a=i;
				p[ant].b=j;
				p[ant].dis=q(x[i],y[i],x[j],y[j]);
			}
		}
		sort(p+1,p+1+ant,cmp);
		for(int i=1;i<=n;i++)pre[i]=i;
		for(int i=1;i<=ant;i++)
		{
			ll a=find(p[i].a);
			ll b=find(p[i].b);
			if(a!=b)
			{
				pre[a]=b;
				if(find(1)==find(2))
				{
					printf("Scenario #%d\n",o++);
					printf("Frog Distance = %.3f\n\n",p[i].dis);
					break;
				}
			}
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值