This is the core logic. For a neighbor with edge weight :
If taking the path through is shorter than the current best path to , update it.
if dist[u] + w < dist[v] then
dist[v] := dist[u] + w
pq.push((dist[v], v))
Relaxation improves distance estimates. Eventually, all estimates become exact shortest distances. The greedy order ensures you finalize nodes in shortest-distance order.