1
class Solution {
2
public int reachableNodes(int[][] edges, int maxMoves, int n) {
3
int[][] graph = new int[n][n];
4
for (int[] t : graph) {
5
Arrays.fill(t, -1);
6
}
7
for (int[] t : edges) {
8
graph[t[0]][t[1]] = t[2];
9
graph[t[1]][t[0]] = t[2];
10
}
11
PriorityQueue<int[]> heap = new PriorityQueue<>((a, b) -> b[1] - a[1]);
12
int ans = 0;
13
boolean[] vis = new boolean[n];
14
heap.offer(new int[] {0, maxMoves});
15
while (!heap.isEmpty()) {
16
int[] info = heap.poll();
17
int nearestNodeId = info[0];
18
int maxMovesRemaining = info[1];
19
if (vis[nearestNodeId]) {
20
continue;
21
}
22
// visiting the current node
23
vis[nearestNodeId] = true;
24
// since we visited this node we increment our counter
25
ans++;
26
for (int i = 0; i < n; i++) {
27
// checking if we do have an edge
28
if (graph[nearestNodeId][i] != -1) {
29
if (!vis[i] && maxMovesRemaining >= graph[nearestNodeId][i] + 1) {
30
heap.offer(new int[] {i, maxMovesRemaining - graph[nearestNodeId][i] - 1});
31
}
32
int movesTaken = Math.min(maxMovesRemaining, graph[nearestNodeId][i]);
33
graph[nearestNodeId][i] -= movesTaken;
34
graph[i][nearestNodeId] -= movesTaken;
35
ans += movesTaken;
36
}
37
}
38
}
39
return ans;
40
}
41
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0