pku 2253(floyd变形)

View Code
/*

  Name: floyd 

  Copyright: 

  Author: 

  Date: 19/04/12 22:39

  Description: 求所有连接1跟2的路径中,最大边权中的最小值 

*/



#include <cmath>

#include <cstdio>

#include <iostream>



using namespace std;



const int N = 205;



double dis[N][N];



struct point {

    double x;

    double y;

}p[N];



double max(double a, double b) {

    return a > b ? a : b;

}



double floyd(int n) {

    for (int k=0; k<n; ++k) {

        for (int i=0; i<n; ++i) {

            for (int j=0; j<n; ++j) {

                double maxs = max(dis[i][k], dis[k][j]);//递推式变形 

                if (dis[i][j] > maxs) dis[i][j] = maxs;

            }

        }

    }

    return dis[0][1];

}



int main() {

    int n, t = 0;

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

        for (int i=0; i<n; ++i) scanf ("%lf%lf", &p[i].x, &p[i].y);

        for (int i=0; i<n; ++i) {

            for (int j=0; j<i; ++j) {

                if (i == j) dis[i][j] = 0;

                else dis[i][j] = dis[j][i] = sqrt((p[i].x-p[j].x)*(p[i].x-p[j].x)+(p[i].y-p[j].y)*(p[i].y-p[j].y)); 

            }

        }

        double ans = floyd(n);

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

        printf ("Frog Distance = %.3lf\n\n", ans);

    }

    return 0;

} 

 

你可能感兴趣的:(floyd)