-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrims.java
49 lines (48 loc) · 1.04 KB
/
Prims.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
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Scanner;
public class Prims {
public static void main(String[] args) {
Scanner in= new Scanner(System.in);
int n= in.nextInt();
int m= in.nextInt();
boolean [] vis= new boolean[n];
ArrayList<e>[] adj= new ArrayList[n];
for (int i = 0; i < adj.length; i++) {
adj[i]= new ArrayList<>();
}
for (int i = 0; i < m; i++) {
int a= in.nextInt()-1;
int b= in.nextInt()-1;
int c= in.nextInt();
adj[a].add(new e(b,c));
adj[b].add(new e(a,c));
}
PriorityQueue<e> pq= new PriorityQueue<>();
for (int i = 0; i < adj[0].size(); i++) {
pq.offer(adj[0].get(i));
}
vis[0]= true;
long mst=0;
while(!pq.isEmpty()) {
e cur = pq.poll();
if(vis[cur.to]) continue;
vis[cur.to]= true;
mst+= cur.w;
for(e nex: adj[cur.to]) {
pq.add(nex);
}
}
System.out.println(mst);
}
static class e implements Comparable<e>{
int to, w;
public e(int a, int b) {
to= a;
w= b;
}
public int compareTo(e o) {
return this.w- o.w;
}
}
}