|
| 1 | +const fs = require("fs"); |
| 2 | + |
| 3 | +const input = fs.readFileSync(0, "utf8").trim().split("\n"); |
| 4 | +const [N, M] = input[0].split(" ").map(Number); |
| 5 | + |
| 6 | +const graph = Array.from({ length: N + 1 }, () => []); |
| 7 | +for (let i = 1; i <= M; i++) { |
| 8 | + const [a, b, c, d] = input[i].split(" ").map(Number); |
| 9 | + graph[a].push({ to: b, cost: c, tree: d }); |
| 10 | + graph[b].push({ to: a, cost: c, tree: d }); |
| 11 | +} |
| 12 | + |
| 13 | +// ============================== |
| 14 | +// 自前の最小ヒープ (Priority Queue) |
| 15 | +// ============================== |
| 16 | +class MinHeap { |
| 17 | + constructor() { |
| 18 | + this.heap = []; |
| 19 | + } |
| 20 | + |
| 21 | + push(item) { |
| 22 | + this.heap.push(item); |
| 23 | + this._siftUp(); |
| 24 | + } |
| 25 | + |
| 26 | + pop() { |
| 27 | + if (this.heap.length === 0) return null; |
| 28 | + const top = this.heap[0]; |
| 29 | + const end = this.heap.pop(); |
| 30 | + if (this.heap.length !== 0) { |
| 31 | + this.heap[0] = end; |
| 32 | + this._siftDown(); |
| 33 | + } |
| 34 | + return top; |
| 35 | + } |
| 36 | + |
| 37 | + isEmpty() { |
| 38 | + return this.heap.length === 0; |
| 39 | + } |
| 40 | + |
| 41 | + _siftUp() { |
| 42 | + let i = this.heap.length - 1; |
| 43 | + while (i > 0) { |
| 44 | + const p = Math.floor((i - 1) / 2); |
| 45 | + if (this._compare(this.heap[i], this.heap[p]) < 0) { |
| 46 | + [this.heap[i], this.heap[p]] = [this.heap[p], this.heap[i]]; |
| 47 | + i = p; |
| 48 | + } else break; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + _siftDown() { |
| 53 | + let i = 0; |
| 54 | + const n = this.heap.length; |
| 55 | + while (true) { |
| 56 | + let smallest = i; |
| 57 | + const l = 2 * i + 1, r = 2 * i + 2; |
| 58 | + if (l < n && this._compare(this.heap[l], this.heap[smallest]) < 0) smallest = l; |
| 59 | + if (r < n && this._compare(this.heap[r], this.heap[smallest]) < 0) smallest = r; |
| 60 | + if (smallest === i) break; |
| 61 | + [this.heap[i], this.heap[smallest]] = [this.heap[smallest], this.heap[i]]; |
| 62 | + i = smallest; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // 辞書順比較: [距離, -木の本数] |
| 67 | + _compare(a, b) { |
| 68 | + if (a[0] !== b[0]) return a[0] - b[0]; |
| 69 | + return a[1] - b[1]; // -木の数が小さい = 木の数が多い |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// ============================== |
| 74 | +// Dijkstra with 木の本数管理 |
| 75 | +// ============================== |
| 76 | + |
| 77 | +const dist = Array(N + 1).fill(Infinity); |
| 78 | +const treeCount = Array(N + 1).fill(-Infinity); |
| 79 | +dist[1] = 0; |
| 80 | +treeCount[1] = 0; |
| 81 | + |
| 82 | +const pq = new MinHeap(); |
| 83 | +pq.push([0, 0, 1]); // [距離, -木の数, 頂点] |
| 84 | + |
| 85 | +while (!pq.isEmpty()) { |
| 86 | + const [curCost, negTrees, u] = pq.pop(); |
| 87 | + |
| 88 | + if (curCost > dist[u]) continue; |
| 89 | + if (curCost === dist[u] && -negTrees < treeCount[u]) continue; |
| 90 | + |
| 91 | + for (const { to: v, cost: c, tree: t } of graph[u]) { |
| 92 | + const newCost = curCost + c; |
| 93 | + const newTrees = -negTrees + t; |
| 94 | + if ( |
| 95 | + newCost < dist[v] || |
| 96 | + (newCost === dist[v] && newTrees > treeCount[v]) |
| 97 | + ) { |
| 98 | + dist[v] = newCost; |
| 99 | + treeCount[v] = newTrees; |
| 100 | + pq.push([newCost, -newTrees, v]); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +console.log(`${dist[N]} ${treeCount[N]}`); |
0 commit comments