-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1197.java
More file actions
60 lines (54 loc) · 1.75 KB
/
Copy path1197.java
File metadata and controls
60 lines (54 loc) · 1.75 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
// https://www.acmicpc.net/problem/1197
// 최소 스패닝 트리
import java.util.*;
import java.lang.*;
import java.io.*;
class Main {
private static int n, m, head[];
private static PriorityQueue<Edge> edges = new PriorityQueue<>();
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(reader.readLine());
n = Integer.parseInt(st.nextToken()); // 정점의 갯수
m = Integer.parseInt(st.nextToken()); // 간선의 갯수
head = new int[n];
for(int i=1; i<n; i++) head[i] = i;
for(int i=0; i<m; i++){
st = new StringTokenizer(reader.readLine());
int a = Integer.parseInt(st.nextToken()) - 1;
int b = Integer.parseInt(st.nextToken()) - 1;
int c = Integer.parseInt(st.nextToken());
edges.add(new Edge(a, b, c));
}
long answer = 0;
while(!edges.isEmpty()){
Edge cur = edges.poll();
if(find(cur.x) != find(cur.y)){
union(cur.x, cur.y);
answer += cur.w;
}
}
System.out.println(answer);
}
private static int find(int v){
if(head[v] == v) return v;
else return head[v] = find(head[v]);
}
private static void union(int a, int b){
a = find(a);
b = find(b);
if(a < b) head[a] = b;
else head[b] = a;
}
}
class Edge implements Comparable<Edge> {
int x, y, w;
public Edge(int x, int y, int w){
this.x = x;
this.y = y;
this.w = w;
}
public int compareTo(Edge o){
return this.w - o.w;
}
}