function shortestPaths(graph, source) {
const dist = new Map([[source, 0]])
const pq = new MinHeap([[0, source]])
while (!pq.isEmpty()) {
const [cost, node] = pq.pop()
if (cost !== dist.get(node)) continue
for (const [next, weight] of graph[node]) {
const nextCost = cost + weight
if (nextCost < (dist.get(next) ?? Infinity)) {
dist.set(next, nextCost)
pq.push([nextCost, next])
}
}
}
}