-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathp11085.java
More file actions
84 lines (75 loc) · 2.34 KB
/
p11085.java
File metadata and controls
84 lines (75 loc) · 2.34 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
군사이동
크루스칼, MST(Union-Find by Rank)
*/
import java.io.*;
import java.util.*;
public class p11085 {
static class Edge implements Comparable<Edge>{
int u, v, cost;
public Edge (int u, int v, int cost) {
this.u = u;
this.v = v;
this.cost = cost;
}
public int compareTo(Edge o) {
return o.cost - cost;
}
}
static int p, w, c, v;
static int[] parent, rank;
static PriorityQueue<Edge> pq = new PriorityQueue<>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
p = Integer.parseInt(st.nextToken());
w = Integer.parseInt(st.nextToken());
parent = new int[p];
rank = new int[p];
for (int i = 0; i < p; i++) {
parent[i] = i;
}
st = new StringTokenizer(br.readLine());
c = Integer.parseInt(st.nextToken());
v = Integer.parseInt(st.nextToken());
for (int i = 0; i < w; i++) {
st = new StringTokenizer(br.readLine());
int u = Integer.parseInt(st.nextToken());
int v = Integer.parseInt(st.nextToken());
int cost = Integer.parseInt(st.nextToken());
pq.add(new Edge(u, v, cost));
}
mst();
}
static void mst() {
int ret = 0;
while (!pq.isEmpty()) {
Edge edge = pq.poll();
union(edge.u, edge.v);
// 모든 경로들을 union하면서, c와 v가 서로 연결되어있는지 확인한다. (같은 루트인지)
if (find(c) == find(v)) {
ret = edge.cost;
break;
}
}
System.out.println(ret);
}
static void union(int u, int v) {
u = find(u);
v = find(v);
if (u != v) {
if (rank[u] < rank[v]) {
parent[u] = v;
} else {
parent[v] = u;
if (rank[u] == rank[v]) {
rank[u]++;
}
}
}
}
static int find(int x) {
if (x == parent[x]) return x;
return parent[x] = find(parent[x]);
}
}