-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimulateNetwork.java
111 lines (101 loc) · 2.87 KB
/
SimulateNetwork.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
101
102
103
104
105
106
107
108
109
110
111
/* https://www.hackerearth.com/ja/challenges/hiring/globalsoft-backend-hiring-challenge/algorithm/efficient-network/
use #prim algorithm and then adjust dist.
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Scanner;
class SimulateNetwork {
public static void Prim(ArrayList<ArrayList<Node>> graph, int[] distArr, int src) {
int sz = graph.size();
boolean[] visitedArr = new boolean[sz];
Arrays.fill(visitedArr, false);
PriorityQueue<Node> pq = new PriorityQueue<>();
pq.add(new Node(src, 0));
distArr[src] = 0;
Node neighbour;
int id, w;
while (!pq.isEmpty()) {
id = pq.poll().id;
visitedArr[id] = true;
for (int i = 0; i < graph.get(id).size(); i++) {
neighbour = graph.get(id).get(i);
w = neighbour.latency;
if (!visitedArr[neighbour.id] && w < distArr[neighbour.id]) {
distArr[neighbour.id] = w;
pq.add(new Node(neighbour.id, w));
}
}
}
}
public static long getMinimumLatency(int[] distArr, int[] availableCableArr) {
long ans = 0;
if (availableCableArr != null) {
Arrays.sort(distArr);
Arrays.sort(availableCableArr);
int idx = 0;
int val = 0;
for (int i = distArr.length - 1; i > 0; i--) {
val = distArr[i];
if (idx < availableCableArr.length && val > availableCableArr[idx]) {
val = availableCableArr[idx];
idx++;
}
ans += val;
}
} else {
for (int val : distArr) {
ans += val;
}
}
return ans;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
ArrayList<ArrayList<Node>> graph = new ArrayList<>();
int sz = n + 1;
for (int i = 0; i < sz; i++) {
graph.add(new ArrayList<>());
}
int src, dest, latency;
for (int i = 0; i < m; i++) {
src = sc.nextInt();
dest = sc.nextInt();
latency = sc.nextInt();
if (src != dest) {
graph.get(src).add(new Node(dest, latency));
graph.get(dest).add(new Node(src, latency));
}
}
int[] distArr = new int[sz];
Arrays.fill(distArr, Integer.MAX_VALUE);
distArr[0] = 0;
Prim(graph, distArr, 1);
long ans;
int q = sc.nextInt();
if (q > 0) {
int[] availableCableArr = new int[q];
int tmp;
for (int i = 0; i < q; i++) {
tmp = sc.nextInt();
availableCableArr[i] = tmp;
}
ans = getMinimumLatency(distArr, availableCableArr);
} else {
ans = getMinimumLatency(distArr, null);
}
System.out.println(ans);
}
}
class Node implements Comparable<Node> {
int id, latency;
Node(int id, int latency) {
this.id = id;
this.latency = latency;
}
public int compareTo(Node other) {
return this.latency - other.latency;
}
}