Skip to content

Commit 6dcf4f6

Browse files
authored
Merge pull request #1732 from AlgorithmWithGod/LiiNi-coder
[20251223] BOJ / G4 / 행성 연결 / 이인희
2 parents e96186d + 95a1725 commit 6dcf4f6

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
public static void main(String[] args) throws IOException {
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
int N = Integer.parseInt(br.readLine());
9+
int[][] cost = new int[N][N];
10+
for(int i = 0; i < N; i++){
11+
StringTokenizer st = new StringTokenizer(br.readLine());
12+
for(int j = 0; j < N; j++){
13+
cost[i][j] = Integer.parseInt(st.nextToken());
14+
}
15+
}
16+
boolean[] visited = new boolean[N];
17+
PriorityQueue<int[]> pq = new PriorityQueue<>(
18+
(a, b) -> a[1] - b[1]
19+
);
20+
pq.offer(new int[]{0, 0});
21+
22+
long answer = 0;
23+
int count = 0;
24+
while(!pq.isEmpty()){
25+
int[] cur = pq.poll();
26+
int u = cur[0];
27+
int c = cur[1];
28+
if(visited[u])
29+
continue;
30+
31+
visited[u] = true;
32+
answer += c;
33+
count++;
34+
if(count == N)
35+
break;
36+
for(int v = 0; v < N; v++){
37+
if(!visited[v]){
38+
pq.offer(new int[]{v, cost[u][v]});
39+
}
40+
}
41+
}
42+
System.out.println(answer);
43+
}
44+
}
45+
46+
```

0 commit comments

Comments
 (0)