POJ 2253 Frogger

这道题是要求石头1到石头2的最长跳跃距离的最小值,用了堆优化的dij来做,想不到好

久没写最短路,这玩意都写的不顺了。也可以用floyd求出任意两个石头间的距离,也就是

跳跃距离,然后找到1到2的最长路径的最小值。

/*Accepted    636K    0MS    C++    1453B    2012-07-26 11:51:06*/

#include<cstdio>

#include<cstring>

#include<cstdlib>

#include<queue>

#include<cmath>

#include<iostream>

using namespace std;



typedef pair< double,int > pii;

const int MAXN = 1 << 8;

double x[MAXN], y[MAXN] ;

double g[MAXN][MAXN] ;

int n;



double dis( double a, double b)

{

    return sqrt(a * a + b * b);

}



void ReadGragh()

{

    for( int i = 1; i <= n; i ++)

        scanf( "%lf%lf", &x[i], &y[i]);

    for( int i = 1; i <= n; i ++)

        for( int j = i; j <= n; j ++)

        {

            g[i][j] = g[j][i] = dis( x[i] - x[j], y[i] - y[j]);

        }

}



double Dijkstra()

{

    priority_queue< pii, vector<pii>, greater<pii> > q;

    double dist[MAXN] = {0.0};

    for( int i = 2; i <= n; i ++)

        dist[i] = 1e120;

    q.push( make_pair(0.0, 1));

    while( !q.empty())

    {

        pii u = q.top(); q.pop();

        int x = u.second;

        if( x == 2) return dist[2];

        if( dist[x] != u.first) continue; //防止结点重复扩展

        for( int i = 2; i <= n; i ++)

        {

            if( i != x && dist[i] > max(dist[x], g[x][i]) )

            {

                dist[i] = max(dist[x], g[x][i]);

                q.push( make_pair(dist[i], i));

            }

        }

    }

    return 0;

}



int main()

{

    int cas = 1;

    while( scanf("%d", &n), n)

    {

        ReadGragh();

        printf( "Scenario #%d\n", cas ++);

        printf( "Frog Distance = %.3f\n\n", Dijkstra());

    }

}

 

 

 

你可能感兴趣的:(poj)