1928. Minimum Cost to Reach Destination in Time

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 maxTime minutes 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 the minimum cost to complete your journey, or -1 if you cannot complete it within maxTime minutes.

Example 1:

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 is 0 -> 1 -> 2 -> 5, which takes 30 minutes and has $11 worth of passing fees.

Example 2:

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 is 0 -> 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.

Example 3:

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.

Constraints:

  • 1 <= maxTime <= 1000

  • n == passingFees.length

  • 2 <= n <= 1000

  • n - 1 <= edges.length <= 1000

  • 0 <= xi, yi <= n - 1

  • 1 <= timei <= 1000

  • 1 <= passingFees[j] <= 1000

  • The graph may contain multiple edges between two nodes.

  • The graph does not contain self loops.

Solution

Dijkstra's Algo to find the path with minimum cost.

With PriorityQueue

With A table to filter the unlikely ok path. ( Both spend time and fee are less than last time we visited the same city)

    public int minCost(int maxTime, int[][] edges, int[] passingFees) {
        int n = passingFees.length;
        Map<Integer, List<int[]>> map = new HashMap<>();
        
        for(int[] edge : edges){
            if(!map.containsKey(edge[0])){
                map.put(edge[0], new LinkedList<int[]>());   
            } if(!map.containsKey(edge[1])){
                map.put(edge[1], new LinkedList<int[]>());   
            }
            map.get(edge[0]).add(new int[]{edge[1], edge[2]});
            map.get(edge[1]).add(new int[]{edge[0], edge[2]});
        }
        
        //[cost, index, spendTime]
        PriorityQueue<int[]> pq = new PriorityQueue<>((a,b) -> {
            if(a[0] != b[0]) return Integer.compare(a[0], b[0]);
            return Integer.compare(a[2], b[2]);
        });
        int[] minSpendTime = new int[n];
        for(int i = 0; i < n; i++){
            minSpendTime[i] = Integer.MAX_VALUE;   
        }
        pq.add(new int[]{passingFees[0], 0, 0});
        
        
        while(!pq.isEmpty()) {
            int[] curCity = pq.poll();
            int fee = curCity[0];
            int index = curCity[1];
            int time = curCity[2];
            //because with pq, we can ensure the next fee is greater than current fee,
            //So, all we must make sure is that the current time shall smaller than prev time
            if(time > maxTime || time >= minSpendTime[index]) continue;
            minSpendTime[index] = time;
            if(index == n-1) return fee;
            for(int next[] : map.get(index)) {
                int newfee = fee + passingFees[next[0]];
                int newTime = time + next[1];
                pq.add(new int[]{newfee, next[0], newTime});
            } 
        }
        return -1;
    }

Last updated

Was this helpful?