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