题目大意:有两只青蛙和若干块石头,现在已知这些东西的坐标,两只青蛙A坐标和青蛙B坐标是第一个和第二个坐标,现在A青蛙想要到B青蛙那里去,并且A青蛙可以借助任意石头的跳跃,而从A到B有若干通路,问从A到B的所有通路上的最大边 石头的距离没有直接给出需用距离公式计算
解题思路:一个简单的最短路径变形,只要将d[i]的意义变为从1到i的最大边即可,然后修改下更新条件就可以了
package 最短路;
import java.util.Scanner;
public class A2253 {
Scanner cin = new Scanner(System.in);
int n;
int cases;
int stone[][];
double map[][];
public void inPut() {
while (true) {
cases++;
n = cin.nextInt();
if (n == 0) {
return;
}
stone = new int[n][2];
map = new double[n][n];
for (int i = 0; i < n; i++) {
stone[i][0] = cin.nextInt();
stone[i][1] = cin.nextInt();
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
map[i][j] = Math.sqrt((stone[i][0] - stone[j][0])
* (stone[i][0] - stone[j][0])
+ (stone[i][1] - stone[j][1])
* (stone[i][1] - stone[j][1]));
}
}
solve();
}
}
private void solve() {
// System.out.println(Arrays.deepToString(map));
System.out.println("Scenario #" + cases);
System.out.printf("Frog Distance = %.3f\n\n", floyd());
}
private double floyd() {
for (int i = 0; i < n; i++) {
for (int k = 0; k < n; k++) {
for (int j = 0; j < n; j++) {
// 这个if语句里面的东西有改变,根据题意是要求两个石头之间的所有路径中最大跳(necessary jump
// range)最小的
// 这样就能把从石头A跳到石头B的所有路径中最大跳最小的值存到map[A][B]中,最后map[0][1]中存的就是所要的值
if (map[k][i] != 0
&& map[i][j] != 0
&& map[k][j] > (map[k][i] > map[i][j] ? map[k][i]
: map[i][j])) {
map[k][j] = (map[k][i] > map[i][j] ? map[k][i]
: map[i][j]);
}
}
}
}
return map[0][1];
}
public static void main(String[] args) {
new A2253().inPut();
}
}