-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJY_42861_2.java
More file actions
58 lines (47 loc) · 1.42 KB
/
JY_42861_2.java
File metadata and controls
58 lines (47 loc) · 1.42 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
import java.util.*;
class Solution {
static class Edge implements Comparable<Edge> {
int v, cost;
public Edge(int v, int cost) {
this.v = v;
this.cost = cost;
}
@Override
public int compareTo(Edge other) {
return this.cost - other.cost;
}
@Override
public String toString() {
return ">> v:"+this.v+" cost:"+this.cost;
}
}
public int solution(int n, int[][] costs) {
int answer = 0;
// 그래프 만들기
List<Edge>[] g = new ArrayList[n];
for(int i=0; i<n; i++) {
g[i] = new ArrayList<>();
}
for(int[] c: costs){
int v1 = c[0];
int v2 = c[1];
g[v1].add(new Edge(v2, c[2]));
g[v2].add(new Edge(v1, c[2]));
}
boolean[] visited = new boolean[n];
PriorityQueue<Edge> pq = new PriorityQueue<>();
pq.add(new Edge(0, 0));
while(!pq.isEmpty()) {
Edge now = pq.poll();
if(visited[now.v]) continue;
visited[now.v] = true;
answer += now.cost;
// now와 연결된 섬들 탐색
for(Edge e: g[now.v]) {
if(visited[e.v]) continue;
pq.add(e);
}
}
return answer;
}
}