-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimPqMain.java
More file actions
52 lines (49 loc) · 1.37 KB
/
PrimPqMain.java
File metadata and controls
52 lines (49 loc) · 1.37 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
package algorithm;
import java.util.*;
public class PrimPqMain {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[][] g = new int[N][N];
boolean[] v = new boolean[N];
int[] minEdge = new int[N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
g[i][j] = sc.nextInt();
}
minEdge[i] = Integer.MAX_VALUE;
}
// Arrays.fill(minEdge, Integer.MAX_VALUE);
int res = 0, cnt = 0;
PriorityQueue<int[]> pq = new PriorityQueue<>((x, y) -> Integer.compare(x[1], y[1]));
minEdge[0] = 0;
pq.offer(new int[] { 0, 0 }); // 정점, 가중치
System.out.println(Arrays.toString(minEdge));
System.out.println();
while (!pq.isEmpty()) {
int[] cur = pq.poll();
int minVertex = cur[0];
int min = cur[1];
if (v[minVertex])
continue;
v[minVertex] = true;
res += min;
System.out.println(Arrays.toString(v));
System.out.println("minVertex=" + minVertex);
System.out.println("min=" + min);
System.out.println("result=" + res);
if (cnt++ == N - 1)
break;
for (int j = 0; j < N; j++) {
if (!v[j] && g[minVertex][j] != 0 && minEdge[j] > g[minVertex][j]) {
minEdge[j] = g[minVertex][j];
pq.offer(new int[] { j, g[minVertex][j] });
}
}
System.out.println(Arrays.toString(minEdge));
System.out.println();
}
System.out.println(res);
sc.close();
}
}