-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimMSTFast.java
More file actions
58 lines (45 loc) · 1.55 KB
/
PrimMSTFast.java
File metadata and controls
58 lines (45 loc) · 1.55 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
//O(N^2) variation of Prim's, works very well on dense graphs since requires only O(N) memory
import java.util.*;
import java.io.*;
public class PrimMSTFast {
public static void main(String[] args) throws IOException {
FastScanner in = new FastScanner(System.in);
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int n = in.nextInt();
int m = in.nextInt();
boolean[] vis = new boolean[n + 1];
int[][] adj = new int[n + 1][n + 1];
long[] minEdge = new long[n + 1];
for (int[] x : adj) Arrays.fill(x, Integer.MAX_VALUE);
for (int i = 0; i < m; i++) {
int u = in.nextInt();
int v = in.nextInt();
int w = in.nextInt();
adj[u][v] = w;
adj[v][u] = w;
}
long dist = 0;
for (int i = 1; i <= n; i++) minEdge[i] = Integer.MAX_VALUE;
minEdge[1] = 0;
for (int i = 0; i < n; i++) {
int min = 0;
for (int j = 1; j <= n; j++) {
if (!vis[j] && (min == 0 || minEdge[j] < minEdge[min])) min = j;
}
if (minEdge[min] == Integer.MAX_VALUE) {
out.println("No MST!");
out.close();
return;
}
vis[min] = true;
dist += minEdge[min];
for (int to = 1; to <= n; to++) {
if (adj[min][to] < minEdge[to]) {
minEdge[to] = adj[min][to];
}
}
}
out.println(dist);
out.close();
}
}