2
public int reachableNodes(int[][] edges, int maxMoves, int n) {3
int[][] graph = new int[n][n];4
for (int[] t : graph) {7
for (int[] t : edges) {8
graph[t[0]][t[1]] = t[2];9
graph[t[1]][t[0]] = t[2];11
PriorityQueue<int[]> heap = new PriorityQueue<>((a, b) -> b[1] - a[1]);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]) {22
// visiting the current node23
vis[nearestNodeId] = true;24
// since we visited this node we increment our counter26
for (int i = 0; i < n; i++) {27
// checking if we do have an edge28
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});32
int movesTaken = Math.min(maxMovesRemaining, graph[nearestNodeId][i]);33
graph[nearestNodeId][i] -= movesTaken;34
graph[i][nearestNodeId] -= movesTaken;