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
这道题困了我很长时间,很重要的一点是最开始的时候没想清楚就开始写了,结果题意都没搞明白,其实就是求 青蛙要到达目标点,它至少要跳多远,方法是,用类似floyd算法 ,一个点一个点进行判断,优化结果。
/*
2
0 0
3 4
3
17 4
19 4
18 5
0
*/
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
struct Node{
int x,y;
}a[210];
double res[210][210];
double dist(Node aa,Node bb){
double x2=aa.x-bb.x;
double y2=aa.y-bb.y;
return sqrt(x2*x2+y2*y2);
}
int main()
{
int n;
int num=0;
while(++num){
cin>>n;
if(n==0) break;
for(int i=0;i<n;i++){
cin>>a[i].x>>a[i].y;
}
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++){
res[i][j]=res[j][i]=dist(a[i],a[j]);
}
//两点间距离算好了
for(int k=0;k<n;k++)//枚举
for(int j=0;j<n;j++)
for(int i=0;i<n;i++){
res[i][j]=min(res[i][j],max(res[i][k],res[k][j]));
}
printf("Scenario #%d\nFrog Distance = %.3f\n\n",num,res[0][1]);
}
return 0;
}
后来听某大佬说用最小生成树做,我就试了一下。其实就是类似prim 算法,用result记录结果,每选择离 已选择的点 最近的点,然后更新result。
/*
2
0 0
3 4
3
17 4
19 4
18 5
0
*/
#include<cstring>
#include<cmath>
#include<cstdio>
#include<iostream>
using namespace std;
struct Node{
int x,y;
}a[210];//怎么初始化
double dis[210];
int vis[210];
double result=0;
double dist(Node a,Node b){
return sqrt( (a.x-b.x) * (a.x-b.x) + (a.y-b.y) * (a.y-b.y));
}
int main()
{
int n=1;
while(n){
int t;
cin>>t;
if(t==0) break;
result=0;
for(int i=0;i<t;i++){
cin>>a[i].x>>a[i].y;
dis[i]=111111111111;
}
//输入完成
memset(vis,0,sizeof(vis));
int add=0,previous=-1;//加入点,上一个点
while(add!=1){
vis[add]=1;
for(int i=1;i<t;i++)
{
dis[i]=min( dis[i] , dist(a[i],a[add]) );
}
int minid=-1;
double minnum=0x3f3f3f3f;
for(int i=1;i<t;i++){
if(vis[i]==1) continue;
if( dis[i] < minnum ){
minid=i;
minnum=dis[i];
}
}//找到最短的加入点
result=max(result,minnum);//更新结果
previous=add;
add=minid;
}
printf("Scenario #%d\nFrog Distance = %.3f\n\n",n++,result);
}
return 0;
}