POJ 2253 最短路的简单变形

本文介绍了一个有趣的编程问题——青蛙距离问题。该问题要求计算两块石头之间的最小必要跳跃范围,即所谓的青蛙距离。通过使用Dijkstra算法的一个变种,文章详细解释了如何找到这一距离,并提供了具体的实现代码。

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

这是一道最短的简单变形,题意是求从1到2 找到一条边,它是其中一个路径的最大边,又是所有路径的最大边里面最小的一条。dijkstra算法的简单变形。。笨笨的我又没做出来,但是看了题解后梳理了一遍dijkstra算法,dijkstra算法就是从一个点开始,每次找到一条离它最近的点而且这个点是没有做过的,然后走到这个点后对所以别的点进行收缩(走过的当然不会收缩,它又不往回走)。而在这条路上,d[]不是代表着从起点到终点的最短路径,而是当前走的这条路径里面最大的那条边。
if(!vis[j]&&d[j]>max(d[u],distance(s[u],s[j]))) d[j]=max(d[u],distance(s[u],s[j]));
想想画画就明白了。但是为什么它要求当前这个路径里面的最大边还是可以用dijkstra算法,因为,虽然是整合,但其实它每一轮都是一条路径,也就是说它的路径其实又是独立的存在,所以它每次求的都是当前路径的最大,然后遇到下一条路径的最大比以前路径的最大小,就更新。
而本来首先应该判的是每次找到离原点最近的点,由于d的意义改变,它也变成了找以前路径里面最小而且没有走过的这样可以保证找到的路径是当前路径的最大,但相对于全部路径来说,这个最大路径最有可能是最小的,当走到u==2就跳出,因为已经不可能再回到这个点了,这个点里面保存的最小的大路径已经求出。
if(!vis[j]&&mini>d[j])
mini=d[u=j];

还有一个需要自己注意的问题就是qsort用于整形的时候一定要记得转换类型,c可以这样编译通过,c++不行,需要转型。因为这个wa了好久

#include <stdio.h>  
#include <math.h>  
#define inf 1e8  
#define N 210  
#define max(a,b) (a>b?a:b)
int vis[N];
double d[N];
int n;
typedef struct{  
    int x,y;  
}node; 
node s[N];

double distance(node a,node b){  
    return sqrt((float)((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)));  
}  

void dijkstra()
{
    for(int i=1;i<=n;i++) {d[i]=inf;vis[i]=0;}
    d[1]=0;
    for(int i=1;i<=n;i++) 
    {
        int u;
        double mini=inf;
        for(int j=1;j<=n;j++)
        {
            if(!vis[j]&&mini>d[j])
                mini=d[u=j];
        }
        vis[u]=1;
        if(u==2) break;
        for(int j=1;j<=n;j++)
        {
            if(!vis[j]&&d[j]>max(d[u],distance(s[u],s[j]))) d[j]=max(d[u],distance(s[u],s[j]));
        }
    }
}
int main()
{
    int cnt=1;
    while(scanf("%d",&n)!=EOF&&n)
    {
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&s[i].x,&s[i].y);
    }
    dijkstra();
        printf("Scenario #%d\nFrog Distance = %.3lf\n\n",cnt++,d[2]);  
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值