PATH-P3

The fastest journey

Practice 3 of 7Shortest paths11 tests

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

  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.

11 tests, not run yet
Test results
#TestResultReads · time
1The exampleNot run
2The start is the endNot run
3No routeNot run
4More lines, less timeNot run
5A line given twiceNot run
6Zero-minute linesNot run
7A value that goes down laterNot run
81,000 stationsNot run
9100,000 stations, 200,000 linesNot run
10100,000 stations, the end cannot be reachedNot run
112,000 better offers for one busy stationNot run