-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRoadReparation.java
More file actions
64 lines (60 loc) · 1.96 KB
/
RoadReparation.java
File metadata and controls
64 lines (60 loc) · 1.96 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.PriorityQueue;
public class RoadReparation {
public static class Edge implements Comparable<Edge>
{
int v;
int val;
Edge(int v, int val) {
this.v = v;
this.val = val;
}
public int compareTo(Edge simpson) {
return this.val - simpson.val;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] s = br.readLine().split(" ");
int n = Integer.parseInt(s[0]);
int m = Integer.parseInt(s[1]);
ArrayList<ArrayList<Edge>> al = new ArrayList<>();
PriorityQueue<Edge> q = new PriorityQueue<>();
q.add(new Edge(0, 0));
for(int i = 0; i < n; i++) {
al.add(new ArrayList<>());
}
for(int i = 0; i < m; i++) {
String s1[] = br.readLine().split(" ");
int a = Integer.parseInt(s1[0]);
int b = Integer.parseInt(s1[1]);
int val = Integer.parseInt(s1[2]);
al.get(a-1).add(new Edge(b-1, val));
al.get(b-1).add(new Edge(a-1, val));
}
boolean flag = false;
boolean[] vis = new boolean[n];
long ans = 0;
int count = 0;
while(!q.isEmpty()) {
Edge rem = q.remove();
if(vis[rem.v]) continue;
vis[rem.v] = true;
int u = rem.v;
ans += rem.val;
count++;
for(Edge e: al.get(u)) {
if(!vis[e.v]) {
q.add(e);
}
}
}
if(count == n) System.out.println(ans);
else System.out.println("IMPOSSIBLE");
}
}