forked from nanwan03/poj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2395 Out of Hay.java
55 lines (54 loc) · 1.25 KB
/
2395 Out of Hay.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
import java.io.*;
import java.util.*;
import java.math.*;
public class Main {
private static final int MAX = 1000000001;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int size = in.nextInt();
int lines = in.nextInt();
int[][] map = new int[size][size];
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
map[i][j] = MAX;
}
}
for (int i = 1; i <= lines; ++i) {
int u = in.nextInt() - 1;
int v = in.nextInt() - 1;
int dis = in.nextInt();
map[u][v] = Math.min(map[u][v], dis);
map[v][u] = Math.min(map[v][u], dis);
}
System.out.println(prim(map, size));
}
private static int prim(int[][] map, int size) {
int max = 0;
int[] lowCost = new int[size];
boolean[] used = new boolean[size];
for (int i = 0; i < size; ++i) {
lowCost[i] = map[0][i];
used[i] = false;
}
used[0] = true;
for (int i = 1; i < size; ++i) {
int j = 0;
while (used[j]) {
j++;
}
for (int k = 0; k < size; ++k) {
if ((!used[k]) && (lowCost[k] < lowCost[j])) {
j = k;
}
}
used[j] = true;
max = Math.max(max, lowCost[j]);
for (int k = 0; k < size; ++k) {
if (!used[k] && (map[j][k] < lowCost[k])) {
lowCost[k] = map[j][k];
}
}
}
return max;
}
}