POJ 2253 Frogger floyd算法

时间:2023-03-08 20:52:57

题目:click here

题意:

  给出两只青蛙的坐标A、B,和其他的n-2个坐标,任意两坐标间是双向连通的。显然从A到B存在至少一条的通路,每一条通路的元素都是这条通路中前后两个点的距离,这些距离中又有一个最大距离。现在要求求出所有通路的最大距离,并把这些最大距离作比较,把最小的一个最大距离作为青蛙的最小跳远距离。

分析:

  最小化最大值--------

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#define power(a) ((a)*(a))
using namespace std;
const int INF = 1e9+;
const int M = ; struct node { double x, y; } point[M]; // 点的坐标
int n, tcase;
double cost[M][M]; // 邻接矩阵存图
double dis( node a, node b ) { // 距离
return sqrt( power(a.x-b.x) + power(a.y-b.y) );
} void floyd() { // 弗洛伊德算法
for( int k=; k<=n; k++ )
for( int i=; i<=n; i++ )
for( int j=; j<=n; j++ )
cost[i][j] = min( cost[i][j], max(cost[i][k],cost[k][j]) );
} void solve() {
for( int i=; i<=n; i++ )
scanf("%lf%lf", &point[i].x, &point[i].y );
for( int i=; i<=n; i++ ) {
cost[i][i] = ;
for( int j=i+; j<=n; j++ ) {
double d = dis( point[i], point[j] );
cost[i][j] = d; // 建立无向图
cost[j][i] = d;
}
}
floyd();
printf("Scenario #%d\n", tcase++);
printf("Frog Distance = %.3f\n\n", cost[][] );
}
int main() {
tcase = ;
while( ~scanf("%d", &n ) && n ) {
solve();
}
return ;
}