Frogger UVA 534(最小瓶颈路+floyd算法)

来源:互联网 发布:软件是怎么设计的 编辑:程序博客网 时间:2024/05/02 03:48



 Frogger

From:UVA, 534

Submit

Time Limit: 3000 MS

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 Specification 

The input file will contain one or more test cases. The first line of each test case will contain the number of stones n ( $2 \le n \le 200$). The next n lines each contain two integers xiyi ( $0 \le x_i,y_i \le 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 Specification 

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 

20 03 4317 419 418 50

Sample Output 

Scenario #1Frog Distance = 5.000Scenario #2Frog Distance = 1.414

 题目大意:

有两只青蛙,在两块不同的石头上,有一只想要去拜访另一只,要求通过它石头进行跳跃然后在一起呀在一起,可想而知,由于石头有很多,他们中间的路径也有很多,现在要求求一个长度:从每条路径里面挑出那步跨越最大的,然后从这些跨越最大的里面挑出最小的。

解题思路:

              Floyd-warshall算法。


代码:

#include<iostream>#include<cstdio>#include<cmath>using namespace std;struct node{    int x,y;    node(int x0=0,int y0=0){x=x0,y=y0;}}stone[1<<9];int n,t=0;double dis[1<<9][1<<9],l,r;//快bool con[1<<9][1<<9];void input(){    for(int i=0;i<n;i++) cin>>stone[i].x>>stone[i].y;}double distanse(node a,node b){    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}void solve(){    l=0,r=1e5;    for(int i=0;i<n;i++){        for(int j=0;j<n;j++){            dis[i][j]=distanse(stone[i],stone[j]);        }    }    while(r-l>=1e-5){//写成这种形式的        double mid=(l+r)/2;        for(int i=0;i<n;i++){            for(int j=0;j<n;j++){                if(dis[i][j]>mid) con[i][j]=false;//在原图中删去所有边长大于mid的边。                else con[i][j]=true;            }        }        for(int k=0;k<n;k++){            for(int i=0;i<n;i++){                for(int j=0;j<n;j++){                    con[i][j] |= con[i][k]&con[k][j];//在边长不超过mid的情况下计算可达的标志con矩阵。                }            }        }        if(con[0][1]) r=mid;//若节点0(起点)到1(终点)的边长不超过mid,则最长边的最小值在左区间        else l=mid;//否则在右区间    }}void outResult(){    printf("Scenario #%d\nFrog Distance = %.3lf\n\n",++t,r);}int main(){    while(cin>>n&&n){        input();        solve();        outResult();    }    return 0;}





0 0
原创粉丝点击