POJ 2253-Frogger(最短路的变形)

Frogger
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 55496 Accepted: 17492


题目链接:点击打开链接


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 那去玩,然而可能有时候他们俩蹲的石头太远,青蛙 A 没办法跳过去,所以他会借助于别的石头来达到目的地,现在定义一个青蛙距离,青蛙距离是指在青蛙 A 到达青蛙 B 所选路径最大的弹跳距离,我们需要尽可能的让这个最大的弹跳距离荆轲能的小,例如:

青蛙 A 到 青蛙 B 现在有两条路径可选:

第一条路线是借助中间 2 个石头,那么需要弹跳三次,这三次的距离分别是:2——3——4

第一条路线是借助中间 2个石头,那么需要弹跳四次,这三次的距离分别是:1——1——1——5,

那么我们肯定选择第一条路径,因为第一条路径它所需要的最大弹跳距离是 4 ,而第二条路径所需要的最大弹跳距离是 5.

现在给出石头的位置坐标,前两个坐标是青蛙 A 和青蛙 B 的位置,后面如果还列出有坐标的话就是中介石头,题意就是酱紫的,理解了吧!


分析:

本题昨天写的,但是找 bug 却找到今天,原因就是我写的时候想坐标计算位置肯定不免出现浮点小数的距离,所以结构体总的变量就设置为 double 类型的,村坐标的时候就是以%lf存入的,感觉这样应该应该让精度更准确,但是哇,一直 wa ,于是才将左边改回来了。按照 int 类型存入,在计算距离的时候用(double)强制转换一下,于是这样竟然对了,感觉用 double 也是对的呀!看到这篇博文的宝宝能不能为我解答一下。

本题是最短路的变形,我用的是迪杰斯特拉变形写的,当然还有很多别的方式,在这里就不一一分享了,本题也是求单源点的距离,起始点和终点,我们都很明确,所以哇,想想用迪杰斯特拉写最短路的时候,我们的更新路径是不是用所找到的没被访问点中距离起始点最近的点来更新其他没有被访问过的点,而本题是让求路径中的最大弹跳距离的最小值,假设我们有三个点 A , B , C , A 是其实点,B 是终点,A 到 B 的距离是 5,A 到 C 的距离是 3,C 到 B 的距离是 4,那么如果 dis 中存的是其他所有点到A 的路径的最大弹跳力,是不是 dis[B]=min(dis[B],max(dis[C],s[C][B])); S 数组存的是任意两块石头的距离,好了,看看代码实现吧!

#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string>
#include<string.h>
using namespace std;
#define INF 0x3f3f3f3f
int n,m=0;
double h[205][205];
struct book{
int x,y;
}s[205];
double check(struct book a,struct book b)///求两点之间的距离
{
    return (double)sqrt((double)(a.x-b.x)*(a.x-b.x)+(double)(a.y-b.y)*(a.y-b.y));
}

void Dijkstra()
{
    int vis[205],bj;
    double dis[205],minx;
    for(int i=0;i<n;i++)
    {
        dis[i]=h[0][i];
        vis[i]=0;
    }
    vis[0]=1;
    bj=0;
    for(int i=1;i<n;i++)
    {
        minx=INF;
        for(int j=0;j<n;j++)
        {
            if(!vis[j]&&minx>dis[j])
            {
                bj=j;
                minx=dis[j];
            }
        }
        vis[bj]=1;
        for(int j=0;j<n;j++)///更新
        {
            dis[j]=min(dis[j],max(dis[bj],h[bj][j]));
        }
    }
    printf("Scenario #%d\n",++m);
    printf("Frog Distance = %.3f\n",dis[1]);
    printf("\n");
}

int main()
{
    while(~scanf("%d",&n)&&n)
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d %d",&s[i].x,&s[i].y);
        }
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                h[i][j]=h[j][i]=check(s[i],s[j]);
            }
        }
        Dijkstra();
    }
    return 0;
}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值