[kuangbin带你飞]专题四 最短路练习 B

166 篇文章 0 订阅
32 篇文章 0 订阅

真的不知道要败给读题到什么时候。。。

http://poj.org/problem?id=2253
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.

题意:

从0到1,使经过的所有路径中长度最长的距离最短
咋就这么难懂。。。

tip:

看明白题倒是直接写出来了,看见有人问,也说一句吧。。。dij本身就是基于贪心的一种求最短路的方式,所谓贪心,就是保证每次松弛都是当前最优(短)的,那拿他去松弛别人保证了这些不会再松弛回来,所以有限。这道题的贪心最优变成了距离的最大值最小,还是个堆,只是现在的dis不是距离了,而是一条路上距离的最大值,使他最小。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <vector>
#include <deque>
#include <utility>
#include <functional>
const int maxn = 210;
const double eps = 1e-6;
const int INF = (1<<30);
using namespace std;
int n;
double ans;
double dis[maxn],dist[maxn][maxn];
typedef pair<int,double>pii;
priority_queue <pii,vector<pii> ,greater <pii> > pq;

void dij(){
    while(!pq.empty()){
        int no = pq.top().second;
        if(no == 1) return;
        pq.pop();
        for(int i = 1 ; i < n ; i++){
            if(dis[i] > max(dis[no],dist[i][no])){
                dis[i] = max(dis[no],dist[i][no]);
                pq.push(make_pair(dis[i],i));
            }
        }
    }
}
int x[maxn],y[maxn];
void init(){
    ans = 0;
    for(int i = 0 ; i < n ; i++){
        scanf("%d%d",&x[i],&y[i]);
    }
    for(int i = 1 ; i < n ; i++){
        dis[i] = INF;
        for(int j = i-1 ; j >= 0 ; j--){
            dist[i][j] = dist[j][i] = sqrt((double)((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])));
            //printf("dist[%d][%d] = %lf\n",i,j,dist[i][j]);
        }
    }

    while(!pq.empty()){
        pq.pop();
    }
    dis[0] = 0;
    pq.push(make_pair(0,0));
}

int main(){
    int ca = 0;
    while(~scanf("%d",&n)&&n){
        init();
        dij();
        printf("Scenario #%d\nFrog Distance = %.3f\n\n",++ca,dis[1]);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值