Frogger (poj 2253)
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
题目翻译:
弗雷迪青蛙正坐在湖中央的一块石头上。突然,他注意到菲奥娜·青蛙正坐在另一块石头上。他打算去看她,但由于水很脏,而且到处都是游客的防晒霜,他想避免游泳,而是跳着去够她。
不幸的是菲奥娜的石头超出了他的跳跃范围。因此,Freddy考虑使用其他石头作为中间停站,通过一系列的小跳跃到达她的位置。
要执行给定的跳转序列,青蛙的跳转范围显然必须至少与序列中发生的最长跳转一样长。
因此,两块石头之间的青蛙距离(人类也称之为极小极大距离)被定义为两块石头之间所有可能路径上的最小必要跳跃距离。
给你弗雷迪的石头,菲奥娜的石头和湖里所有其他石头的坐标。你的工作是计算弗雷迪和菲奥娜石头之间的青蛙距离。
输入
输入将包含一个或多个测试用例。每个测试用例的第一行将包含石头的数量n (2<=n<=200)。接下来的n行每个包含两个整数习,yi(0 <=习,yi <= 1000),表示stone #i的坐标。石头1号是弗雷迪的石头,石头2号是菲奥娜的石头,其他n-2块石头没有人。每个测试用例后面都有一个空行。输入被n的0(0)值终止。
输出
对于每个测试用例,打印一行“Scenario #x”和一行“Frog Distance = y”,其中x替换为测试用例号(它们的编号从1开始),y替换为适当的实数,打印为三个小数。在每个测试用例之后,甚至在最后一个测试用例之后,都放一行空白。
样例输入
2
0 0
3 4
3
17 4
19 4
18 5
0
样例输出
场景# 1
青蛙距离= 5.000
场景# 2
青蛙距离= 1.414
思路:复制一下别人的题意,有两只青蛙和若干块石头,现在已知这些东西的坐标,两只青蛙A坐标和青蛙B坐标是第一个和第二个坐标,现在A青蛙想要到B青蛙那里去,并且A青蛙可以借助任意石头的跳跃,而从A到B有若干通路,问从A到B的所有通路上的最大边,比如有 有两条通路 1(4)5 (3)2 代表1到5之间的边为4, 5到2之间的边为3,那么该条通路跳跃范围(两块石头之间的最大距离)为 4, 另一条通路 1(6) 4(1) 2 ,该条通路的跳跃范围为6, 两条通路的跳跃范围分别是 4 ,6,我们要求的就是最小的那一个跳跃范围,即4,用二种方法都能解决。
AC代码(Floyd):
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#define MAX 1010
using namespace std;
double dp[MAX][MAX];
int x[MAX],y[MAX];
int main()
{
int n,i,j,k;
int a=1;
while(~scanf("%d",&n),n)
{
memset(dp,MAX,sizeof(dp));
for(i=1; i<=n; i++)
scanf("%d%d",&x[i],&y[i]);
for(i=1; i<=n; i++)
for(j=i+1; j<=n; j++)
dp[i][j]=dp[j][i]=sqrt(double(x[i]-x[j])*(x[i]-x[j])+double(y[i]-y[j])*(y[i]-y[j]));
for(k=1; k<=n; k++)
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
dp[i][j]=min(dp[i][j],max(dp[i][k],dp[k][j]));
printf("Scenario #%d\nFrog Distance = %.3lf\n\n",a++,dp[1][2]);
}
return 0;
}
AC代码(Dijkstra):
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#define MAX 1010
#define inf 0x3f3f3f3f
using namespace std;
double dp[MAX][MAX];
double dis[MAX];
int vis[MAX];
int x[MAX],y[MAX];
int main()
{
int n,i,j,k,a=1;
while(~scanf("%d",&n),n)
{
memset(dp,inf,sizeof(dp));
memset(vis,0,sizeof(vis));
for(i=1; i<=n; i++)
scanf("%d%d",&x[i],&y[i]);
for(i=1; i<=n; i++)
for(j=i+1; j<=n; j++)
dp[i][j]=dp[j][i]=sqrt(double(x[i]-x[j])*(x[i]-x[j])+double(y[i]-y[j])*(y[i]-y[j]));
for(i=1; i<=n; i++)
{
dis[i]=inf;
dis[1]=0;
}
for(i=1; i<=n; i++)
{
int minn=inf;
for(j=1; j<=n; j++)
{
if(vis[j]==0 && dis[j]<minn)
{
k=j;
minn=dis[j];
}
}
vis[k]=1;
for(j=1; j<=n; j++)
dis[j]=min(dis[j],max(dis[k],dp[k][j]));
}
printf("Scenario #%d\nFrog Distance = %.3lf\n\n",a++,dis[2]);
}
return 0;
}