|
| 1 | +/** |
| 2 | + * @param {number} n |
| 3 | + * @param {number[][]} edges |
| 4 | + * @return {number} |
| 5 | + */ |
| 6 | +const countRestrictedPaths = function(n, edges) { |
| 7 | + const g = {} |
| 8 | + for (let [u, v, w] of edges) { |
| 9 | + g[u] = g[u] || [] |
| 10 | + g[u].push([v, w]) |
| 11 | + g[v] = g[v] || [] |
| 12 | + g[v].push([u, w]) |
| 13 | + } |
| 14 | + const dist = Array(n + 1).fill(Infinity) |
| 15 | + dist[n] = 0 |
| 16 | + const pq = new PQ((a, b) => a[0] < b[0]) |
| 17 | + pq.push([0, n]) |
| 18 | + while(!pq.isEmpty()) { |
| 19 | + const [d, u] = pq.pop() |
| 20 | + if (d !== dist[u]) continue |
| 21 | + for (let [v, w] of (g[u] || [])) { |
| 22 | + if (dist[v] > dist[u] + w) { |
| 23 | + dist[v] = dist[u] + w |
| 24 | + pq.push([dist[v], v]) |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | + const mod = 1e9 + 7 |
| 29 | + const memo = Array(n + 1).fill(null) |
| 30 | + const dfs = (src) => { |
| 31 | + if (memo[src] !== null) return memo[src] |
| 32 | + if (src === n) return 1 |
| 33 | + let res = 0 |
| 34 | + for (let [v, w] of (g[src] || [])) { |
| 35 | + if (dist[src] > dist[v]) { |
| 36 | + res = (res + dfs(v)) % mod |
| 37 | + } |
| 38 | + } |
| 39 | + return memo[src] = res |
| 40 | + } |
| 41 | + return dfs(1) |
| 42 | +}; |
| 43 | + |
| 44 | +class PQ { |
| 45 | + constructor(comparator = (a, b) => a > b) { |
| 46 | + this.heap = [] |
| 47 | + this.top = 0 |
| 48 | + this.comparator = comparator |
| 49 | + } |
| 50 | + size() { |
| 51 | + return this.heap.length |
| 52 | + } |
| 53 | + isEmpty() { |
| 54 | + return this.size() === 0 |
| 55 | + } |
| 56 | + peek() { |
| 57 | + return this.heap[this.top] |
| 58 | + } |
| 59 | + push(...values) { |
| 60 | + values.forEach((value) => { |
| 61 | + this.heap.push(value) |
| 62 | + this.siftUp() |
| 63 | + }) |
| 64 | + return this.size() |
| 65 | + } |
| 66 | + pop() { |
| 67 | + const poppedValue = this.peek() |
| 68 | + const bottom = this.size() - 1 |
| 69 | + if (bottom > this.top) { |
| 70 | + this.swap(this.top, bottom) |
| 71 | + } |
| 72 | + this.heap.pop() |
| 73 | + this.siftDown() |
| 74 | + return poppedValue |
| 75 | + } |
| 76 | + replace(value) { |
| 77 | + const replacedValue = this.peek() |
| 78 | + this.heap[this.top] = value |
| 79 | + this.siftDown() |
| 80 | + return replacedValue |
| 81 | + } |
| 82 | + |
| 83 | + parent = (i) => ((i + 1) >>> 1) - 1 |
| 84 | + left = (i) => (i << 1) + 1 |
| 85 | + right = (i) => (i + 1) << 1 |
| 86 | + greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) |
| 87 | + swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) |
| 88 | + siftUp = () => { |
| 89 | + let node = this.size() - 1 |
| 90 | + while (node > this.top && this.greater(node, this.parent(node))) { |
| 91 | + this.swap(node, this.parent(node)) |
| 92 | + node = this.parent(node) |
| 93 | + } |
| 94 | + } |
| 95 | + siftDown = () => { |
| 96 | + let node = this.top |
| 97 | + while ( |
| 98 | + (this.left(node) < this.size() && this.greater(this.left(node), node)) || |
| 99 | + (this.right(node) < this.size() && this.greater(this.right(node), node)) |
| 100 | + ) { |
| 101 | + let maxChild = |
| 102 | + this.right(node) < this.size() && |
| 103 | + this.greater(this.right(node), this.left(node)) |
| 104 | + ? this.right(node) |
| 105 | + : this.left(node) |
| 106 | + this.swap(node, maxChild) |
| 107 | + node = maxChild |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | +// another |
| 113 | + |
| 114 | + |
1 | 115 | /**
|
2 | 116 | * @param {number} n
|
3 | 117 | * @param {number[][]} edges
|
|
0 commit comments