1928. Minimum Cost to Reach Destination in Time
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```java []
import java.util.*;

class Solution {
    List<int[]>[] graph;

    public int minCost(int maxTime, int[][] edges, int[] passingFees) {
        int n = passingFees.length;
        graph = new List[n];
        Arrays.setAll(graph, i -> new ArrayList<>());

        for (int[] edge : edges) {
            graph[edge[0]].add(new int[]{edge[1], edge[2]});
            graph[edge[1]].add(new int[]{edge[0], edge[2]});
        }

        int[] minTime = new int[n];
        Arrays.fill(minTime, Integer.MAX_VALUE);

        int[][] minCost = new int[n][maxTime + 1];
        for (int[] row : minCost) Arrays.fill(row, Integer.MAX_VALUE);

        PriorityQueue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[1]));
        pq.offer(new int[]{0, passingFees[0], 0});

        minCost[0][0] = passingFees[0];

        while (!pq.isEmpty()) {
            int[] curr = pq.poll();
            int node = curr[0], cost = curr[1], time = curr[2];

            if (time > maxTime) continue;

            if (minCost[node][time] < cost) continue;

            if (node == n - 1) return cost;

            for (int[] next : graph[node]) {
                int nextNode = next[0], travelTime = next[1];
                int newTime = time + travelTime;
                int newCost = cost + passingFees[nextNode];

                if (newTime > maxTime) continue;

                if (newCost < minCost[nextNode][newTime]) {
                    minCost[nextNode][newTime] = newCost;
                    pq.offer(new int[]{nextNode, newCost, newTime});
                }
            }
        }

        return -1;
    }
}

```
['Java']