Minimize Fuel Cost Across Cities
MicrosoftSoftware Engineer · OnsiteLast updated
Calculate the minimum monetary cost to travel between two cities in a road network where fuel is consumed per unit distance and can be stockpiled with unlimited capacity at local city prices.
Problem Statement
You are planning a road trip across a network of n cities labeled from 0 to n - 1. You are given an undirected weighted graph represented by edges, where edges[i] = [u, v, dist] indicates a bidirectional road between city u and city v requiring dist units of fuel to traverse.
Each city has a gas station with a specific fuel price given in the array prices, where prices[i] is the price per unit of fuel at city i. Your vehicle has an unlimited fuel tank capacity, meaning you can purchase and store as much fuel as you want at any city you visit. You start at city start with an empty fuel tank and must reach city target.
Because tank capacity is unlimited, any fuel used along an edge u -> v is optimally purchased at the cheapest city visited on or before city u along your route.
Return the minimum total fuel cost to travel from start to target. If target is unreachable from start, return -1.
Function Signature
def min_fuel_cost(n: int, edges: list[list[int]], prices: list[int], start: int, target: int) -> int:
pass
Constraints
1 <= n <= 5000 <= len(edges) <= 2000edges[i] = [u, v, dist]where0 <= u, v < n,u != v, and1 <= dist <= 10^4prices.length == n1 <= prices[i] <= 10^40 <= start, target < n- The graph may have multiple edges between the same pair of cities; you may assume all edge weights are positive.
Examples
Example 1
Input:
n = 3
edges = [[0, 1, 5], [1, 2, 10], [0, 2, 12]]
prices = [10, 2, 20]
start = 0
target = 2
Output: 70
Explanation:
Path 1 (direct): 0 -> 2
Fuel needed: 12. Cheapest station seen so far is city 0 (price 10).
Cost = 12 * 10 = 120.
Path 2: 0 -> 1 -> 2
- Edge 0 -> 1 (dist 5): Bought at city 0 (price 10) = 5 * 10 = 50.
- At city 1, fuel price is 2, which is lower than city 0.
- Edge 1 -> 2 (dist 10): Bought at city 1 (price 2) = 10 * 2 = 20.
Total cost = 50 + 20 = 70.
Example 2
Input:
n = 4
edges = [[0, 1, 4], [2, 3, 3]]
prices = [5, 4, 3, 2]
start = 0
target = 3
Output: -1
Explanation:
There is no path connecting city 0 and city 3.
Hints
Hint 1
Because your carrying capacity is unlimited, the optimal price paid for an edge u -> v is the minimum fuel price among all cities visited up to that point.
Hint 2
A standard shortest-path search fails because a path with higher accumulated cost might visit a city with a much lower fuel price that pays off later.
Hint 3
Model your state in Dijkstra's algorithm as (city, min_price_seen) and prune any state where an existing visit to the same city achieved both a lower cost and a lower or equal fuel price.
Follow-up questions
- How does the state space and complexity change if the fuel tank has a strict finite capacity C?
- Can you precompute the all-pairs shortest paths to avoid searching through high-price intermediate cities?
- How would you handle negative edge weights or negative prices if subsidies were introduced at certain cities?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.