Submission #66860095
Source Code Expand
import * as fs from 'fs';
const input = fs.readFileSync('/dev/stdin', 'utf8').trim().split('\n');
const [N, M] = input[0].split(' ').map(Number);
const graph: [number, number][][] = Array.from({ length: N }, () => []);
for (let i = 1; i <= M; i++) {
const [a, b, c] = input[i].split(' ').map(Number);
graph[a - 1].push([b - 1, c]);
graph[b - 1].push([a - 1, c]);
}
class MinHeap {
private heap: [number, number][] = [];
push(item: [number, number]) {
this.heap.push(item);
this.bubbleUp(this.heap.length - 1);
}
pop(): [number, number] | undefined {
if (this.heap.length === 0) return undefined;
const top = this.heap[0];
const end = this.heap.pop()!;
if (this.heap.length > 0) {
this.heap[0] = end;
this.bubbleDown(0);
}
return top;
}
private bubbleUp(index: number) {
while (index > 0) {
const parent = Math.floor((index - 1) / 2);
if (this.heap[parent][0] <= this.heap[index][0]) break;
[this.heap[parent], this.heap[index]] = [this.heap[index], this.heap[parent]];
index = parent;
}
}
private bubbleDown(index: number) {
const length = this.heap.length;
while (true) {
let smallest = index;
const left = 2 * index + 1;
const right = 2 * index + 2;
if (left < length && this.heap[left][0] < this.heap[smallest][0]) smallest = left;
if (right < length && this.heap[right][0] < this.heap[smallest][0]) smallest = right;
if (smallest === index) break;
[this.heap[index], this.heap[smallest]] = [this.heap[smallest], this.heap[index]];
index = smallest;
}
}
size(): number {
return this.heap.length;
}
}
const dist: number[] = Array(N).fill(Infinity);
dist[0] = 0;
const pq = new MinHeap();
pq.push([0, 0]); // [距離, 頂点]
while (pq.size() > 0) {
const [currentDist, u] = pq.pop()!;
if (currentDist > dist[u]) continue;
for (const [v, cost] of graph[u]) {
const newDist = currentDist + cost;
if (newDist < dist[v]) {
dist[v] = newDist;
pq.push([newDist, v]);
}
}
}
for (const d of dist) {
console.log(d === Infinity ? -1 : d);
}
Submission Info
| Submission Time | |
|---|---|
| Task | A64 - Shortest Path 2 |
| User | myoshizumi |
| Language | TypeScript 5.1 (Node.js 18.16.1) |
| Score | 1000 |
| Code Size | 2437 Byte |
| Status | AC |
| Exec Time | 639 ms |
| Memory | 132688 KiB |
Compile Error
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 1000 / 1000 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | sample00.txt, sample01.txt |
| All | 00.txt, 01_may_forget_continue.txt, 02_may_use_max_heap.txt, 03_may_use_max_heap.txt, 04.txt, 05.txt, 06.txt, 07.txt, sample00.txt, sample01.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 00.txt | AC | 204 ms | 104480 KiB |
| 01_may_forget_continue.txt | AC | 639 ms | 132688 KiB |
| 02_may_use_max_heap.txt | AC | 517 ms | 119528 KiB |
| 03_may_use_max_heap.txt | AC | 559 ms | 119660 KiB |
| 04.txt | AC | 257 ms | 58996 KiB |
| 05.txt | AC | 457 ms | 108328 KiB |
| 06.txt | AC | 39 ms | 42756 KiB |
| 07.txt | AC | 639 ms | 123704 KiB |
| sample00.txt | AC | 40 ms | 42780 KiB |
| sample01.txt | AC | 40 ms | 42828 KiB |