-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_6497.java
More file actions
74 lines (57 loc) · 2.05 KB
/
Copy pathBOJ_6497.java
File metadata and controls
74 lines (57 loc) · 2.05 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
import java.io.*;
import java.util.*;
public class BOJ_6497 {
static class Edge implements Comparable<Edge> {
int to, cost;
Edge(int to, int cost) {
this.to = to;
this.cost = cost;
}
@Override
public int compareTo(Edge o) {
return this.cost - o.cost;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
while(true) {
st = new StringTokenizer(br.readLine());
int m = Integer.parseInt(st.nextToken()); // 집의 수
int n = Integer.parseInt(st.nextToken()); // 길이의 수
if (m == 0 && n == 0) break;
List<List<Edge>> graph = new ArrayList<>();
for (int i = 0; i < m; i++) {
graph.add(new ArrayList<>());
}
int totalCost = 0;
for(int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
int z = Integer.parseInt(st.nextToken());
graph.get(x).add(new Edge(y, z));
graph.get(y).add(new Edge(x, z));
totalCost += z;
}
boolean[] visited = new boolean[m];
PriorityQueue<Edge> pq = new PriorityQueue<>();
pq.add(new Edge(0, 0));
int mstCost = 0;
int count = 0;
while (!pq.isEmpty() && count < m) {
Edge cur = pq.poll();
if (visited[cur.to]) continue;
visited[cur.to] = true;
mstCost += cur.cost;
count++;
for (Edge next : graph.get(cur.to)) {
if (!visited[next.to]) {
pq.add(next);
}
}
}
System.out.println(totalCost - mstCost);
}
}
}