POJ2253-Frogger

12 篇文章 0 订阅

题目要求找出点0到点1在每个路径中跳跃距离的最大值中最小的一个。

首先初始化数组d,d[i][j]表示从点i跳到点j的最小跳跃距离。

将Floyed-Warshall算法变形一下,用临时变量存取从点i跳到点k的最小跳跃距离与点k到点j的最小跳跃距离中的最大值(同一路径取最大值)

将该临时变量与以求得的最短跳跃距离d[i][j]比较,取里面最小的一个。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

const int maxn = 200 + 10;
typedef pair<double, double> P;

P coor[maxn];
double d[maxn][maxn];

double dist(double x1, double y1, double x2, double y2)
{
    double mult = pow(x1 - x2, 2) + pow(y1 - y2, 2);
    return sqrt(mult);
}

int main()
{
    int n;
    int tcase = 1;
    
    while (scanf("%d", &n) == 1 && n) {
        for (int i = 0; i < n; i++) {
            scanf("%lf%lf", &coor[i].first, &coor[i].second);
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                d[i][j] = dist(coor[i].first, coor[i].second,
                               coor[j].first, coor[j].second);
            }
        }
        for (int k = 0; k < n; k++) {
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    double tmp = max(d[i][k], d[k][j]);
                    d[i][j] = min(d[i][j], tmp);
                }
            }
        }
        printf("Scenario #%d\n", tcase++);
        printf("Frog Distance = %.3f\n\n", d[0][1]);
        memset(coor, 0, sizeof(coor));
    }
    
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值