POJ-2253-Frogger(弗洛伊德 迪杰斯特拉)

来源:互联网 发布:导航端口修改工具 编辑:程序博客网 时间:2024/06/06 18:35

Frogger
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 35750 Accepted: 11505
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

题意:多组数据,每组数据第一行给出N,代表接着有N组坐标,第一个坐标是起点,第二个坐标是终点,通过中转,输出起点到终点过程中最小距离的最大值。

思路:迪杰斯特拉或者弗洛伊德均可,求最短路改成求最短路的最大值即可。

代码 弗洛伊德(79ms)

#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>#include<math.h>#include<queue>#include<iomanip>using namespace std;//最小距离中的最大值const int maxn=205;double map[maxn][maxn];int N;void Floyd(){    for(int k=1; k<=N; k++)        for(int i=1; i<=N; i++)            for(int j=1; j<=N; j++)                if(map[i][j]>map[i][k]&&map[i][j]>map[k][j])                    map[i][j]=map[j][i]=max(map[i][k],map[k][j]);}int main(){    int casen=1;    while(~scanf("%d",&N)&&N)    {        double x[maxn];        double y[maxn];        for(int i=1; i<=N; i++)        {            scanf("%lf%lf",&x[i],&y[i]);            for(int j=1; j<i; j++)                map[i][j]=map[j][i]=(double)sqrt((x[j]-x[i])*(x[j]-x[i])+(y[j]-y[i])*(y[j]-y[i]));        }        Floyd();        printf("Scenario #%d\n",casen++);//        printf("Frog Distance = %.3lf\n\n",map[1][2]);        cout<<fixed<<setprecision(3)<<"Frog Distance = "<<map[1][2]<<endl<<endl;    }    return 0;}

代码 迪杰斯特拉(0ms)

#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>#include<math.h>#include<queue>#include<iomanip>using namespace std;//最小距离中的最大值//迪杰斯特拉版本const int maxn=205;const double INF=9999999.9;double map[maxn][maxn];double dis[maxn];//记录起点到各点最小距离的最大值bool vis[maxn];int N;void Dijkstra(int star){    for(int i=1; i<=N; i++)    {        dis[i]=map[i][star];        vis[i]=0;    }    vis[star]=1;    for(int i=1; i<N; i++)    {        double minn=INF;        int point;        for(int j=1; j<=N; j++)            if(vis[j]==0&&dis[j]<minn)            {                minn=dis[j];                point=j;            }        if(minn==INF)            return;        vis[point]=1;        for(int j=1; j<=N; j++)            if(dis[j]<INF&&dis[point]<dis[j]&&map[point][j]<dis[j])                dis[j]=max(dis[point],map[point][j]);    }}int main(){    int casen=1;    while(~scanf("%d",&N)&&N)    {        for(int i=1; i<=N; i++)            for(int j=1; j<=N; j++)                i==j?map[i][j]=0:map[i][j]=INF;        double x[maxn];        double y[maxn];        for(int i=1; i<=N; i++)        {            scanf("%lf%lf",&x[i],&y[i]);            for(int j=1; j<i; j++)                map[i][j]=map[j][i]=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));        }        Dijkstra(1);//1到各点最小距离最大值        printf("Scenario #%d\n",casen++);        cout<<fixed<<setprecision(3)<<"Frog Distance = "<<dis[2]<<endl<<endl;    }    return 0;}
0 0