POJ 2253 Frogger

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.
题意:给n个点,求出第一个点到第二个点的多条路径中任意两点最长距离的最短的距离。
可以看做dijkstra的变形,贪心思想,用数组d[i]代表i到源点路径中相邻两点的最长距离。

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<cmath>
#include<functional>
using namespace std;
#define N 1000+5
#define MAXN 1000000
#define mem(arr,a) memset(arr,a,sizeof(arr))
#define INF 0x3f3f3f3f
#define LL long long int 
#define pow(a) (a)*(a)
double d[N];
struct node{
    int x, y;
}p[N];
int vis[N];
double cost[N][N];
int n;
int cnt = 1;
double dis(node a, node b){
    return sqrt((double)pow(a.x - b.x) + (double)pow(a.y - b.y));
}
void dijkstra(){
    mem(d, 0);
    mem(vis, 0);

    while (1){
        int v = -1;
        for (int i = 1; i <= n; i++){
            if (!vis[i] && (v == -1 || d[v] > d[i]))v = i;
        }
        if (v == -1)break;
        vis[v] = 1;
        for (int i = 1; i <= n; i++){
            if (!vis[i])
            {
                if (!d[i])d[i] = max(d[v], cost[v][i]);
                else if (d[i] < d[v])continue;
                else d[i] = min(d[i], max(d[v], cost[v][i]));
            }
        }
    }
    printf("Scenario #%d\nFrog Distance = %.3f\n\n", cnt++, d[2]);
}
int main(){
    while (cin >> n){
        if (n == 0)break;
        for (int i = 1; i <= n; i++){
            cin >> p[i].x >> p[i].y;
        }
        mem(cost, 0);
        for (int i = 1; i <= n; i++){
            for (int j = 1; j <= n; j++){
                cost[i][j] = dis(p[i], p[j]);
            }
        }
        dijkstra();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值