-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWaterConnectionProblem.java
More file actions
34 lines (26 loc) · 916 Bytes
/
WaterConnectionProblem.java
File metadata and controls
34 lines (26 loc) · 916 Bytes
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
package Greedy;
import java.util.ArrayList;
import java.util.Arrays;
public class WaterConnectionProblem {
ArrayList<ArrayList<Integer>> solve(int n, int p, ArrayList<Integer> a, ArrayList<Integer> b, ArrayList<Integer> d) {
ArrayList<ArrayList<Integer>> ans = new ArrayList<>();
int[] m = new int[n + 1];
int[] cost = new int[n + 1];
for (int i = 0; i < p; i++) {
m[a.get(i)] = b.get(i);
cost[b.get(i)] = d.get(i);
}
for (int i = 1; i <= n; i++) {
if (cost[i] == 0 && m[i] != 0) {
int end = i;
int dia = Integer.MAX_VALUE;
while (m[end] != 0) {
end = m[end];
dia = Math.min(dia, cost[end]);
}
ans.add(new ArrayList<>(Arrays.asList(i, end, dia)));
}
}
return ans;
}
}