728x90
https://www.acmicpc.net/problem/10423
728x90
메모리: 47,436 KB , 시간: 560 ms
사용 알고리즘: 그래프 이론, 최소 스패닝 트리
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class Main {
static int[] parent;
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
int K = Integer.parseInt(st.nextToken());
// 크루스칼 알고리즘 사용
parent = new int[N + 1];
for (int i = 1; i <= N; i++) {
parent[i] = i;
}
st = new StringTokenizer(br.readLine());
for (int i = 0; i < K; i++) {
parent[Integer.parseInt(st.nextToken())] = 0;
}
PriorityQueue<int[]> pq = new PriorityQueue<>((o1, o2) -> o1[2] - o2[2]);
int u, v, w;
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
u = Integer.parseInt(st.nextToken());
v = Integer.parseInt(st.nextToken());
w = Integer.parseInt(st.nextToken());
pq.add(new int[] {u, v, w});
}
long result = 0;
int[] now;
while(!pq.isEmpty()) {
now = pq.poll();
if(union(now[0], now[1])) result += now[2];
}
System.out.println(result);
}
private static int find(int a) {
if(parent[a] == a) return a;
return parent[a] = find(parent[a]);
}
private static boolean union(int a, int b) {
a = find(a);
b = find(b);
if(a == b) return false;
if(a < b) parent[b] = a;
else parent[a] = b;
return true;
}
}
728x90
'Problem Solving > BOJ' 카테고리의 다른 글
백준, BOJ 14657] 준오는 최종인재야!! (java) (0) | 2024.04.19 |
---|---|
[백준, BOJ 1174] 줄어드는 수 (java) (1) | 2024.04.19 |
[백준, BOJ 12757] 전설의 JBNU (java) (0) | 2024.04.12 |
[백준, BOJ 19535] ㄷㄷㄷㅈ (java) (0) | 2024.04.11 |
[백준, BOJ 3665] 최종 순위 (java) (0) | 2024.04.09 |