|
| 1 | +class MinHeap { |
| 2 | + constructor() { |
| 3 | + this.heap = []; |
| 4 | + } |
| 5 | + push(value) { |
| 6 | + this.heap.push(value); |
| 7 | + this.bubbleUp(); |
| 8 | + } |
| 9 | + |
| 10 | + pop() { |
| 11 | + if (this.heap.length === 1) return this.heap.pop(); |
| 12 | + const min = this.heap[0]; |
| 13 | + this.heap[0] = this.heap.pop(); |
| 14 | + this.bubbleDown(); |
| 15 | + return min; |
| 16 | + } |
| 17 | + |
| 18 | + bubbleUp() { |
| 19 | + let index = this.heap.length - 1; |
| 20 | + |
| 21 | + while (index > 0) { |
| 22 | + const parentIndex = Math.floor((index - 1) / 2); |
| 23 | + if (this.heap[parentIndex] <= this.heap[index]) break; |
| 24 | + [this.heap[parentIndex], this.heap[index]] = [ |
| 25 | + this.heap[index], |
| 26 | + this.heap[parentIndex], |
| 27 | + ]; |
| 28 | + index = parentIndex; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + bubbleDown() { |
| 33 | + let index = 0; |
| 34 | + const length = this.heap.length; |
| 35 | + |
| 36 | + while (true) { |
| 37 | + const left = 2 * index + 1; |
| 38 | + const right = 2 * index + 2; |
| 39 | + let smallest = index; |
| 40 | + |
| 41 | + if (left < length && this.heap[left] < this.heap[smallest]) { |
| 42 | + smallest = left; |
| 43 | + } |
| 44 | + |
| 45 | + if (right < length && this.heap[right] < this.heap[smallest]) { |
| 46 | + smallest = right; |
| 47 | + } |
| 48 | + |
| 49 | + if (smallest === index) break; |
| 50 | + |
| 51 | + [this.heap[index], this.heap[smallest]] = [ |
| 52 | + this.heap[smallest], |
| 53 | + this.heap[index], |
| 54 | + ]; |
| 55 | + |
| 56 | + index = smallest; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + least() { |
| 61 | + return this.heap[0]; |
| 62 | + } |
| 63 | + |
| 64 | + size() { |
| 65 | + return this.heap.length; |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +const input = require('fs') |
| 70 | + .readFileSync(process.platform === 'linux' ? '/dev/stdin' : './input.txt') |
| 71 | + .toString() |
| 72 | + .trim() |
| 73 | + .split('\n') |
| 74 | + .map((el) => el.split(' ').map(Number)); |
| 75 | + |
| 76 | +function solution(input) { |
| 77 | + const [N, M] = input[0]; |
| 78 | + const adjList = Array.from({ length: N + 1 }, () => []); |
| 79 | + |
| 80 | + for (let i = 1; i <= M; i++) { |
| 81 | + const [A, B, C] = input[i]; |
| 82 | + adjList[A].push([B, C]); |
| 83 | + adjList[B].push([A, C]); |
| 84 | + } |
| 85 | + |
| 86 | + function dijkstra(start) { |
| 87 | + const distances = Array(N + 1).fill(Infinity); |
| 88 | + const minHeap = new MinHeap(); |
| 89 | + minHeap.push([0, start]); |
| 90 | + distances[start] = 0; |
| 91 | + |
| 92 | + while (minHeap.size() > 0) { |
| 93 | + const [currentCost, currentNode] = minHeap.pop(); |
| 94 | + |
| 95 | + if (currentCost > distances[currentNode]) continue; |
| 96 | + |
| 97 | + for (const [nextNode, nextCost] of adjList[currentNode]) { |
| 98 | + const newCost = currentCost + nextCost; |
| 99 | + |
| 100 | + if (newCost < distances[nextNode]) { |
| 101 | + distances[nextNode] = newCost; |
| 102 | + minHeap.push([newCost, nextNode]); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return distances[N]; |
| 108 | + } |
| 109 | + |
| 110 | + return dijkstra(1); |
| 111 | +} |
| 112 | + |
| 113 | +console.log(solution(input)); |
0 commit comments