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 city2iand 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 (integerdivision). Each discount may only be used once , and you can only use at most one discount per highway.
Return _theminimum total cost to go from city _0to cityn - 1, or-1if it is not possible to go from city0to cityn - 1.

Input: n =5, highways =[[0,1,4],[2,1,3],[1,4,11],[3,2,3],[3,4,2]], discounts =1Output: 9Explanation:
Go from 0 to 1for 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 4is4+5=9.
Example 2:
1
2
3
4
5
6
7
8

Input: n =4, highways =[[1,3,17],[1,2,7],[3,2,5],[0,1,6],[3,0,20]], discounts =20Output: 8Explanation:
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 3is3+3+2=8.
Example 3:
1
2
3
4
5

Input: n =4, highways =[[0,1,3],[2,3,2]], discounts =0Output: -1Explanation:
It is impossible to go from 0 to 3 so return-1.
We need to find the minimum cost from city 0 to city n-1, using up to discounts discounts, where each discount halves the toll on a highway. This is a shortest path problem with an extra state: the number of discounts used so far. Dijkstra’s algorithm can be adapted to handle this state.
classSolution {
publicintminimumCost(int n, int[][] highways, int discounts) {
List<int[]>[] g =new ArrayList[n];
for (int i = 0; i < n; ++i) g[i]=new ArrayList<>();
for (int[] h : highways) {
g[h[0]].add(newint[]{h[1], h[2]});
g[h[1]].add(newint[]{h[0], h[2]});
}
long[][] dist =newlong[n][discounts+1];
for (long[] row : dist) Arrays.fill(row, Long.MAX_VALUE);
dist[0][0]= 0;
PriorityQueue<long[]> pq =new PriorityQueue<>(Comparator.comparingLong(a->a[0]));
pq.offer(newlong[]{0, 0, 0});
while (!pq.isEmpty()) {
long[] cur = pq.poll();
long cost = cur[0]; int u = (int)cur[1], d = (int)cur[2];
if (cost > dist[u][d]) continue;
for (int[] e : g[u]) {
int v = e[0], w = e[1];
if (dist[v][d]> cost + w) {
dist[v][d]= cost + w;
pq.offer(newlong[]{dist[v][d], v, d});
}
if (d < discounts && dist[v][d+1]> cost + w/2) {
dist[v][d+1]= cost + w/2;
pq.offer(newlong[]{dist[v][d+1], v, d+1});
}
}
}
long ans = Long.MAX_VALUE;
for (int d = 0; d <= discounts; ++d) ans = Math.min(ans, dist[n-1][d]);
return ans == Long.MAX_VALUE?-1 : (int)ans;
}
}
classSolution:
defminimumCost(self, n: int, highways: list[list[int]], discounts: int) -> int:
import heapq
g = [[] for _ in range(n)]
for a, b, w in highways:
g[a].append((b, w))
g[b].append((a, w))
dist = [[float('inf')] * (discounts+1) for _ in range(n)]
dist[0][0] =0 pq: list[tuple[int, int, int]] = [(0, 0, 0)]
while pq:
cost, u, d = heapq.heappop(pq)
if cost > dist[u][d]: continuefor v, w in g[u]:
if dist[v][d] > cost + w:
dist[v][d] = cost + w
heapq.heappush(pq, (dist[v][d], v, d))
if d < discounts and dist[v][d+1] > cost + w//2:
dist[v][d+1] = cost + w//2 heapq.heappush(pq, (dist[v][d+1], v, d+1))
ans = min(dist[n-1])
return-1if ans == float('inf') else ans