-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrimSpecialSubtree.java
100 lines (87 loc) · 2.48 KB
/
PrimSpecialSubtree.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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* https://www.hackerrank.com/challenges/primsmstsub/problem
#prim
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Scanner;
class PrimSpecialSubtree {
public static int Prim(ArrayList<ArrayList<Node>> graph, int[] weightArr, int src) {
int minusVal = 0;
int sz = graph.size();
boolean[] visitedArr = new boolean[sz];
Arrays.fill(visitedArr, false);
boolean[][] edgeArr = new boolean[sz][sz];
for (int i = 0; i < sz; i++) {
Arrays.fill(edgeArr[i], false);
}
weightArr[src] = 0;
PriorityQueue<Node> pq = new PriorityQueue<>();
pq.add(new Node(src, 0));
Node node;
int w, id;
while (!pq.isEmpty()) {
node = pq.poll();
id = node.id;
if (visitedArr[id]) continue; // ??
visitedArr[id] = true;
Collections.sort(graph.get(id));
for (Node neighbour : graph.get(id)) {
w = neighbour.weight;
if (!visitedArr[neighbour.id] && w < weightArr[neighbour.id]) {
if (!edgeArr[id][neighbour.id]) {
edgeArr[id][neighbour.id] = true;
edgeArr[neighbour.id][id] = true;
} else {
minusVal += w;
}
weightArr[neighbour.id] = w;
pq.add(new Node(neighbour.id, w));
}
}
}
return minusVal;
}
public static int getTotalWeight(int[] weightArr, int minusVal) {
int ans = 0;
for (int weight : weightArr) {
ans += weight;
}
return ans - minusVal;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n, m, x, y, r, start;
n = sc.nextInt();
m = sc.nextInt();
int sz = n + 1;
ArrayList<ArrayList<Node>> graph = new ArrayList<>();
for (int i = 0; i < sz; i++) {
graph.add(new ArrayList<Node>());
}
for (int i = 0; i < m; i++) {
x = sc.nextInt();
y = sc.nextInt();
r = sc.nextInt();
graph.get(x).add(new Node(y, r));
graph.get(y).add(new Node(x, r));
}
start = sc.nextInt();
int[] weightArr = new int[sz];
Arrays.fill(weightArr, Integer.MAX_VALUE);
weightArr[0] = 0;
int minusVal = Prim(graph, weightArr, start);
System.out.println(getTotalWeight(weightArr, minusVal));
}
}
class Node implements Comparable<Node> {
int id, weight;
public Node(int id, int weight) {
this.id = id;
this.weight = weight;
}
public int compareTo(Node other) {
return this.weight - other.weight;
}
}