const best = new Array(n).fill(Infinity);
best[start] = 0;
const heap = new MinHeap([[0, start]]);
while (heap.size() > 0) {
const [time, node] = heap.pop();
if (time > best[node]) continue;
for (const [next, cost] of roads[node]) {
if (time + cost < best[next]) {
best[next] = time + cost;
heap.push([best[next], next]);
}
}
}
const parent = Array.from({ length: n }, (_, i) => i);
const find = (x: number): number => {
if (parent[x] !== x) parent[x] = find(parent[x]);
return parent[x];
};
const union = (a: number, b: number): boolean => {
const leaderA = find(a), leaderB = find(b);
if (leaderA === leaderB) return false;
parent[leaderB] = leaderA;
return true;
};