-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDH_42861.java
More file actions
69 lines (53 loc) · 1.43 KB
/
DH_42861.java
File metadata and controls
69 lines (53 loc) · 1.43 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
import java.util.*;
/*
섬 연결하기
*/
class DH_42861 {
static class Edge implements Comparable<Edge> {
int s, e, w;
public Edge(int s, int e, int w) {
this.s = s;
this.e = e;
this.w = w;
}
@Override
public int compareTo(Edge o) {
return Integer.compare(this.w, o.w);
}
}
static int[] p;
static PriorityQueue<Edge> q;
static int mst() {
int cnt = 0, result = 0;
// 간선이 N - 1개 만들어질 때까지 진행
while(!q.isEmpty() && cnt < (p.length - 1) - 1) {
Edge current = q.poll();
if(find(current.s) == find(current.e)) continue;
union(current.s, current.e);
result += current.w;
}
return result;
}
static void union(int a, int b) {
a = find(a);
b = find(b);
if(a != b) p[b] = a;
}
static int find(int a) {
return p[a] = a == p[a] ? a: find(p[a]);
}
public int solution(int n, int[][] costs) {
q = new PriorityQueue<>();
p = new int[n + 1];
for(int i = 0; i < p.length; i++) p[i] = i;
for(int[] c: costs) {
int s = c[0];
int e = c[1];
int w = c[2];
// Queue에 Edge 저장
q.add(new Edge(s, e, w));
q.add(new Edge(e, s, w));
}
return mst();
}
}