2016huasacm暑假集训训练三 B-Frogger

题目链接: https://vjudge.net/contest/123674#problem/B

题意:一只青蛙在湖中一颗石头上, 它想去有另一只青蛙的石头上,但是 湖里的水很脏,它不愿意游泳,所以它要跳过去;

给出 两只青蛙所在石头的坐标, 及 湖里其他石头的坐标;任一两个坐标点间都是双向连通的。显然从A到B存在至少一条的通路,每一条通路的元素都是这条通路中前后两个点的距离,这些距离中又有一个最大距离。

现在要求求出所有通路的最大距离,并把这些最大距离作比较,把最小的一个最大距离作为青蛙的最小跳远距离。

只能说英语是硬伤,刚开始一直没看懂题目,最后还是一懂半懂,以为直接直接排序输出最大值就行,直接wa,后来还是用了floyd算法才Ac

 1 import java.io.BufferedInputStream;
 2 import java.util.Scanner;
 3 
 4 public class Main {
 5     static int n;
 6     static int cases = 0;
 7     static int stone[][];
 8     static double map[][];
 9     public static void main(String[] args) {
10         Scanner s = new Scanner(new BufferedInputStream(System.in));
11         while (s.hasNext()) {
12             cases++;
13             n = s.nextInt();
14             if (n == 0) {
15                 return;
16             }
17             stone = new int[n][2];
18             map = new double[n][n];
19             for (int i = 0; i < n; i++) {
20                 stone[i][0] = s.nextInt();
21                 stone[i][1] = s.nextInt();
22             }
23 
24             for (int i = 0; i < n; i++) {     //i j 之间边的长度
25                 for (int j = 0; j < n; j++) {
26                     map[i][j] = Math.sqrt((stone[i][0] - stone[j][0]) * (stone[i][0] - stone[j][0])
27                             + (stone[i][1] - stone[j][1]) * (stone[i][1] - stone[j][1]));
28                 }
29             }
30 
31             System.out.println("Scenario #" + cases);
32 
33             System.out.printf("Frog Distance = %.3f

", floyd());
34         }
35         s.close(); 
36     }
37     
38 static double floyd() { 
39         for (int i = 0; i < n; i++) {
40             for (int k = 0; k < n; k++) {
41                 for (int j = 0; j < n; j++) {
42 
43                     if (map[k][i] != 0 && map[i][j] != 0
44                             && map[k][j] > (map[k][i] > map[i][j] ? map[k][i] : map[i][j])) {
45                         map[k][j] = (map[k][i] > map[i][j] ? map[k][i] : map[i][j]);
46                     }
47                 }
48             }
49         }
50         return map[0][1];
51     }
52 }
原文地址:https://www.cnblogs.com/LIUWEI123/p/5715571.html