Fastest delivery time to every depot

Graphs Senior Python 3.13+ Reviewed By Best Latest Apps

Predict the output

What does this print?

Python 3.13
import heapq
roads = {0: [(1, 4), (2, 1)], 2: [(1, 2)], 1: []}
best = {0: 0}
heap = [(0, 0)]
order = []
while heap:
    t, d = heapq.heappop(heap)
    if t > best[d]:
        continue
    order.append(d)
    for nxt, w in roads[d]:
        if t + w < best.get(nxt, 10 ** 9):
            best[nxt] = t + w
            heapq.heappush(heap, (t + w, nxt))
print(order, best[1])

Options

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A [0, 2, 1] 3

Right: going via depot 2 (1 + 2) beats the direct road of 4.

The heap pops depots in order of time, and the stale entry (4, 1) is skipped because 3 is already better.

Why not the others?
  • B [0, 1, 2] 4. Depot 2 at time 1 is popped before depot 1 at time 4.
  • C [0, 2, 1] 4. The route through depot 2 lowers depot 1 to 3.
Dijkstra settles depots 0 to 4 in order of delivery time; a second run starts from depot 2.

Steps come from running the Python solution.

Text version

From depot 0:

Nodes [0, 1, 2, 3, 4]; edges 0→1 (4), 0→2 (1), 2→1 (2), 1→3 (1), 2→3 (5), 3→4 (3).

  1. Visit 0, distances [0, 4, 1, ∞, ∞]. Settle 0 at 0; improve 1, 2
  2. Visit 2, distances [0, 3, 1, 6, ∞]. Settle 2 at 1; improve 1, 3
  3. Visit 1, distances [0, 3, 1, 4, ∞]. Settle 1 at 3; improve 3
  4. Visit 3, distances [0, 3, 1, 4, 7]. Settle 3 at 4; improve 4
  5. Visit 4, distances [0, 3, 1, 4, 7]. Settle 4 at 7

Result: [0, 3, 1, 4, 7] (-1 = no route)

From depot 2:

Nodes [0, 1, 2, 3, 4]; edges 0→1 (4), 0→2 (1), 2→1 (2), 1→3 (1), 2→3 (5), 3→4 (3).

  1. Visit 2, distances [∞, 2, 0, 5, ∞]. Settle 2 at 0; improve 1, 3
  2. Visit 1, distances [∞, 2, 0, 3, ∞]. Settle 1 at 2; improve 3
  3. Visit 3, distances [∞, 2, 0, 3, 6]. Settle 3 at 3; improve 4
  4. Visit 4, distances [∞, 2, 0, 3, 6]. Settle 4 at 6

Result: [-1, 2, 0, 3, 6] (-1 = no route)

30-second answer

Dijkstra's algorithm: keep a min-heap of (time, depot). Pop the closest unsettled depot and try every road out of it; if that gives a faster time to a neighbour, record it and push it. Stale heap entries are skipped when popped. O((V + E) log V) time, O(V + E) space.

Practise saying it · 60 s. No recording, just a timer

Explain Dijkstra's algorithm and its requirements.

Say your answer out loud, then check which of these points you covered:

  • Min-heap of (time, node)
  • Skip stale entries
  • Non-negative weights; O((V+E) log V)

Deep dive

Non-negative times are required: once a depot is popped, no later route can be faster.

Skipping stale entries (if t > best[d]: continue) replaces a decrease-key operation.

With all times equal, BFS gives the same answer in O(V + E).

Python solution Ran on Python 3.13.5 · mypy --strict
Python 3.13
import heapq


def fastest_times(n: int, roads: list[list[int]],
                  source: int) -> list[int]:
    out: list[list[tuple[int, int]]] = [[] for _ in range(n)]
    for a, b, minutes in roads:
        out[a].append((b, minutes))
    best = [-1] * n
    best[source] = 0
    heap = [(0, source)]
    while heap:
        t, d = heapq.heappop(heap)
        if t > best[d]:
            continue
        for nxt, minutes in out[d]:
            if best[nxt] < 0 or t + minutes < best[nxt]:
                best[nxt] = t + minutes
                heapq.heappush(heap, (t + minutes, nxt))
    return best

Common mistakes

  • Settling a depot when it is pushed rather than when it is popped.
  • Using Dijkstra with negative times; use Bellman-Ford instead.

Senior take & follow-ups

State the non-negative requirement and why it matters before you code.

Follow-up questions

Think of your answer first, then open the hint.

  1. What if some roads have negative times?

    Show hint

    Bellman-Ford in O(V·E), which also detects negative cycles.

  2. How would you limit the number of roads used?

    Show hint

    Track (time, depot, roads used) states, or run k rounds of Bellman-Ford.

See it animated on iPhone (Dijkstra Algorithm in DSA Master - Data & Algorithms)

What each operation costs: Breadth First Search · Depth First Search · Topological Sort · Dijkstra Algorithm

From the author

DSA Master - Data & Algorithms

Free on iPhone and iPad: watch 50+ data structures and algorithms step through animations, run the code in the playground, and try the daily challenge.

Suggest a fix if something here is wrong or out of date.