-
Notifications
You must be signed in to change notification settings - Fork 1
/
p14950.java
83 lines (76 loc) · 2.34 KB
/
p14950.java
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
/*
정복자
kruskal, MST(union-find by rank)
오래걸린 부분과 이유
단순히 모든 간선을 넣는게 아니라, 1번 노드를 맨 처음으로 탐색해야 하기 때문에
간선의 배열을 만들어서 정렬을 따로 시켜주어야 한다.
*/
import java.io.*;
import java.util.*;
public class p14950 {
static class Edge implements Comparable<Edge> {
int u, v, cost;
public Edge (int u, int v, int cost) {
this.u = u;
this.v = v;
this.cost = cost;
}
public int compareTo(Edge o) {
return cost - o.cost;
}
}
static int n, m, t;
static int[] parent, rank;
static Edge[] edges;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
t = Integer.parseInt(st.nextToken());
parent = new int[n + 1];
rank = new int[n + 1];
edges = new Edge[m];
for (int i = 0; i <= n; i++) {
parent[i] = i;
}
for (int i = 0; i < m; i++) {
st = new StringTokenizer(br.readLine());
int u = Integer.parseInt(st.nextToken());
int v = Integer.parseInt(st.nextToken());
int cost = Integer.parseInt(st.nextToken());
edges[i] = new Edge(u, v, cost);
}
Arrays.sort(edges);
mst();
}
static void mst() {
long ret = 0;
int cnt = 0;
for (Edge edge : edges) {
if (find(edge.u) == find(edge.v)) continue;
union(edge.u, edge.v);
ret += edge.cost + (cnt * t);
cnt++;
}
System.out.println(ret);
}
static void union(int u, int v) {
u = find(u);
v = find(v);
if (u != v) {
if (rank[u] < rank[v]) {
parent[u] = v;
} else {
parent[v] = u;
if (rank[u] == rank[v]) {
rank[u]++;
}
}
}
}
static int find (int x) {
if (x == parent[x]) return x;
return parent[x] = find(parent[x]);
}
}