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
- Hint 1Nudge
- Hint 2Approach Open hint 1 first
- Hint 3Pseudocode Open hint 2 first
- Worked solutionlast resort Opens after all 3 hints
Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.
| # | Test | Result | Reads · time |
|---|---|---|---|
| 1 | The example | Not run | |
| 2 | A value too big | Not run | |
| 3 | A value too small | Not run | |
| 4 | The start is not 0 | Not run | |
| 5 | None for a station that can be reached | Not run | |
| 6 | A number for a station that cannot be reached | Not run | |
| 7 | Unreachable stations marked None | Not run | |
| 8 | Zero-minute lines in a loop | Not run | |
| 9 | Ties and zero-minute lines | Not run | |
| 10 | 100,000 stations, right | Not run | |
| 11 | 100,000 stations, one value too big | Not run | |
| 12 | 100,000 stations, a tenth cannot be reached | Not run |