PATH-P1

Are these the shortest times?

Practice 1 of 7Shortest paths12 tests

The problem

Someone has worked out the shortest travel time from station s to every station, and handed you the list d. Check it without trusting it. Return True if every entry is right: d[v] is the least number of minutes from s to v, or None exactly when v cannot be reached. Otherwise return False.

edges = [(0, 1, 4), (0, 2, 1), (2, 1, 2),
         (1, 3, 1), (2, 3, 5), (3, 4, 3)]
are_shortest(5, edges, 0, [0, 3, 1, 4, 7])   # True
are_shortest(5, edges, 0, [0, 4, 1, 5, 8])   # False

In the second call, the line 2 → 1 reaches 1 in 1 + 2 = 3 minutes, less than the claimed 4. Lines are one-way, and times are whole numbers from 0 up. A line can take 0 minutes.

Limits

  • Up to 100,000 stations and about 150,000 lines.
  • Aim for O(V + E): read each line a fixed number of times. The big tests have 3 seconds each. Running a shortest-path search yourself and comparing is accepted too.

Builds on PATH-1 The problem.

Continue on a laptop

Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.

Need a hint?0 of 3 opened

  1. Hint 1Nudge
  2. Hint 2Approach Open hint 1 first
  3. Hint 3Pseudocode Open hint 2 first
  4. Worked solutionlast resort Opens after all 3 hints
Python waits for a wider screensolution.py

Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.

12 tests, not run yet
Test results
#TestResultReads · time
1The exampleNot run
2A value too bigNot run
3A value too smallNot run
4The start is not 0Not run
5None for a station that can be reachedNot run
6A number for a station that cannot be reachedNot run
7Unreachable stations marked NoneNot run
8Zero-minute lines in a loopNot run
9Ties and zero-minute linesNot run
10100,000 stations, rightNot run
11100,000 stations, one value too bigNot run
12100,000 stations, a tenth cannot be reachedNot run