Minimum Cost to Reach City With Discounts - Problem

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.

Input & Output

Example 1 — Basic Highway Network
$ Input: n = 5, highways = [[0,1,4],[1,2,3],[2,3,8],[3,4,2],[1,3,6]], discounts = 1
Output: 9
💡 Note: Optimal path: 0→1→3→4. Without discount: 4+6+2=12. With discount on highway (1,3) with toll 6: 4+3+2=9. This is better than path 0→1→2→3→4 which costs 4+3+8+2=17, or 4+3+4+2=13 with discount on (2,3).
Example 2 — No Path Available
$ Input: n = 4, highways = [[0,1,3],[2,3,5]], discounts = 2
Output: -1
💡 Note: Cities 0-1 are connected, cities 2-3 are connected, but no path exists from city 0 to city 3. Return -1.
Example 3 — Single City
$ Input: n = 1, highways = [], discounts = 0
Output: 0
💡 Note: Already at destination (city 0 = city n-1 when n=1), so cost is 0.

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
  • There are no duplicate highways.

Visualization

Tap to expand
Minimum Cost to Reach City With Discounts INPUT Graph with 5 cities (n=5) 0 1 2 3 4 4 3 8 2 6 n = 5 discounts = 1 highways = [[0,1,4],[1,2,3], [2,3,8],[3,4,2],[1,3,6]] Start: 0, End: 4 ALGORITHM STEPS 1 Modified Dijkstra State: (cost, city, discounts_left) 2 Priority Queue Min-heap by cost 3 Two Choices Per Edge Full toll OR discounted (toll/2) 4 Track Best Cost For each (city, discounts_used) Exploration Trace (0,0,1) cost=0 (0,1,1) cost=4 [0-->1, full] (0,1,0) cost=2 [0-->1, disc] (2,3,0) cost=5 [1-->3, full] (8,4,0) cost=8 [3-->4, full] Optimal: 0-->1-->3-->4 Use discount on edge 0-->1 Cost: 2 + 6 + 2 = 8 FINAL RESULT Optimal Path Found 0 2 DISC 1 6 3 2 4 Cost Breakdown 0 --> 1: 4/2 = 2 (discount) 1 --> 3: 6 (full toll) 3 --> 4: 2 (full toll) Output 8 Key Insight: Use modified Dijkstra with state (city, discounts_remaining). For each edge, explore both options: pay full toll OR use a discount (toll/2). Track minimum cost for each unique state to avoid revisiting. Time: O((V+E) * D * log(V*D)) where D = number of discounts. Space: O(V * D) for the dist array. TutorialsPoint - Minimum Cost to Reach City With Discounts | Optimal Solution (Modified Dijkstra)
Asked in
Google 15 Amazon 12 Microsoft 8
28.5K Views
Medium Frequency
~25 min Avg. Time
890 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