1
var countRestrictedPaths = function (n, edges) {2
// create an adjacency list to store the neighbors with weight for each node3
const adjList = new Array(n + 1).fill(null).map(() => new Array(0));4
for (const [node1, node2, weight] of edges) {5
adjList[node1].push([node2, weight]);6
adjList[node2].push([node1, weight]);9
// using djikstras algorithm10
// find the shortest path from n to each node11
const nodeToShortestPathDistance = {};12
const heap = new Heap();15
while (numNodeSeen < n) {16
const [node, culmativeWeight] = heap.pop();17
if (nodeToShortestPathDistance.hasOwnProperty(node)) {20
nodeToShortestPathDistance[node] = culmativeWeight;22
for (const [neighbor, edgeWeight] of adjList[node]) {23
if (nodeToShortestPathDistance.hasOwnProperty(neighbor)) {26
heap.push([neighbor, edgeWeight + culmativeWeight]);30
// do a DFS caching the number of paths for each node31
// so that we don't need to redo the number of paths again32
// when we visit that node through another path33
const nodeToNumPaths = {};34
const dfs = (node) => {35
if (node === n) return 1;36
if (nodeToNumPaths.hasOwnProperty(node)) {37
return nodeToNumPaths[node];40
for (const [neighbor, weight] of adjList[node]) {42
nodeToShortestPathDistance[node] > nodeToShortestPathDistance[neighbor]44
count += dfs(neighbor);47
return (nodeToNumPaths[node] = count % 1000000007);62
return this.store.length;66
if (this.store.length < 2) {67
return this.store.pop();69
const result = this.store[0];70
this.store[0] = this.store.pop();77
this.heapifyUp(this.store.length - 1);82
const parent = Math.floor((child - 1) / 2);83
if (this.shouldSwap(child, parent)) {84
[this.store[child], this.store[parent]] = [97
let [child, child2] = [1, 2]98
.map((x) => parent * 2 + x)99
.filter((x) => x < this.size());100
if (this.shouldSwap(child2, child)) {103
if (this.shouldSwap(child, parent)) {104
[this.store[child], this.store[parent]] = [115
shouldSwap(child, parent) {116
return child && this.store[child][1] < this.store[parent][1];