题目要求找出点0到点1在每个路径中跳跃距离的最大值中最小的一个。
首先初始化数组d,d[i][j]表示从点i跳到点j的最小跳跃距离。
将Floyed-Warshall算法变形一下,用临时变量存取从点i跳到点k的最小跳跃距离与点k到点j的最小跳跃距离中的最大值(同一路径取最大值)
将该临时变量与以求得的最短跳跃距离d[i][j]比较,取里面最小的一个。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn = 200 + 10;
typedef pair<double, double> P;
P coor[maxn];
double d[maxn][maxn];
double dist(double x1, double y1, double x2, double y2)
{
double mult = pow(x1 - x2, 2) + pow(y1 - y2, 2);
return sqrt(mult);
}
int main()
{
int n;
int tcase = 1;
while (scanf("%d", &n) == 1 && n) {
for (int i = 0; i < n; i++) {
scanf("%lf%lf", &coor[i].first, &coor[i].second);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
d[i][j] = dist(coor[i].first, coor[i].second,
coor[j].first, coor[j].second);
}
}
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
double tmp = max(d[i][k], d[k][j]);
d[i][j] = min(d[i][j], tmp);
}
}
}
printf("Scenario #%d\n", tcase++);
printf("Frog Distance = %.3f\n\n", d[0][1]);
memset(coor, 0, sizeof(coor));
}
return 0;
}