The problem
Find the fastest journey from station s to station t. Return a pair (time, route), where time is the least number of minutes and route is a list of stations from s to t that takes exactly that time. Return None if t cannot be reached.
edges = [(0, 1, 4), (0, 2, 1), (2, 1, 2),
(1, 3, 1), (2, 3, 5), (3, 4, 3)]
fastest(5, edges, 0, 4) # (7, [0, 2, 1, 3, 4])
fastest(5, edges, 4, 0) # None
fastest(5, edges, 2, 2) # (0, [2])
Lines are one-way, and times are whole numbers from 0 up. When several routes are equally fast, any of them is accepted: the tests add up the times of your route’s lines and compare them with your time and with the least time.
Limits
- Up to 102,002 stations and 200,000 lines.
- Looking through every station to find the nearest one is too slow here, and so is handling a station again for every old entry it left in the heap. The big tests have 3 or 4 seconds each.
Builds on PATH-3 Dijkstra.
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 | The start is the end | Not run | |
| 3 | No route | Not run | |
| 4 | More lines, less time | Not run | |
| 5 | A line given twice | Not run | |
| 6 | Zero-minute lines | Not run | |
| 7 | A value that goes down later | Not run | |
| 8 | 1,000 stations | Not run | |
| 9 | 100,000 stations, 200,000 lines | Not run | |
| 10 | 100,000 stations, the end cannot be reached | Not run | |
| 11 | 2,000 better offers for one busy station | Not run |