✅Dijkstra Algo | JAVA | Easy to understand
Code


# Code
```java []
class Solution {
    public int minCost(int maxTime, int[][] edges, int[] fee) {
    List<List<int[]>> adjList = new ArrayList<>();
    for (int i = 0; i < fee.length; i++) {
        adjList.add(new ArrayList<>());
    }
    for (int[] edge : edges) {
        int u = edge[0];
        int v = edge[1];
        int t = edge[2];
        adjList.get(u).add(new int[]{v, t});
        adjList.get(v).add(new int[]{u, t});
    }

    PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> {
        if (a[1] == b[1]) {
            return a[2] - b[2];
        }
        return a[1] - b[1];
    });

    pq.add(new int[]{0, fee[0], 0});

    int n = fee.length;
    int[] dist = new int[n];
    int[] times = new int[n];

    Arrays.fill(dist, Integer.MAX_VALUE);
    Arrays.fill(times, Integer.MAX_VALUE);

    dist[0] = 0;
    times[0] = 0;

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

        if (time > maxTime) {
            continue;
        }

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

        for (int[] neighbor : adjList.get(node)) {
            int neiNode = neighbor[0];
            int neiCost = fee[neiNode];

            if (cost + neiCost < dist[neiNode]) {
                dist[neiNode] = cost + neiCost;
                pq.add(new int[]{neiNode, cost + neiCost, time + neighbor[1]});
                times[neiNode] = time + neighbor[1];
            } else if (time + neighbor[1] < times[neiNode]) {
                pq.add(new int[]{neiNode, cost + neiCost, time + neighbor[1]});
                times[neiNode] = time + neighbor[1];
            }
        }
    }
    return dist[n - 1] == Integer.MAX_VALUE || times[n - 1] > maxTime ? -1 : dist[n - 1];
}

}
```
['Java']