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 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
0123$6$4$2$3๐ŸŽŸ๏ธ Strategic Discount Usage (1 coupon available)๐Ÿ’ก Key Insight: We need to track (city, discounts_used) as our state๐Ÿ” Path Analysis: 0โ†’2โ†’3 costs $4+$3=$7, or with discount: $2+$3=$5๐Ÿ” Alternative: 0โ†’1โ†’3 costs $6+$2=$8, or with discount: $3+$2=$5โœ… Optimal: Both discounted paths cost $5 (use coupon on the $4 or $6 toll)STARTTARGET๐ŸŽŸ๏ธDiscountCoupon๐Ÿ›ฃ๏ธ Minimum Cost Highway Route with Discounts
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!
Asked in
Google 23 Amazon 19 Meta 15 Microsoft 12
28.5K Views
Medium Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen