There is a country of n cities numbered from 0 to n - 1 where all the cities are connected by bi-directional roads. The roads are represented as a 2D integer array edges where edges[i] = [xi, yi, timei] denotes a road between cities xi and yi that takes timei minutes to travel. There may be multiple roads of differing travel times connecting the same two cities, but no road connects a city to itself.
Each time you pass through a city, you must pay a passing fee. This is represented as a 0-indexed integer array passingFees of length n where
passingFees[j] is the amount of dollars you must pay when you pass through city j.
In the beginning, you are at city 0 and want to reach city n - 1 in
maxTimeminutes or less. The cost of your journey is the summation of passing fees for each city that you passed through at some moment of your journey (including the source and destination cities).
Given maxTime, edges, and passingFees, return _theminimum cost to complete your journey, or _-1if you cannot complete it withinmaxTimeminutes.

Input: maxTime =30, edges =[[0,1,10],[1,2,10],[2,5,10],[0,3,1],[3,4,10],[4,5,15]], passingFees =[5,1,2,20,20,3] Output:11 Explanation: The path to take is0->1->2->5, which takes 30 minutes and has $11 worth of passing fees.
**** Input: maxTime =29, edges =[[0,1,10],[1,2,10],[2,5,10],[0,3,1],[3,4,10],[4,5,15]], passingFees =[5,1,2,20,20,3] Output:48 Explanation: The path to take is0->3->4->5, which takes 26 minutes and has $48 worth of passing fees. You cannot take path 0->1->2->5 since it would take too long.
Input: maxTime =25, edges =[[0,1,10],[1,2,10],[2,5,10],[0,3,1],[3,4,10],[4,5,15]], passingFees =[5,1,2,20,20,3] Output:-1 Explanation: There is no way to reach city 5 from city 0 within 25 minutes.
We need to find the minimum cost to reach the destination within a time limit, where each city has a passing fee. Since the time spent on each path matters, we use Dijkstra’s algorithm with an extra state for time spent so far.
classSolution {
publicintminCost(int maxTime, int[][] edges, int[] passingFees) {
int n = passingFees.length;
List<int[]>[] g =new ArrayList[n];
for (int i = 0; i < n; ++i) g[i]=new ArrayList<>();
for (int[] e : edges) {
g[e[0]].add(newint[]{e[1], e[2]});
g[e[1]].add(newint[]{e[0], e[2]});
}
int[][] cost =newint[n][maxTime+1];
for (int[] row : cost) Arrays.fill(row, Integer.MAX_VALUE);
PriorityQueue<int[]> pq =new PriorityQueue<>(Comparator.comparingInt(a->a[0]));
cost[0][0]= passingFees[0];
pq.offer(newint[]{passingFees[0], 0, 0});
while (!pq.isEmpty()) {
int[] cur = pq.poll();
int c = cur[0], u = cur[1], t = cur[2];
if (c > cost[u][t]) continue;
for (int[] e : g[u]) {
int v = e[0], w = e[1];
if (t + w <= maxTime && c + passingFees[v]< cost[v][t+w]) {
cost[v][t+w]= c + passingFees[v];
pq.offer(newint[]{cost[v][t+w], v, t+w});
}
}
}
int ans = Integer.MAX_VALUE;
for (int t = 0; t <= maxTime; ++t) ans = Math.min(ans, cost[n-1][t]);
return ans == Integer.MAX_VALUE?-1 : ans;
}
}
classSolution:
defminCost(self, maxTime: int, edges: list[list[int]], passingFees: list[int]) -> int:
import heapq
n = len(passingFees)
g = [[] for _ in range(n)]
for a, b, w in edges:
g[a].append((b, w))
g[b].append((a, w))
cost = [[float('inf')] * (maxTime+1) for _ in range(n)]
cost[0][0] = passingFees[0]
pq: list[tuple[int, int, int]] = [(passingFees[0], 0, 0)]
while pq:
c, u, t = heapq.heappop(pq)
if c > cost[u][t]: continuefor v, w in g[u]:
if t + w <= maxTime and c + passingFees[v] < cost[v][t+w]:
cost[v][t+w] = c + passingFees[v]
heapq.heappush(pq, (cost[v][t+w], v, t+w))
ans = min(cost[-1])
return-1if ans == float('inf') else ans