-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ1647.java
More file actions
73 lines (64 loc) · 1.77 KB
/
BOJ1647.java
File metadata and controls
73 lines (64 loc) · 1.77 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
package ¹éÁØ;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
public class BOJ1647 {
static int[] parent;
static ArrayList<Node> list;
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
list = new ArrayList<>();
parent = new int[N+1];
int temp = 0, cost = 0;
for(int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int start = Integer.parseInt(st.nextToken());
int end = Integer.parseInt(st.nextToken());
int weight = Integer.parseInt(st.nextToken());
list.add(new Node(start, end, weight));
}
for(int i = 1; i <= N; i++) {
parent[i] = i;
}
Collections.sort(list);
for(int i = 0; i < list.size(); i++) {
Node tmp = list.get(i);
if(find(tmp.start) != find(tmp.end)) {
temp += tmp.weight;
union(tmp.start, tmp.end);
cost = tmp.weight;
}
}
System.out.print(temp - cost);
}
public static int find(int a) {
if(a == parent[a]) return a;
return parent[a] = find(parent[a]);
}
public static void union(int a, int b) {
a = find(a);
b = find(b);
if(a < b) parent[b] = a;
else parent[a] = b;
}
static class Node implements Comparable<Node>{
int start;
int end;
int weight;
Node(int start, int end, int weight){
this.start = start;
this.end = end;
this.weight = weight;
}
@Override
public int compareTo(Node o) {
return weight - o.weight;
}
}
}