|
| 1 | +/** |
| 2 | +* There are n cities connected by m flights. Each fight starts from city u and |
| 3 | +* arrives at v with a price w. |
| 4 | +* |
| 5 | +* Now given all the cities and flights, together with starting city src and |
| 6 | +* the destination dst, your task is to find the cheapest price from src to dst |
| 7 | +* with up to k stops. If there is no such route, output -1. |
| 8 | +* |
| 9 | +* Example 1: |
| 10 | +* Input: |
| 11 | +* n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]] |
| 12 | +* src = 0, dst = 2, k = 1 |
| 13 | +* Output: 200 |
| 14 | +* |
| 15 | +* Explanation: |
| 16 | +* The graph looks like this: |
| 17 | +* https://s3-lc-upload.s3.amazonaws.com/uploads/2018/02/16/995.png |
| 18 | +* |
| 19 | +* The cheapest price from city 0 to city 2 with at most 1 stop costs 200, as |
| 20 | +* marked red in the picture. |
| 21 | +* |
| 22 | +* Example 2: |
| 23 | +* Input: |
| 24 | +* n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]] |
| 25 | +* src = 0, dst = 2, k = 0 |
| 26 | +* Output: 500 |
| 27 | +* |
| 28 | +* Explanation: |
| 29 | +* The graph looks like this: |
| 30 | +* https://s3-lc-upload.s3.amazonaws.com/uploads/2018/02/16/995.png |
| 31 | +* |
| 32 | +* The cheapest price from city 0 to city 2 with at most 0 stop costs 500, as |
| 33 | +* marked blue in the picture. |
| 34 | +* |
| 35 | +* Note: |
| 36 | +* The number of nodes n will be in range [1, 100], with nodes labeled from 0 |
| 37 | +* to n - 1. |
| 38 | +* The size of flights will be in range [0, n * (n - 1) / 2]. |
| 39 | +* The format of each flight will be (src, dst, price). |
| 40 | +* The price of each flight will be in the range [1, 10000]. |
| 41 | +* k is in the range of [0, n - 1]. |
| 42 | +* There will not be any duplicated flights or self cycles. |
| 43 | +*/ |
| 44 | + |
| 45 | +public class CheapestFlightsWithinKStops787 { |
| 46 | +public int findCheapestPrice(int n, int[][] flights, int src, int dst, int K) { |
| 47 | +int[][] graph = constructGraph(n, flights); |
| 48 | +Queue<int[]> q = new LinkedList<>(); |
| 49 | +q.add(new int[]{src, 0}); |
| 50 | +int[][] dist = new int[n][K+2]; |
| 51 | +for (int[] row: dist) Arrays.fill(row, Integer.MAX_VALUE); |
| 52 | +dist[src][0] = 0; |
| 53 | +while (!q.isEmpty()) { |
| 54 | +int[] cur = q.poll(); |
| 55 | +int[] nei = graph[cur[0]]; |
| 56 | +for (int next=0; next<n; next++) { |
| 57 | +int weight = nei[next]; |
| 58 | +if (weight == 0) continue; |
| 59 | +int newDis = dist[cur[0]][cur[1]] + weight; |
| 60 | +if (cur[1] + 1 <= K+1 && newDis < dist[next][cur[1]+1]) { |
| 61 | +dist[next][cur[1]+1] = newDis; |
| 62 | +q.add(new int[]{next, cur[1]+1}); |
| 63 | +} |
| 64 | +} |
| 65 | +} |
| 66 | +int res = Integer.MAX_VALUE; |
| 67 | +for (int r: dist[dst]) { |
| 68 | +res = Math.min(res, r); |
| 69 | +} |
| 70 | +return res == Integer.MAX_VALUE ? -1 : res; |
| 71 | +} |
| 72 | + |
| 73 | +private int[][] constructGraph(int n, int[][] flights) { |
| 74 | +int[][] res = new int[n][n]; |
| 75 | +for (int[] f: flights) { |
| 76 | +res[f[0]][f[1]] = f[2]; |
| 77 | +} |
| 78 | +return res; |
| 79 | +} |
| 80 | + |
| 81 | +} |
| 82 | + |
0 commit comments