POJ 2253 Frogger(最短路变形,floyd算法)

Frogger
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 48119 Accepted: 15310

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

Source


题目大意:

给出两只青蛙的坐标A、B,和其他的n-2个坐标,任一两个坐标点间都是双向连通的。显然从A到B存在至少一条的通路,每一条通路的元素都是这条通路中前后两个点的距离,这些距离中又有一个最大距离。

现在要求求出所有通路的最大距离,并把这些最大距离作比较,把最小的一个最大距离作为青蛙的最小跳远距离。

 

Floyd算法

用Floyd算法求出两两最短路,再求出从每个点开始的最长路,最后从这n个最长路中求出最小的那个即为所求。


也就是说,给一个图,求给出的两个节点之间,所有路径中权值最大的最小值(就是取出每一条路径里的权值最大值,然后答案就是这些最大值的最小值,有些拗口,注意一下)。可以写为road[i][j]=road[j][i]=min(road[i][j],max(road[i][k],road[k][j]));



代码没什么好难理解的,主要是题意要理解:

#include<iostream>  
#include<math.h>  
#include<iomanip>  
using namespace std;  
//给一个图,求给出的两个节点之间,所有路径中权值最大的最小值(就是取出每一条路径里的权值最大值,然后答案就是这些最大值的最小值,有些拗口,注意一下)  
class coordinate  //每个石头的结构体
{  
public:  
    double x,y;  
}point[201];  
  
double path[201][201];   //两点间的权值  
  
int main(void)  
{  
    int i,j,k;  
  
    int cases=1;  //cases代表测试数据数
    while(cases)  
    {  
        /*Read in*/  
  
        int n;   //numbers of stones;  
        cin>>n;  
        if(!n)break;  
  
        for(i=1;i<=n;i++)  
            cin>>point[i].x>>point[i].y;  
  
        /*Compute the weights of any two points*/  //计算任意两点的距离,存入path中
  
        for(i=1;i<=n-1;i++)  
            for(j=i+1;j<=n;j++)  
            {  
                double x2=point[i].x-point[j].x;  
                double y2=point[i].y-point[j].y;  
                path[i][j]=path[j][i]=sqrt(x2*x2+y2*y2);  //双向性  
            }  
  
        /*Floyd Algorithm*/  //floyd算法
  
        for(k=1;k<=n;k++)    //k点是第3点  
            for(i=1;i<=n-1;i++)    //主要针对由i到j的松弛,最终任意两点间的权值都会被分别松弛为最大跳的最小(但每个两点的最小不一定相同)  
                for(j=i+1;j<=n;j++)  
                    if(path[i][k]<path[i][j] && path[k][j]<path[i][j])    //当边ik,kj的权值都小于ij时,则走i->k->j路线,否则走i->j路线  
                        if(path[i][k]<path[k][j])               //当走i->k->j路线时,选择max{ik,kj},只有选择最大跳才能保证连通  
                            path[i][j]=path[j][i]=path[k][j];  
                        else  
                            path[i][j]=path[j][i]=path[i][k];  
  
        cout<<"Scenario #"<<cases++<<endl;  
        cout<<fixed<<setprecision(3)<<"Frog Distance = "<<path[1][2]<<endl;  
        //fixed用固定的小数点位数来显示浮点数(包括小数位全为0)  
        //setprecision(3)设置小数位数为3  
        cout<<endl;  
    }  
    return 0;  
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值