|
| 1 | +/** |
| 2 | + * @param {number} n |
| 3 | + * @param {number[][]} roads |
| 4 | + * @return {number} |
| 5 | + */ |
| 6 | +const countPaths = function(n, roads) { |
| 7 | + const graph = {} |
| 8 | + for(let r of roads) { |
| 9 | + const [u, v, t] = r |
| 10 | + if(graph[u] == null) graph[u] = [] |
| 11 | + if(graph[v] == null) graph[v] = [] |
| 12 | + graph[u].push([v, t]) |
| 13 | + graph[v].push([u, t]) |
| 14 | + } |
| 15 | + |
| 16 | + return dijkstra(graph, n, 0) |
| 17 | + |
| 18 | + function dijkstra(graph, n, src) { |
| 19 | + const dist = Array(n).fill(Infinity) |
| 20 | + const ways = Array(n).fill(0), mod = 1e9 + 7 |
| 21 | + ways[src] = 1 |
| 22 | + dist[src] = 0 |
| 23 | + const pq = new PriorityQueue((a, b) => a[0] === b[0] ? a[1] < b[1] : a[0] < b[0]) |
| 24 | + pq.push([0, 0]) |
| 25 | + while(!pq.isEmpty()) { |
| 26 | + const [d, u] = pq.pop() |
| 27 | + if(d > dist[u]) continue |
| 28 | + if(graph[u] == null) graph[u] = [] |
| 29 | + for(const [v, time] of graph[u]) { |
| 30 | + if(dist[v] > d + time) { |
| 31 | + ways[v] = ways[u] |
| 32 | + dist[v] = d + time |
| 33 | + pq.push([dist[v], v]) |
| 34 | + } else if(dist[v] === d + time) { |
| 35 | + ways[v] = (ways[v] + ways[u]) % mod |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + return ways[n - 1] |
| 40 | + } |
| 41 | + |
| 42 | +}; |
| 43 | + |
| 44 | +class PriorityQueue { |
| 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 | + |
0 commit comments