Frogger POJ

来源:互联网 发布:佳园软件 编辑:程序博客网 时间:2024/05/16 15:58
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
20 03 4317 419 418 50
Sample Output
Scenario #1Frog Distance = 5.000Scenario #2Frog Distance = 1.414

题目说的是找一条路,不用路径最短,只需要在这条路径上frog跳的最大距离最小即可。

第一种: 最小生成树

记录下任意两点之间的距离,然后按照距离排序,然后构造最小生成树,每次添加完一条边之后看看1和2是不是在同一棵树里面了,如果是的话,这条最后加的边就是

最大的距离,因为我们排序了么

#include <stdio.h>#include<string.h>#include<algorithm>#include<cmath>using namespace std;typedef long long ll;const int INF=0x3f3f3f3f;struct stone{    int x,y;}s[210];int pre[210];struct Edge{    int u,v;    double dis;}edge[40010];int find_anc(int x){    return pre[x]==x?x:pre[x]=find_anc(pre[x]);}int cmp(Edge a,Edge b){    return a.dis<b.dis;}int main(){    int n;    int T=1;    while(~scanf("%d",&n))    {        if(!n)            break;        for(int i=1;i<=n;i++)        {            scanf("%d %d",&s[i].x,&s[i].y);            pre[i]=i;        }        int cnt=0;        for(int i=1;i<=n;i++)            for(int j=i+1;j<=n;j++)        {            edge[cnt].u=i,edge[cnt].v=j;            int x1=s[i].x,y1=s[i].y,x2=s[j].x,y2=s[j].y;            double dis = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2);            edge[cnt++].dis=dis;        }        sort(edge,edge+cnt,cmp);        double ans;        for(int i=0;i<cnt;i++)        {            int u=edge[i].u,v=edge[i].v;            int fu=find_anc(u),fv=find_anc(v);            if(fu!=fv)            {                pre[fu]=fv;                if(find_anc(1)==find_anc(2))                {                    ans=edge[i].dis;                    break;                }            }        }        printf("Scenario #%d\n",T++);        printf("Frog Distance = %.3f\n\n",sqrt(ans));    }    return 0;}

第二种,dijstra

就是不断的找路径中最小步的松弛点,看看能不能使当前两点的路径中的最大步变短。

#include <stdio.h>#include<algorithm>#include <math.h>#include<cstring>using namespace std;double graph[201][201];int pos[201][3];int vis[210];const double INF = 1e20;double dis[210];int n,T=1;void dijstra(){    memset(vis,0,sizeof(vis));    for(int i=1;i<=n;i++)        dis[i]=INF;    dis[1]=0;    for(int i=1;i<=n;i++)    {        double min_dis=INF;        int min_id;        for(int j=1;j<=n;j++)        {            if(!vis[j]&&dis[j]<min_dis)            {                min_dis=dis[j];                min_id=j;            }        }        vis[min_id]=1;        for(int j=1;j<=n;j++)        {            if(dis[j]>dis[min_id]&&dis[j]>graph[min_id][j])                dis[j]=max(dis[min_id],graph[min_id][j]);        }    }}int main(){    while(~scanf("%d",&n)&&n)    {     for (int i=1;i<=n;i++)        scanf("%d %d",&pos[i][1],&pos[i][2]);    for (int i=1;i<=n-1;i++)    {        for (int j=i+1;j<=n;j++)            graph[i][j]=graph[j][i]=sqrt(((pos[i][1]-pos[j][1])*(pos[i][1]-pos[j][1]))+((pos[i][2]-pos[j][2])*(pos[i][2]-pos[j][2])));    }    dijstra();    printf("Scenario #%d\n",T++);    printf("Frog Distance = %.3f\n\n",dis[2]);   }}


第三种folyd,思路同上,只不过他松弛了所有的点

#include <stdio.h>#include <math.h>using namespace std;double a[201][201];int b[201][3];int main(){    int n,o=1;    while(~scanf("%d",&n)&&n)    {     for (int i=1;i<=n;i++)        scanf("%d %d",&b[i][1],&b[i][2]);    for (int i=1;i<=n-1;i++)    {        for (int j=i+1;j<=n;j++)            a[i][j]=a[j][i]=sqrt(((b[i][1]-b[j][1])*(b[i][1]-b[j][1]))+((b[i][2]-b[j][2])*(b[i][2]-b[j][2])));    }    for (int k=1;k<=n;k++)//我们不是在找最短路径,我们是在找可行路上的最大值        for (int i=1;i<=n;i++)            for (int j=1;j<=n;j++)            {                if (a[i][j]>a[i][k]&&a[i][j]>a[k][j])//也就是说我们当前的松弛点,可以经过当前的松弛点使最大距离变短,我们就去更新                {                    if (a[i][k]>a[k][j])                        a[i][j]=a[j][i]=a[i][k];                    else                        a[i][j]=a[j][i]=a[k][j];                }            }    printf("Scenario #%d\n",o++);    printf("Frog Distance = %.3f\n\n",a[1][2]);   }}


原创粉丝点击