Find cheapest routes with Dijkstra's algorithm
When edges have costs, the fewest edges is not the cheapest route. Dijkstra's algorithm always settles the closest unsettled node next, using a heap of tentative distances.
import heapq
frontier = [(0, "a")]
cost, node = heapq.heappop(frontier)Think before coding
With non-negative costs, the cheapest entry in the heap can never be improved later, so it is final. Skip stale heap entries whose cost is larger than the best known distance instead of deleting them. Work through the example by hand before changing the starter. Decide what should happen for an empty input, a single value and a repeated value where those cases apply. The checks use several inputs so a hard-coded answer cannot stand in for the rule.
Your task
Write distances(graph, start) where graph maps a node to a list of (neighbour, cost) pairs with non-negative costs. Return a dict of the cheapest cost to every reachable node.
The explanation opens here once every check passes.