-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheapestFlightsWithinKStops.java
54 lines (46 loc) · 1.81 KB
/
CheapestFlightsWithinKStops.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.namanh.dijkstra;
import java.util.*;
/**
* https://leetcode.com/problems/cheapest-flights-within-k-stops
* You are also given three integers src, dst, and k, return the cheapest price from src to dst with at most k stops.
* If there is no such route, return -1.
*
* S1: Use Dijkstra
* S2: We don't use normal dictionary, because we still add edge if price or step smaller than previous value
*/
public class CheapestFlightsWithinKStops {
public int findCheapestPrice(int n, int[][] flights, int src, int dst, int k) {
int[][] adj = new int[n][n];
for (int[] flight : flights) {
adj[flight[0]][flight[1]] = flight[2];
}
int[] minPrice = new int[n];
int[] minStep = new int[n];
Arrays.fill(minPrice, Integer.MAX_VALUE);
Arrays.fill(minStep, Integer.MAX_VALUE);
Queue<int[]> edge = new PriorityQueue<>(Comparator.comparingInt(a -> a[1]));
edge.offer(new int[]{src, 0, k + 1});
while (!edge.isEmpty()) {
int[] info = edge.poll();
int city = info[0];
int price = info[1];
int numK = info[2];
if (city == dst) return price;
if (numK == 0) continue;
for (int c2 = 0; c2 < n; c2++) {
if (adj[city][c2] > 0) {
int price2 = adj[city][c2];
int numK2 = numK - 1;
if (price + price2 < minPrice[c2]) {
minPrice[c2] = price + price2;
edge.offer(new int[]{c2, minPrice[c2], numK2});
} else if (numK2 > minStep[c2]) {
edge.offer(new int[]{c2, price + price2, numK2});
}
minStep[c2] = numK2;
}
}
}
return -1;
}
}