Boss

Boss: The Courier Network

The boss
Boss Fight!
Every passing test breaks a shield.

Storm season! The Guild's couriers carry lantern fuel between islands. The bridge-keepers have sent in their list of bridges, and every courier wants the fastest route, with every island on the way, before setting off.

You already have every piece:

  • Bridge Routes: turn a list of bridges into a network, and rebuild a route from a prev dict.
  • Closest Island First: Dijkstra's algorithm for the fastest times.

Now put them together. The one new idea: whenever Dijkstra finds a faster time to an island, also remember where it came from in prev. At the end, walk prev back from the goal, just like in Bridge Routes.

Assignment

Bridges are (from_island, to_island, minutes) tuples and are one-way.

  1. build_network(bridges): return a dict from each island to a list of (neighbor, minutes) tuples.
  2. shortest_times(network, start): return a dict of the fastest time to every reachable island (including start: 0), using Dijkstra.
  3. fastest_route(network, start, goal): return a tuple (minutes, path), where path is the list of islands from start to goal. Follow these steps:
    • Run Dijkstra as in step 2, but whenever you improve best[nxt], also set prev[nxt] = island.
    • If goal is not in best, return (None, []).
    • Otherwise rebuild the path from prev and return (best[goal], path).