Fastest Delivery Route with Fuel Station Stops
Trees & GraphsAuto-graded · 17 test casesLast updated
Tests the ability to extend Dijkstra's algorithm with a constrained resource (fuel), requiring candidates to track both position and remaining fuel as state.
Fastest Delivery Route with Fuel Station Stops
You are building a routing engine for a delivery fleet. The city is modeled as a directed, weighted graph of n intersections (numbered 0 to n-1) connected by roads. Each road has a travel time (positive integer).
Your vehicle starts at intersection 0 with a full tank holding exactly k units of fuel. Every road segment consumes 1 unit of fuel regardless of its travel time. Certain intersections are fuel stations — arriving at one instantly refills your tank to k.
Find the minimum total travel time to reach intersection n-1 from intersection 0. If it is impossible to reach the destination, return -1.
Details
- You begin at node
0withkfuel. Node0itself is not a fuel station (unless listed as one). - Arriving at a fuel station (including potentially node
n-1if it appears instations) refills tokimmediately. - You may visit the same node more than once (e.g., to refuel), but the optimal solution never needs more than
n * (k+1)state expansions. - If
n == 1, you are already at the destination; return0.
Input
| Parameter | Type | Description |
|---|---|---|
n | int | Number of intersections (1 ≤ n ≤ 300) |
roads | List[List[int]] | Each element is [u, v, t] — a directed road from u to v with travel time t (1 ≤ t ≤ 1000) |
stations | List[int] | Intersections that are fuel stations (may be empty) |
k | int | Full tank capacity (1 ≤ k ≤ 50) |
Constraints
1 ≤ n ≤ 3000 ≤ len(roads) ≤ 50000 ≤ len(stations) ≤ n1 ≤ k ≤ 50- Road travel times are positive integers (
1 ≤ t ≤ 1000) - There are no self-loops, but there may be multiple edges between the same pair of nodes.
- Stations list may contain duplicates; treat them as a set.
- Node indices in
roadsandstationsare in range[0, n-1].
Examples
Example 1
Input: fastest_delivery(4, [[0, 1, 2], [1, 2, 3], [2, 3, 5], [0, 2, 10], [1, 3, 20]], [2], 2)
Output: 10
Example 2
Input: fastest_delivery(3, [[0, 1, 1], [1, 2, 1]], [], 1)
Output: -1
Example 3
Input: fastest_delivery(1, [], [], 3)
Output: 0
Hints
Hint 1
The key insight is that the state must include both the current node AND the remaining fuel — two different arrivals at the same node with different fuel levels are distinct states for Dijkstra.
Hint 2
Use a min-heap on (travel_time, node, fuel_remaining). When you arrive at a fuel station, set fuel to k before exploring neighbors.
Hint 3
To avoid revisiting a state (node, fuel) with higher cost, maintain a 2D dist array: dist[node][fuel] = best known travel time to reach that node with that much fuel remaining.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Core Insight: State = (node, fuel_remaining)
A plain Dijkstra on nodes alone fails here because arriving at the same intersection with different fuel levels leads to different future options. We must treat the pair (node, fuel) as the Dijkstra state.
Algorithm
- Build an adjacency list from the roads.
- Initialize a 2D distance table
dist[node][fuel]to infinity. Setdist[0][k] = 0(start at node 0, full tank). - Run Dijkstra with a min-heap keyed on cumulative travel time:
- Pop
(time, node, fuel). - If this entry is stale (not the best known for this state), skip.
- If
node == n-1, returntime. - For each outgoing edge
(neighbor, t):- Consume 1 fuel. If fuel is already 0, skip all neighbors.
- If
neighboris a station, set fuel back tok. - Relax
dist[neighbor][new_fuel]iftime + tis better.
- Pop
- If the heap empties without reaching
n-1, return-1.
Why It Is Correct
Dijkstra's correctness relies on non-negative edge weights and the state space being finite. Both hold here: travel times are positive integers, and the state space n × (k+1) is finite (at most 300 × 51 = 15 300 states). Each state is finalized at most once, so the algorithm terminates and yields optimal results.
Complexity
- State space: O(n · k)
- Transitions per state: O(degree) — total O(E · k) relaxations
- Heap operations: O(E · k · log(n · k))
- Space: O(n · k + E)
With n ≤ 300, k ≤ 50, E ≤ 5000, this is comfortably within limits.
import heapq
from typing import List
def fastest_delivery(n: int, roads: List[List[int]], stations: List[int], k: int) -> int:
if n == 1:
return 0
station_set = set(stations)
adj = [[] for _ in range(n)]
for u, v, t in roads:
adj[u].append((v, t))
INF = float('inf')
dist = [[INF] * (k + 1) for _ in range(n)]
start_fuel = k
dist[0][start_fuel] = 0
heap = [(0, 0, start_fuel)]
while heap:
time, node, fuel = heapq.heappop(heap)
if time > dist[node][fuel]:
continue
if node == n - 1:
return time
for neighbor, t in adj[node]:
if fuel == 0:
break
new_fuel = fuel - 1
if neighbor in station_set:
new_fuel = k
new_time = time + t
if new_time < dist[neighbor][new_fuel]:
dist[neighbor][new_fuel] = new_time
heapq.heappush(heap, (new_time, neighbor, new_fuel))
return -1