787. Cheapest Flights Within K Stops

Posted tobeabetterpig

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了787. Cheapest Flights Within K Stops相关的知识,希望对你有一定的参考价值。

There are n cities connected by m flights. Each fight starts from city u and arrives at v with a price w.
Now given all the cities and flights, together with starting city src and the destination dst, your task is to find the cheapest price from src to dst with up to k stops. If there is no such route, output -1.
Example 1:
Input: 
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
src = 0, dst = 2, k = 1
Output: 200
Explanation: 
The graph looks like this:
?

The cheapest price from city 0 to city 2 with at most 1 stop costs 200, as marked red in the picture.
Example 2:
Input: 
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
src = 0, dst = 2, k = 0
Output: 500
Explanation: 
The graph looks like this:
?

The cheapest price from city 0 to city 2 with at most 0 stop costs 500, as marked blue in the picture.











https://www.youtube.com/watch?v=FSm1zybd0Tk


https://www.jianshu.com/p/8a6bcb85ce0e
https://www.youtube.com/watch?v=obWXjtg0L64

Idea
It happen to be the same idea of Dijkstra‘s algorithm, but we need to keep the path while explore the grape.
https://leetcode.com/problems/cheapest-flights-within-k-stops/discuss/115541/JavaPython-Priority-Queue-Solution

 Single-Source Shortest Paths Problem
https://www.youtube.com/watch?time_continue=183&v=Aa2sqUhIn-E


https://www.youtube.com/watch?v=GazC3A4OQTE


// need to go thru an example , other’s code 

public int findCheapestPrice(int n, int[][] flights, int src, int dst, int k) {
    // prices map, baically convert the int[][] flights into a hashmap for quick lookup of edges and costs 
        Map<Integer, Map<Integer, Integer>> prices = new HashMap<>(); // key : node1 value : map<key: node2, value:  cost from node1 to node2>
        for (int[] f : flights) {
            if (!prices.containsKey(f[0])) prices.put(f[0], new HashMap<>()); 
            prices.get(f[0]).put(f[1], f[2]);
        }
        Queue<int[]> pq = new PriorityQueue<>((a, b) -> (Integer.compare(a[0], b[0])));
        pq.add(new int[] {0, src, k + 1}); // (cost from source node, current city, how many stops we can stop by) stops = k + 1
        while (!pq.isEmpty()) {
            int[] top = pq.remove();
            int price = top[0];
            int city = top[1];
            int stops = top[2];
            if (city == dst) return price; // how to guarentee this is it 
            if (stops > 0) { // stops > 0, still can stop somewhere 
                Map<Integer, Integer> adj = prices.getOrDefault(city, new HashMap<>()); // get its nei node,and costs from this current node to the nei node 
                for (int a : adj.keySet()) {
                    pq.add(new int[] {price + adj.get(a), a, stops - 1}); // add all it‘s nei cost from source node, nei node, stops 
                }
            }
        }
        return -1;
    }

 

以上是关于787. Cheapest Flights Within K Stops的主要内容,如果未能解决你的问题,请参考以下文章

LWC 72: 787. Cheapest Flights Within K Stops

787. Cheapest Flights Within K Stops

787. Cheapest Flights Within K Stops

787. Cheapest Flights Within K Stops

787. Cheapest Flights Within K Stops

[Leetcode] 787 Cheapest Flights Within K Stops