Minimum Cost to Reach City With Discounts - Problem
Imagine you're planning a road trip across a network of cities connected by toll highways! ๐ฃ๏ธ
You have
Here's the twist: You have a limited number of discount coupons that can cut any highway's toll in half! Each coupon can only be used once, and you can use at most one coupon per highway.
Goal: Find the minimum cost to travel from city
Example: With highways
You have
n cities numbered from 0 to n-1, connected by a series of highways. Each highway is represented as [city1, city2, toll], meaning you can travel between these cities in either direction for the given toll cost.Here's the twist: You have a limited number of discount coupons that can cut any highway's toll in half! Each coupon can only be used once, and you can use at most one coupon per highway.
Goal: Find the minimum cost to travel from city
0 to city n-1, using your discounts strategically. If it's impossible to reach the destination, return -1.Example: With highways
[[0,1,4], [1,2,3], [0,2,10]] and 1 discount, you could go 0โ1 (cost 4) then 1โ2 with discount (cost 1), totaling 5 instead of the direct route's cost of 10. Input & Output
example_1.py โ Simple Path with Discount
$
Input:
n = 5, highways = [[0,1,4],[2,1,3],[1,4,11],[3,2,3],[3,4,2]], discounts = 1
โบ
Output:
9
๐ก Note:
The optimal path is 0โ1โ2โ3โ4. Costs are 4+3+3+2=12 normally. Using the discount on the highway 1โ4 (cost 11โ5) gives us path 0โ1โ4 with cost 4+5=9, which is optimal.
example_2.py โ Multiple Discounts
$
Input:
n = 4, highways = [[0,1,3],[2,3,2]], discounts = 0
โบ
Output:
-1
๐ก Note:
There's no path from city 0 to city 3 since the highways don't connect these cities. The graph has disconnected components: {0,1} and {2,3}.
example_3.py โ Strategic Discount Usage
$
Input:
n = 4, highways = [[0,1,10],[1,2,10],[2,3,10],[0,3,20]], discounts = 1
โบ
Output:
25
๐ก Note:
Without discounts: path 0โ1โ2โ3 costs 30, direct path 0โ3 costs 20. With 1 discount optimally used on 0โ3, we get cost 10. But the optimal is actually 0โ1โ2โ3 using discount on any 10-cost edge: 5+10+10=25. Wait, that's wrong. Actually 0โ3 with discount = 10 is optimal.
Constraints
- 1 โค n โค 1000
- 0 โค highways.length โค 1000
- highways[i].length == 3
- 0 โค city1i, city2i โค n - 1
- city1i โ city2i
- 0 โค tolli โค 105
- 0 โค discounts โค 500
- No duplicate highways between the same pair of cities
Visualization
Tap to expand
Understanding the Visualization
1
Build the Highway Network
Create a graph where cities are nodes and highways are bidirectional edges with toll costs
2
Track State as (City, Discounts Used)
Each state represents your current location and how many discount coupons you've used so far
3
Use Priority Queue for Minimum Cost
Always explore the cheapest unvisited state first, similar to Dijkstra's algorithm
4
For Each Highway, Consider Both Options
When moving to a neighboring city, try both using and not using a discount coupon
5
Return Cost When Destination Reached
The first time we reach the destination city, we've found the optimal cost
Key Takeaway
๐ฏ Key Insight: This problem transforms the classic shortest path problem into a 3D state space where we track (city, discounts_used, cost). By using Dijkstra's algorithm on this expanded state space, we can efficiently find the optimal way to use our limited discount coupons!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code