【POJ 2253】Frogger(Dijkstra)

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

题目大意

有两只青蛙,分别在两个石头上,青蛙A想要到青蛙B那儿去,他可以直接跳到B的石头上,也可以跳到其他石头上,再从其他石头跳到B那儿,求青蛙从A到B的所有路径中最小的Frog Distance,我们定义Frog Distance为从A到B的一条路径中某次跳的最大距离,例如,如果从A到B某条路径跳的距离是2,5,6,4,则Frog Distance就是6,题目输入的第一行代表石头的个数,当个数为0时结束程序,接着有n行,其中第2,3行分别代表A,B青蛙的坐标,其他n-2行分别代表空的石头的坐标,输出一个小数(保留三位),具体格式参见样例,注意每输出一个答案还要再空一行(包括结束)。

思路

思路还是Dij,不过在更新点的时候要改一改,因为每次只要保留从上一个点调到这一个点的最大距离,所以更新时,如果从当前点跳到这一点的跳跃距离比其他路径中调到这一点的跳跃距离大的话就更新为这个大的值,否则不更新。详细实现看代码和注释

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;
const int maxn=200+5;
const int INF=0x3f3f3f3f;
double map[maxn][maxn],dis[maxn];
bool vis[maxn];
int n;

struct proc
{
    int x,y;
}stone[maxn];

double cal_dis(proc a,proc b)//计算两点之间的距离
{
    double tmp=(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
    return sqrt(tmp);
}

void dij(int s)
{
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++)
    {
        dis[i]=map[i][1];
    }
    dis[s]=0;
    vis[s]=true;
    for(int i=1;i<=n;i++)
    {
        int pos=-1;
        for(int j=1;j<=n;j++)//选择一条跳跃距离最短的一条边,加入边集
        {
            if(!vis[j]&&(pos==-1||dis[j]<dis[pos]))
            {
                pos=j;
            }
        }
        vis[pos]=1;//pos记录了所选边能够到达的另一端点
        for(int j=1;j<=n;j++)
        {
            if(!vis[j])
            {
                double maxx=max(dis[pos],map[pos][j]);
                if(dis[j]>maxx) dis[j]=maxx;//如果从pos跳到这个点的跳跃距离大于目前保存的最大距离,则更新。
            }
        }
    }
}

int main()
{
    int cas=1;
    while(~scanf("%d",&n)&&n)
    {
        memset(map,INF,sizeof(map));
        for(int i=1;i<=n;i++)
        {
            scanf("%d %d",&stone[i].x,&stone[i].y);
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<i;j++)
            {
                map[i][j]=map[j][i]=cal_dis(stone[i],stone[j]);
            }
            map[i][i]=0;
        }
        dij(1);
        printf("Scenario #%d\nFrog Distance = %.3f\n\n",cas++,dis[2]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/iceiceicpc/article/details/52212763