-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKruskal.java
More file actions
97 lines (81 loc) · 2.49 KB
/
Kruskal.java
File metadata and controls
97 lines (81 loc) · 2.49 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package kruskal;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;
/*
* 1. Set V and E
* 2. Arrange edge(E) in ascending order.
* 3. Add small weights first. But check if there is a cycle.
* - Cycle check can be checked with DFS.
* ** The implementation is done using the Union-Find algorithm.
*/
/*
** BOJ 1197 - 최소 스패닝 트리 (Kruskal Algorithm)
** https://www.acmicpc.net/problem/1197
*/
public class Main {
private static int V, E;
private static int minCost = 0;
private static int[] parent;
private static Edge[] edges;
public static void main(String[] argv) 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());
parent = new int[V+1];
edges = new Edge[E];
for(int v=1; v<=V; ++v) parent[v] = v;
for(int e=0; e<E; ++e){
st = new StringTokenizer(br.readLine());
edges[e] = new Edge(Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
}
br.close();
Arrays.sort(edges, new Comparator<Edge>(){
@Override
public int compare(Edge o1, Edge o2) {
return o1.cost - o2.cost;
}
});
for(int i=0; i<E; ++i){
int rootX = findRoot(edges[i].v1);
int rootY = findRoot(edges[i].v2);
if(rootX == rootY) continue; // Cycle!!
else {
/* Union-Find's union */
parent[rootX] = rootY;
minCost = minCost + edges[i].cost;
}
}
System.out.println(minCost);
}
/* Related to Union-Find
* Find vertex; x's 'root vertex'.
*/
private static int findRoot(int x){
if(x == parent[x]) return x;
else {
parent[x] = findRoot(parent[x]);
return parent[x];
}
}
/* Edge Informations
* v1 : Vertex 1
* v2 : Vertex 2
* cost : Edge's weight
* */
static class Edge {
int v1;
int v2;
int cost;
public Edge(int v1, int v2, int cost) {
this.v1 = v1;
this.v2 = v2;
this.cost = cost;
}
}
}