-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCobbledStreets.java
More file actions
85 lines (76 loc) · 2.07 KB
/
CobbledStreets.java
File metadata and controls
85 lines (76 loc) · 2.07 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/** https://www.spoj.com/problems/CSTREET/ tag: #prim #mst */
import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Scanner;
class CobbledStreets {
public static void Prim(ArrayList<ArrayList<Node>> graph, long[] distArr, int src) {
int sz = graph.size();
boolean[] visitedArr = new boolean[sz];
Arrays.fill(visitedArr, false);
distArr[src] = 0;
PriorityQueue<Node> pq = new PriorityQueue<>();
pq.add(new Node(src, 0));
Node node;
int id;
long w;
while (!pq.isEmpty()) {
node = pq.poll();
id = node.id;
visitedArr[id] = true;
for (Node neighbour : graph.get(id)) {
w = neighbour.dist;
if (!visitedArr[neighbour.id] && w < distArr[neighbour.id]) {
distArr[neighbour.id] = w;
pq.add(new Node(neighbour.id, w));
}
}
}
}
public static long getPrice(long[] distArr, long price) {
long ans = 0;
for (long val : distArr) {
ans += val;
}
return ans * price;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testcases = sc.nextInt();
long p, c;
int n, m, a, b;
for (int t = 0; t < testcases; t++) {
p = sc.nextLong();
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<>());
}
for (int i = 0; i < m; i++) {
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextLong();
graph.get(a).add(new Node(b, c));
graph.get(b).add(new Node(a, c));
}
long[] distArr = new long[sz];
Arrays.fill(distArr, Long.MAX_VALUE);
distArr[0] = 0;
Prim(graph, distArr, 1);
System.out.println(getPrice(distArr, p));
}
}
}
class Node implements Comparable<Node> {
int id;
long dist;
public Node(int id, long dist) {
this.id = id;
this.dist = dist;
}
public int compareTo(Node other) {
return (int) (this.dist - other.dist);
}
}