Watch 2 video solutions for Minimum Cost to Reach City With Discounts, a medium level problem involving Graph, Heap (Priority Queue), Shortest Path. This walkthrough by LetsCode has 685 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
A series of highways connect n cities numbered from 0 to n - 1. You are given a 2D integer array highways where highways[i] = [city1i, city2i, tolli] indicates that there is a highway that connects city1i and city2i, allowing a car to go from city1i to city2i and vice versa for a cost of tolli.
You are also given an integer discounts which represents the number of discounts you have. You can use a discount to travel across the ith highway for a cost of tolli / 2 (integer division). Each discount may only be used once, and you can only use at most one discount per highway.
Return the minimum total cost to go from city 0 to city n - 1, or -1 if it is not possible to go from city 0 to city n - 1.
Example 1:

Input: n = 5, highways = [[0,1,4],[2,1,3],[1,4,11],[3,2,3],[3,4,2]], discounts = 1 Output: 9 Explanation: Go from 0 to 1 for a cost of 4. Go from 1 to 4 and use a discount for a cost of 11 / 2 = 5. The minimum cost to go from 0 to 4 is 4 + 5 = 9.
Example 2:

Input: n = 4, highways = [[1,3,17],[1,2,7],[3,2,5],[0,1,6],[3,0,20]], discounts = 20 Output: 8 Explanation: Go from 0 to 1 and use a discount for a cost of 6 / 2 = 3. Go from 1 to 2 and use a discount for a cost of 7 / 2 = 3. Go from 2 to 3 and use a discount for a cost of 5 / 2 = 2. The minimum cost to go from 0 to 3 is 3 + 3 + 2 = 8.
Example 3:

Input: n = 4, highways = [[0,1,3],[2,3,2]], discounts = 0 Output: -1 Explanation: It is impossible to go from 0 to 3 so return -1.
Constraints:
2 <= n <= 10001 <= highways.length <= 1000highways[i].length == 30 <= city1i, city2i <= n - 1city1i != city2i0 <= tolli <= 1050 <= discounts <= 500Problem Overview: You are given n cities connected by roads with toll costs. You start at city 0 and want to reach city n-1. A limited number of discounts can halve the toll cost of a road (integer division). The task is to compute the minimum total travel cost when you can apply these discounts strategically.
Approach 1: Exhaustive DFS / Backtracking (Exponential Time, O(2^m) time, O(n) space)
The most direct idea is to explore every possible path from the start city to the destination while deciding for each edge whether to apply a discount. For each traversal, you branch into two states: use a discount or pay the full cost. This generates a large decision tree because each road potentially doubles the number of states. While conceptually simple, the number of paths grows exponentially with the number of edges, making this impractical even for moderate graph sizes. This approach mainly helps you reason about the problem structure and why a shortest-path optimization is required.
Approach 2: Dijkstra with Discount State (Optimal) (O((n + m) log(n * k)) time, O(n * k) space)
This problem is a variation of a weighted shortest path. Standard Dijkstra works on a single distance per node, but here the cost depends on how many discounts you've already used. The key idea is to treat (city, discounts_used) as the state. Maintain a priority queue ordered by total cost. From each state, you relax edges in two ways: travel normally or apply a discount (if available) which halves the toll cost. Distances are stored in a 2D array dist[city][discounts_used]. Each state can push new states into the heap, ensuring the cheapest path is processed first. This guarantees the optimal solution because all edge costs remain non‑negative. This pattern is common in problems combining constraints with shortest path algorithms.
Approach 3: Layered Graph Transformation (O((n*k + m*k) log(n*k)) time, O(n*k) space)
Another way to view the same idea is by building a layered graph. Each layer represents how many discounts have been used so far. Node (u, d) means you're at city u after using d discounts. For every road u → v, create two transitions: one within the same layer (full price) and another to the next layer (discount applied). After constructing this expanded graph with n * (discounts + 1) nodes, run classic Dijkstra once. The structure makes the state transition explicit and mirrors techniques used in constrained graph problems and priority-queue based path searches using a heap.
Recommended for interviews: Interviewers expect the modified Dijkstra approach. It demonstrates you can extend classic shortest-path algorithms by encoding additional state. Mentioning the brute force search shows you understand the decision space, but implementing the priority queue solution with (city, discounts_used) states proves strong graph and algorithm design skills.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| DFS / Backtracking | O(2^m) | O(n) | Conceptual exploration or very small graphs |
| Modified Dijkstra with Discount State | O((n+m) log(n*k)) | O(n*k) | Best practical solution for graphs with discount constraints |
| Layered Graph + Dijkstra | O((n*k + m*k) log(n*k)) | O(n*k) | When modeling constraints as graph layers for clarity |