Minimum Cost to Reach City With Discounts Problem
Description
LeetCode Problem 2093.
A series of highways connect n cities numbered from 0 to n - 1. You are given a 2D integer array highways where highways[i] = [city1_i, city2_i, toll_i] indicates that there is a highway that connects city1_i and city2_i, allowing a car to go from city1_i to city2_i and vice versa for a cost of toll_i.
You are also given an integer discounts which represents the number of discounts you have. You can use a discount to travel across the i^th highway for a cost of toll_i / 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 <= city1_i, city2_i <= n - 1city1_i != city2_i0 <= toll_i <= 10^50 <= discounts <= 500- There are no duplicate highways.
Solution
Approach: Dijkstra on (City, Discounts Used)
Using a discount changes what the rest of the trip can do, so the state is the pair (city, number of discounts used so far), not just the city. All costs are non-negative, so Dijkstra's algorithm with a min-heap works.
From state (i, k) with cost c, for each highway (i, j, toll) push two next states:
- pay the full toll:
(j, k)with costc + toll; - use a discount (only if
k < discounts):(j, k + 1)with costc + toll / 2.
dist[i][k] keeps the best cost found for each state, so a worse revisit is skipped. The first time city n - 1 is popped its cost is minimal; if the heap empties first, return -1.
Complexity: Time O(E * D * log(E * D)) with E highways and D = discounts + 1; space O(E * D + n * D).
from collections import defaultdict
from heapq import heappop, heappush
from math import inf
class Solution:
def minimumCost(self, n: int, highways: List[List[int]], discounts: int) -> int:
g = defaultdict(list)
for a, b, c in highways:
g[a].append((b, c))
g[b].append((a, c))
q = [(0, 0, 0)]
dist = [[inf] * (discounts + 1) for _ in range(n)]
while q:
cost, i, k = heappop(q)
if k > discounts:
continue
if i == n - 1:
return cost
if dist[i][k] > cost:
dist[i][k] = cost
for j, v in g[i]:
heappush(q, (cost + v, j, k))
heappush(q, (cost + v // 2, j, k + 1))
return -1