POJ2253 Frogger(最短路)

题目链接

题意:

从0号点,到1号点,找一条能通过的路,使得这条路中的最大的边,比其它所有可能的路中的边都小。

分析:

这题就是按着dijkstra写,写着写着觉得像是prim了。

其中d[n]表示从0出发到达该点的某条路中的最大边,且比其它可能的路中的都小。

从d[i]到d[j], 就是要用 max(d[i], G[i][j]) 去更新 d[j] 。 

注意:

输出时要用 %.3f 不能用 %.3lf.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <cmath>

using namespace std;

const int maxn = 220;

const double INF = 1e60;

int n;
double G[maxn][maxn], d[maxn];

struct Pos {
    int x, y;
}num[maxn];

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

double dijkstra() {
    bool vis[maxn];
    
    memset(vis, false, sizeof(vis));

    for(int i=0; i<n; i++) {
        d[i] = G[0][i];
    }

    d[0] = 0;
    vis[0] = true;

    for(int i=0; i<n-1; i++) {
        double m = INF; int x;
        for(int y=0; y<n; y++) if(!vis[y] && m >= d[y]) m = d[x=y];
        vis[x] = true;
        for(int y=0; y<n; y++){
            if(!vis[y]) {
                double maxx = max(d[x], G[x][y]);
                if(d[y] > maxx) d[y] = maxx;
            }
        }
    }
    
    return d[1];
}

int main() {
    int cnt = 0;
    while(scanf("%d", &n) == 1) {
        if(n == 0) break;

        for(int i=0; i<n; i++) scanf("%d%d", &num[i].x, &num[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);
            }
            G[i][i] = 0;
        }

        printf("Scenario #%d
Frog Distance = %.3f

", ++cnt, dijkstra());
    }

    return 0;
}
原文地址:https://www.cnblogs.com/tanhehe/p/3169865.html