1
var reachableNodes = function (edges, maxMoves, n) {2
const g = Array.from({ length: n }, () => []);4
for (let [u, v, cnt] of edges) {5
g[u].push([v, cnt + 1]);6
g[v].push([u, cnt + 1]);9
// find min budget to reach from 0 to all nodes10
const budget = new Array(n).fill(Infinity);12
const dijkstra = () => {13
// heap will be collection [node, weight]14
const heap = new MinPriorityQueue({ priority: (x) => x[1] });17
const [n, c] = heap.dequeue().element;18
for (let [nextNode, cost] of g[n]) {20
if (budget[nextNode] > temp) {21
budget[nextNode] = temp;22
heap.enqueue([nextNode, temp]);29
// add to sum all reachable nodes from 0 with max move31
for (let w of budget) vis += w <= maxMoves;33
// add intermediate nodes between edges with available budget34
for (let [a, b, c] of edges) {35
let [availableFromA, availableFromB] = [39
if (availableFromA < 0 || availableFromB < 0) {40
vis += Math.max(availableFromA, 0) + Math.max(availableFromB, 0);42
const total = availableFromA + availableFromB;43
vis += total - Math.max(total - c, 0);