POJ - 2253 Frogger (最大权值最小化)

题目大意:有只青蛙要去找伙伴,但是他遇到了个难题,要过河。河水很脏,青蛙不想游过去,恰好河上面有N块石头,但是这只青蛙腿力又不行,所以他想让在去的路上的石头间的最大值最小

解题思路:SPFA更新时顺便记录一下跳到该点的路径的石头间的距离的最大权值即可

这题提交时poj挂了。。。

#include 
#include 
#include 
#include 
using namespace std;
#define N 210
#define INF 0x3f3f3f3f

struct Stone{
    int x, y;
}S[N];

double dis[N][N], jump[N];
int n;
bool vis[N];

void init(){

    for (int i = 1; i <= n; i++) {
        scanf("%d%d", &S[i].x, &S[i].y);
    }

    for (int i = 1; i <= n; i++) 
        for (int j = i; j <= n; j++) {
            if (i == j) 
                dis[i][j] = 0;
            else 
                dis[i][j] = dis[j][i] = (S[i].x - S[j].x) * (S[i].x - S[j].x) + (S[i].y - S[j].y) * (S[i].y - S[j].y); 
        }
}

void SPFA() {

    queue<int> q;
    q.push(1);
    vis[1] = true;
    jump[1] = 0.0;
    for (int i = 2; i <= n; i++)
        jump[i] = INF;

    while (!q.empty()) {
        int t = q.front();
        q.pop();
        vis[t] = false;

        for (int i = 1; i <= n; i++) {
            if (i != t && jump[i] > max(jump[t], dis[t][i])) {
                jump[i] = max(jump[t], dis[t][i]);
                if (!vis[i]) {
                    q.push(i);
                    vis[i] = true;
                }
            }
        }
    }
}

int main() {
    int cas = 1;
    while (scanf("%d", &n) != EOF && n) {
        init();
            SPFA();
        printf("Scenario #%d\n", cas++);
        printf("Frog Distance = %.3lf\n\n", sqrt(jump[2]));
    }
    return 0;
}

你可能感兴趣的:(ACM-图论-最短路)