ACM: 图论题 poj 2253 (poj上的dou…

                                                                             Frogger

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

题意:  Freddy青蛙想跳到Fiona青蛙所在的石头.通过其他石头跳到目的地.
           找出每一步跳跃中最大的距离.但是在所有跳跃的paths总和是最小的.

解题思路:
           1. x,y轴上的点,要进行建图.求每两个点之间的距离.
           2. 既然要最小中求最大的(题目要求),采取floyd算法进行迭代.(第一个思路AC 47MS)
           3. 需要改动的是,记录的不再是路径的最短值.
         当 edges[i][k] < edges[i][j] && edges[k][j] < edges[i][j] 条件满足.
         记录下 max(edges[i][k] , edges[k][j]) 中大的那个值. 最后得出edges[0][1]就是求解的结果.
           (因为输入时候,第0个石头到第1个石头分别是Freddy青蛙和Fiona青蛙所在的石头)

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;

#define MAX 350

int info[MAX][2];
int edges[MAX][MAX];
int n;

int dist(int a1,int b1,int a2,int b2)
{
    return (a1-a2)*(a1-a2) + (b1-b2)*(b1-b2);
}

int max(int a,int b)
{
    return a>b ? a : b;
}

void read_graph()
{
    int i , j;
    for(i = 0; i < n; ++i)
    {
        scanf("%d %d",&info[i][0],&info[i][1]);
    }
    
    for(i = 0; i < n; ++i)
    {
        for(j = 0; j < n; ++j)
        {
            edges[i][j] = dist(info[i][0],info[i][1],info[j][0],info[j][1]);
        }
    }
}


void floyd()
{
    for(int k = 0; k <  n; ++k)
    {
        for(int i = 0; i < n; ++i)
        {
            for(int j = 0; j < n; ++j)
            {
                if(edges[i][k] < edges[i][j] && edges[k][j] < edges[i][j])
                {
                    edges[i][j] = max(edges[i][k],edges[k][j]);
                }
            }
        }
    }
}


int main()
{
//    freopen("input.txt","r",stdin);
    int num = 1;
    while(scanf("%d",&n) != EOF && n != 0)
    {
        read_graph();
        floyd();
        printf("Scenario #%d\n",num++);
        printf("Frog Distance = %.3lf\n",sqrt(edges[0][1]) + 0.00000001);
        printf("\n");
    }
    
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值