Rohim’s routes from the mill on the village road map: a real binary heap, the nearest village settled, its roads relaxed, the route read back through asal…
Why it matters
Map navigation, network routing, supply chains, games: all look for the cheapest route in a weighted graph. Dijkstra is the basis, and three things often go wrong: using it with negative weights, scanning every node each step, and forgetting to keep asal for the route.
The key idea
What leaves the queue is surely shortest
Common mistakes
Using Dijkstra with negative weights The guarantee "settled means shortest" breaks: a negative road found later can shorten a village already settled. Use Bellman-Ford, which can also detect negative cycles.
Scanning every node each step That is O(V²). Use a min-heap to take the nearest in O(log V): O((V + E) log V). On a small map they are about the same; on a big one, tens of times apart.
Keeping only distances, forgetting the route Keep asal (often called prev) every time a distance improves, then read back from the goal to the start.
Try it in this playground
Until the route is read Run to the end and watch the route read back through asal.
A stale entry Watch the queue give up a village already settled, and skip it.
Change one road Pick a road, change its length, run again.
Try a negative road Give a road a value below zero and read why it is refused.