forked from nanwan03/poj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2253 Frogger.java
68 lines (67 loc) · 1.87 KB
/
2253 Frogger.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.io.*;
import java.util.*;
import java.math.*;
class Node {
double x;
double y;
Node(double x, double y) {
this.x = x;
this.y = y;
}
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int testcase = 1;
while (true) {
int nodeNumber = Integer.parseInt(in.nextLine());
if (nodeNumber == 0) {
System.exit(0);
}
double[][] map = new double[nodeNumber + 1][nodeNumber + 1];
Node[] nodes = new Node[nodeNumber + 1];
for (int i = 1; i <= nodeNumber; ++i) {
String[] strs = in.nextLine().split("\\s+");
double x = Double.parseDouble(strs[0]);
double y = Double.parseDouble(strs[1]);
nodes[i] = new Node(x, y);
}
in.nextLine();
for (int i = 1; i <= nodeNumber; ++i) {
for (int j = 1; j <= i - 1; ++j) {
double distance = distance(nodes, i, j);
map[i][j] = distance;
map[j][i] = distance;
}
}
System.out.println("Scenario #" + testcase++);
dijkstra(map, nodes);
}
}
private static double distance(Node[] nodes, int x, int y) {
return Math.sqrt((nodes[x].x - nodes[y].x) * (nodes[x].x - nodes[y].x) + (nodes[x].y - nodes[y].y) * (nodes[x].y - nodes[y].y));
}
private static void dijkstra(double[][] distance, Node[] nodes) {
boolean[] visited = new boolean[nodes.length];
double[] dis = new double[nodes.length];
for (int i = 1; i < nodes.length; ++i) {
dis[i] = distance[1][i];
}
visited[1] = true;
int rst = 0;
for (int i = 2; i < nodes.length; ++i) {
double min = Double.POSITIVE_INFINITY;
for (int j = 1; j < nodes.length; ++j) {
if (!visited[j] && min > dis[j]) {
min = dis[j];
rst = j;
}
}
visited[rst] = true;
for (int j = 1; j < nodes.length; ++j) {
dis[j] = Math.min(dis[j], Math.max(dis[rst], distance[rst][j]));
}
}
System.out.print(String.format("Frog Distance = %.3f\n\n", dis[2]));
}
}