Ctrl K

Checkpoints Within Reach

Hard
A subway has n stations numbered 0 to n - 1, and some pairs of stations are joined by tunnels. A tunnel tunnels[i] = [u, v, c] joins stations u and v and holds c checkpoints placed in a chain: u → checkpoint 1 → … → checkpoint c → v (with c = 0 the stations are joined directly). One step moves between neighbouring objects of a chain (a station and a checkpoint, two checkpoints, or two stations when c = 0). Tunnels can be walked in both directions. You start at station 0 and may take at most budget steps. An object (a station or a checkpoint) is reachable if you can get to it in at most budget steps. Return the number of reachable objects, counting station 0 itself.
Example 1
tunnels=[[0, 1, 3], [1, 2, 0]], budget=5, n=3 → 6

From station 0, four steps reach station 1, passing all 3 checkpoints, and one more step reaches station 2. That is 3 stations and 3 checkpoints: 6.

Example 2
tunnels=[[0, 1, 4], [0, 2, 4], [1, 2, 4]], budget=3, n=3 → 7

The other stations are out of reach, but along each of the two tunnels leaving station 0 you can pass 3 checkpoints: 1 + 3 + 3 = 7.

Example 3
tunnels=[[1, 2, 5]], budget=100, n=3 → 1

Station 0 has no tunnels, so only the station itself is reachable.

Constraints
  • 1 ≤ n ≤ 3000
  • 0 ≤ len(tunnels) ≤ min(n·(n − 1)/2, 10⁴)
  • tunnels[i] = [u, v, c], 0 ≤ u, v < n, u ≠ v
  • At most one tunnel between any two stations
  • 0 ≤ c ≤ 10⁴
  • 0 ≤ budget ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBall Into the Hole HardRepaint Arrows to Reach the Exit HardThrough the Walls to the Far Corner HardRooms on a Timer HardLengths for Roads Under Repair Hard
loading Python…
Input
tunnels=[[0, 1, 3], [1, 2, 0]], budget=5, n=3
Expected
6

Run checks the first 3 tests in your browser. Submit checks all 106 tests on the server, hidden ones included.