diff --git a/code_study/Baekjoon/ts/1916.ts b/code_study/Baekjoon/ts/1916.ts new file mode 100644 index 0000000..ee9909c --- /dev/null +++ b/code_study/Baekjoon/ts/1916.ts @@ -0,0 +1,83 @@ +export {}; + +class PriorityQueue { + private heap: number[][]; + + constructor() { + this.heap = []; + } + + isEmpty(): boolean { + return this.heap.length === 0; + } + + push(node: number[]) { + this.heap.push(node); + let index = this.heap.length - 1; + while (index > 0) { + const parentIndex: number = Math.floor((index - 1) / 2); + if (this.heap[parentIndex][1] <= this.heap[index][1]) break; + + [this.heap[parentIndex], this.heap[index]] = [this.heap[index], this.heap[parentIndex]]; + index = parentIndex; + } + } + + pop(): number[] { + if (this.isEmpty()) return [-1,-1]; + + const root: number[] = this.heap[0]; + const lastNode = this.heap.pop(); + + if (!this.isEmpty()) { + this.heap[0] = lastNode; + let index: number = 0; + const length: number = this.heap.length; + + while (true) { + let smallest: number = index; + const leftChildIndex: number = 2 * index + 1; + const rightChildIndex: number = 2 * index + 2; + + if (leftChildIndex < length && this.heap[leftChildIndex][1] < this.heap[smallest][1]) smallest = leftChildIndex; + if (rightChildIndex < length && this.heap[rightChildIndex][1] < this.heap[smallest][1]) smallest = rightChildIndex; + if (smallest === index) break; + + [this.heap[index], this.heap[smallest]] = [this.heap[smallest], this.heap[index]]; + index = smallest; + } + } + + return root; + } +} + +const input: string[] = require("fs").readFileSync(0, "utf8").toString().trim().split('\n'); +const [N, M]: number[] = [input[0], input[1]].map(Number); +let adj: number[][][] = Array.from({length: N+1}, () => []); + +for(let i=0; i distance[current] + nw) { + distance[next] = distance[current] + nw; + pq.push([next, distance[next]]); + } + } +} + +console.log(distance[v]); \ No newline at end of file