POJ 2254 Frogger 堆优化dijkstra解决最大边权最小值问题

http://poj.org/problem?id=2253

把点的坐标转化为点的序号

求每条路径中最大边权值的最小值,dijkstra算法中原本的dist[i]维护的是起始点到i号点的最短路径总长度;现在dist[i]维护起始点到i点到最大边权值,相应地,松弛操作改写为:d[i]=max(d[j],g[i][j]);


堆优化dijkstra解决最大边权最小值问题:

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

const int maxn = 1005;
int T, N;

struct Point
{
    int x, y;
}point[maxn];

double g[maxn][maxn];

// d[i]:原点到i最大边权值
double d[maxn];

void INIT()
{
    for (int i = 0; i < maxn; i ++ ) d[i] = 1e9;
    memset(point, 0, sizeof point);
    memset(g, 0, sizeof g);
}

double calc(int i, int j)
{
    return sqrt((point[i].x-point[j].x)*(point[i].x-point[j].x) + (point[i].y-point[j].y)*(point[i].y-point[j].y));
}

void dijkstra()
{
    d[0] = 0;
    priority_queue<pair<double, int> > Q;
    Q.push({-d[0], 0});
    while (!Q.empty())
    {
        int head = Q.top().second;
        Q.pop();
        for (int i = 1; i < N; i ++ )
        {
            if(d[i] > max(d[head], g[head][i]))
            {
                d[i] = max(d[head], g[head][i]);
                Q.push({-d[i], i});
            }
        }
    }
}

int main () {
    int Scenario = 0;
    while (cin >> N && N)
    {
        INIT();
        for (int i = 0; i < N; i ++ )
            cin >> point[i].x >> point[i].y;
        for (int i = 0; i < N; i ++ )
            for (int j = 0; j < i; j ++ )
                g[i][j] = g[j][i] = calc(i, j);
        
        dijkstra();
        cout <<"Scenario #"<< ++Scenario <<"\nFrog Distance = ";
        printf("%.3f\n\n", d[1]);
    }
    return 0;
}

你可能感兴趣的:(最短路径,图论)