3
* @param {number[][]} roads6
var countPaths = function (n, roads) {8
dist = Array(n).fill(Infinity),9
minHeap = new MinHeap(),10
count = Array(n).fill(1);13
for (let [from, to, weight] of roads) {14
adj[from] = adj[from] || [];15
adj[from].push([to, weight]);16
adj[to] = adj[to] || [];17
adj[to].push([from, weight]);22
minHeap.enqueue(n - 1, 0);24
while (minHeap.getSize()) {25
let [node, curDist] = minHeap.dequeue();26
let children = adj[node] || [];27
for (let [childNode, childWeight] of children) {28
//IF PATH ALREADY FOUND WITH THIS VALUE THEN ADD COUNT REACHED UNTIL NOW29
if (curDist + childWeight === dist[childNode]) {30
count[childNode] = (count[childNode] + count[node]) % 1000000007;31
} //IF NOT PATH FOUND YET THEN ADD COUNT OF WAYS TO THE NODE WE CAME FROM32
else if (curDist + childWeight < dist[childNode]) {33
count[childNode] = count[node];34
dist[childNode] = curDist + childWeight;35
minHeap.enqueue(childNode, dist[childNode]);43
constructor(list = []) {50
for (let [val, priority] of this.list) this.enqueue(val, priority);54
[this.tree[pos1], this.tree[pos2]] = [this.tree[pos2], this.tree[pos1]];57
enqueue(val, priority) {58
this.tree[this.tree.length] = [val, priority];59
let i = this.tree.length - 1,62
if (this.tree[parent][1] > this.tree[i][1]) this.swap(parent, i);69
let size = this.tree.length - 1,72
let last = this.tree.pop(),73
deleted = this.tree[pos];74
if (!deleted && last) return last;75
this.tree[pos] = last;81
if (pos > this.tree.length) return;82
let leftPos = 2 * pos,83
rightPos = 2 * pos + 1;84
let left = this.tree[leftPos] ? this.tree[leftPos][1] : Infinity;85
let right = this.tree[rightPos] ? this.tree[rightPos][1] : Infinity,95
if (this.tree[pos][1] > minVal) {96
this.swap(pos, minIndex);97
this.heapify(minIndex);102
console.log(this.tree.slice(1));106
return this.tree.length - 1;