Frogger
Description
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 Source |
提示
题意:
弗莱迪青蛙坐在湖中的大石头上,突然它注意到菲奥娜青蛙坐在另一个石头上。它想去搭(biao)讪(bai),但是水太脏了,水里充满了游客的防晒霜,它想用跳的方式来代替游泳。
不幸的是它一口气跳不过去,需要借助旁边的石头。那么,我们定义frog distance为弗莱迪所跳到菲奥娜的路径中最长边里面最短的那一跳,请你求出frog distance为多少?(保证连通)
思路:
最短路变形,我一看慌了,不会啊,还是只有去百度。
d[i]到d[j],用max(d[i],map[i][j])去更新d[j]。
示例程序
第一个是SPFA的做法,第二个是Floyd的做法,比较直观:
Source Code
Problem: 2253 Code Length: 1409B
Memory: 956K Time: 0MS
Language: GCC Result: Accepted
#include <stdio.h>
#include <math.h>
#define MAX 1000000007
struct
{
int x,y;
}stone[200];
double map[200][200];
double Max(double a,double b)
{
if(a>b)
{
return a;
}
else
{
return b;
}
}
double spfa(int n)
{
int q[80000],v[200],i,f=0,top=0;
double d[200],max;
for(i=0;n>i;i++)
{
v[i]=0;
d[i]=MAX;
}
v[0]=1;
d[0]=0;
q[top]=0;
top++;
while(f<top)
{
v[q[f]]=0;
for(i=0;n>i;i++)
{
max=Max(d[q[f]],map[q[f]][i]);
if(d[i]>max)
{
d[i]=max;
if(v[i]==0)
{
v[i]=1;
q[top]=i;
top++;
}
}
}
f++;
}
return d[1];
}
int main()
{
int n,i,i1,i2;
scanf("%d",&n);
for(i=1;n!=0;i++)
{
for(i1=0;n>i1;i1++)
{
scanf("%d %d",&stone[i1].x,&stone[i1].y);
}
for(i1=0;n>i1;i1++)
{
for(i2=0;n>i2;i2++)
{
map[i1][i2]=sqrt((stone[i1].x-stone[i2].x)*(stone[i1].x-stone[i2].x)+(stone[i1].y-stone[i2].y)*(stone[i1].y-stone[i2].y));
map[i2][i1]=map[i1][i2];
}
}
printf("Scenario #%d\nFrog Distance = %.3f\n\n",i,spfa(n)); //请注意在POJ交题遇到double,最好还是用%f去做输出
scanf("%d",&n);
}
return 0;
}
Source Code
Problem: 2253 Code Length: 1215B
Memory: 712K Time: 32MS
Language: GCC Result: Accepted
#include <stdio.h>
#include <math.h>
#define MAX 1000000007
struct
{
int x,y;
}st[200];
double map[200][200];
double max(double x,double y)
{
if(x>y)
{
return x;
}
else
{
return y;
}
}
double min(double x,double y)
{
if(x<y)
{
return x;
}
else
{
return y;
}
}
double flody(int n)
{
int i,i1,i2;
double s;
for(i=0;n>i;i++)
{
for(i1=0;n>i1;i1++)
{
for(i2=0;n>i2;i2++)
{
s=max(map[i1][i],map[i][i2]); //因为两两之间都是连通的,直接按照题意去比较就完了
map[i1][i2]=min(s,map[i1][i2]);
}
}
}
return map[0][1];
}
int main()
{
int n,i,j,i1;
scanf("%d",&n);
for(j=1;n!=0;j++)
{
for(i=0;n>i;i++)
{
scanf("%d %d",&st[i].x,&st[i].y);
}
for(i=0;n>i;i++)
{
for(i1=0;n>i1;i1++)
{
map[i][i1]=sqrt((st[i].x-st[i1].x)*(st[i].x-st[i1].x)+(st[i].y-st[i1].y)*(st[i].y-st[i1].y));
map[i1][i]=map[i][i1];
}
}
printf("Scenario #%d\n",j);
printf("Frog Distance = %.3f\n\n",flody(n));
scanf("%d",&n);
}
return 0;
}