-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ1197.java
More file actions
83 lines (69 loc) · 1.73 KB
/
Copy pathBOJ1197.java
File metadata and controls
83 lines (69 loc) · 1.73 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package day10.mst;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
class Edge {
int from, to, weight;
public Edge(int from, int to, int weight) {
super();
this.from = from;
this.to = to;
this.weight = weight;
}
}
public class BOJ1197 {
static int V, E; //정점 개수, 간선 개수
static Edge[] edgeList; //간선 리스트
//유니온 파인드
static int[] parents;
static void make() {
parents=new int[V+1];
for (int i = 0; i < V; i++) {
parents[i]=i;
}
}
static int find(int x) {
if (parents[x]==x) return x;
return parents[x] = find(parents[x]);
}
static boolean union(int x, int y) {
int xRoot=find(x);
int yRoot=find(y);
if(xRoot==yRoot) {
return false;
}
parents[yRoot]=xRoot;
return true;
}
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer(br.readLine());
V=Integer.parseInt(st.nextToken());
E=Integer.parseInt(st.nextToken());
edgeList=new Edge[E];
for (int i = 0; i < E; i++) {
st=new StringTokenizer(br.readLine());
int from=Integer.parseInt(st.nextToken());
int to=Integer.parseInt(st.nextToken());
int weight=Integer.parseInt(st.nextToken());
edgeList[i]=new Edge(from, to, weight);
}
//weight를 기준으로 edgeList 정렬
Arrays.sort(edgeList, (e1, e2) -> e1.weight - e2.weight);
make();
int edgeCnt=0;
int totalWeight=0;
for (Edge e : edgeList) {
if(union(e.from, e.to)) {
edgeCnt++;
totalWeight+=e.weight;
if(edgeCnt==V-1) {
break;
}
}
}
System.out.println(totalWeight);
}
}