A

来源:互联网 发布:黑色沙漠捏脸数据下载 编辑:程序博客网 时间:2024/06/07 15:52

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

方法一:
并查集+贪心
并查集判断青蛙是否可以到达公主那,贪心的就是先走最短的,一旦可以到达公主,说明最后一个就是青蛙一次需要跳的最远距离。

代码如下:

#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>using namespace std;const int maxn=100000;struct node{    int x,y;    double w;}edge[maxn];int cmp(node aa,node bb){    return aa.w<bb.w;}int pre[maxn],head[maxn],vis[maxn];struct nodee{    int x,y;}stone[maxn*2];int findd(int x){    int r=x;    while(r!=pre[r])        r=pre[r];    int i=x,j;    while(i!=r)    {        j=pre[i];        pre[i]=r;        i=j;    }    return r;}void bing(int x,int y){    int tx,ty;    tx=findd(x);    ty=findd(y);    if(tx!=ty)    {        pre[ty]=tx;    }}int main (){    int n;    int cntt=0;    while(scanf("%d",&n),n)    {           ++cntt;        int i;        for(i=0;i<=2000;i++)        {            pre[i]=i;        }        memset(vis,0,sizeof(vis));        memset(head,-1,sizeof(head));        for(i=1;i<=n;i++)        {            cin>>stone[i].x>>stone[i].y;        }        int j;        int ans=1;        for(i=1;i<=n;i++)        {            for(j=i;j<=n;j++)            {                edge[ans].x=i;                edge[ans].y=j;                edge[ans++].w=sqrt((stone[i].x-stone[j].x)*(stone[i].x-stone[j].x)*1.0+(stone[i].y-stone[j].y)*(stone[i].y-stone[j].y)*1.0);            }        }        sort(edge+1,edge+ans+1,cmp);        for(i=1;i<=ans;i++)        {            bing(edge[i].x,edge[i].y);            if(findd(1)==findd(2))            {                printf("Scenario #%d\n",cntt);                printf("Frog Distance = %.3f\n\n",edge[i].w);                break;            }        }    }}

方法二:
并查集+二分
并查集一样的用途,只是我们来二分最大距离,在这个最大距离的下能不能到达,如果能到达,则继续缩小最大距离,否则就增大最大距离。
代码如下:

#include<iostream>#include<cmath>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=100000;int pre[maxn],vis[maxn];struct edge{    int u,v;    double w;}edge[maxn*3];struct node{    int x,y;}a[maxn*3];int findd(int x){    int r=x;    while(r!=pre[r])        r=pre[r];    int i=x,j;    while(i!=r)    {        j=pre[i];        pre[i]=r;        i=j;    }    return r;}void bing(int x,int y){    int tx,ty;    tx=findd(x);    ty=findd(y);    if(tx!=ty)        pre[ty]=tx;}int cnt;int solve(double s){    int i;    for(i=1;i<=2000;i++)    {        pre[i]=i;    }    for(i=1;i<=cnt;i++)    {        if(edge[i].w<=s)        {            bing(edge[i].u,edge[i].v);        }        if(findd(1)==findd(2))        {            return 1;        }    }    return 0;}int main (){    int n;    int aaa=0;    while(scanf("%d",&n),n)    {        aaa++;        int i,j;        for(i=1;i<=n;i++)        {            cin>>a[i].x>>a[i].y;        }        cnt=1;        for(i=1;i<=n;i++)        {            for(j=i+1;j<=n;j++)            {                edge[cnt].u=i;                edge[cnt].v=j;                edge[cnt++].w=sqrt((a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y));                //cout<<i<<" "<<j<<" "<<edge[cnt-1].w<<endl;            }        }        double l=0.0,r=2000.0;        double ans=0;        for(i=1;i<=2000;i++)        {            pre[i]=i;        }        while(r-l>=1e-5)        {            double mid=(l+r)*1.0/2;            //printf("%.3f\n",mid);            if(solve(mid))            {                ans=mid;                r=mid;                //printf("%f %f\n",l,r);            }            else            {                l=mid;            }        }        printf("Scenario #%d\n",aaa);        printf("Frog Distance = %.3f\n\n",ans);    }} 
原创粉丝点击