-
Notifications
You must be signed in to change notification settings - Fork 2
/
CheapestFlightsWithinKStops.java
138 lines (117 loc) · 3.28 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package com.algorithm.playground.leetcode.problems.lc700.lc780.lc787;
import java.util.*;
/**
* https://leetcode.com/problems/cheapest-flights-within-k-stops/
*/
public class CheapestFlightsWithinKStops {
public class SolutionDijkstra {
public int findCheapestPrice(int n, int[][] flights, int src, int dst, int K) {
int[][] prices = new int[n][n];
for (int[] flight : flights) {
prices[flight[0]][flight[1]] = flight[2];
}
PriorityQueue<City> queue = new PriorityQueue<>();
queue.offer(new City(0, src, -1));
int[] costs = new int[n];
Arrays.fill(costs, Integer.MAX_VALUE);
int[] stops = new int[n];
while (!queue.isEmpty()) {
City city = queue.poll();
if (city.stops <= K) {
if (city.curr == dst) {
return city.price;
} else {
int[] next = prices[city.curr];
for (int i = 0; i < next.length; i++) {
if (next[i] != 0) {
int nextPrice = city.price + next[i];
int nextStop = city.stops + 1;
if (costs[i] > nextPrice || stops[i] > nextStop) {
stops[i] = nextStop;
costs[i] = nextPrice;
queue.offer(new City(costs[i], i, stops[i]));
}
}
}
}
}
}
return -1;
}
private class City implements Comparable<City> {
private int price, curr, stops;
City(int price, int curr, int stops) {
this.price = price;
this.curr = curr;
this.stops = stops;
}
@Override
public int compareTo(City o) {
return Integer.compare(price, o.price);
}
@Override
public String toString() {
return "City{" +
"price=" + price +
", curr=" + curr +
", stops=" + stops +
'}';
}
}
}
/**
* Bellman Fold
*/
public class Solution {
public int findCheapestPrice(int n, int[][] flights, int src, int dst, int K) {
int[] prices = new int[n];
Arrays.fill(prices, Integer.MAX_VALUE);
prices[src] = 0;
for (int i = 0; i <= K; i++) {
int[] tmp = Arrays.copyOf(prices, prices.length);
for (int[] flight : flights) {
int curr = flight[0], next = flight[1], price = flight[2];
if (prices[curr] != Integer.MAX_VALUE) {
tmp[next] = Math.min(tmp[next], prices[curr] + price);
}
}
prices = tmp;
}
return prices[dst] == Integer.MAX_VALUE ? -1 : prices[dst];
}
}
class DFSSolution {
public int findCheapestPrice(int n, int[][] flights, int src, int dst, int K) {
if (src == dst) {
return 0;
}
Map<Integer, List<int[]>> nodes = new HashMap<>();
for (int[] f : flights) {
int from = f[0];
f[0] = from == src ? 1 : 0;
if (!nodes.containsKey(from)) {
nodes.put(from, new ArrayList<>());
}
nodes.get(from).add(f);
}
int max = dfs(nodes, Integer.MAX_VALUE, 0, src, dst, K, new boolean[n]);
return max != Integer.MAX_VALUE ? max : -1;
}
private int dfs(Map<Integer, List<int[]>> nodes, int max, int sum, int curr, int dst, int K, boolean[] seen) {
if (curr == dst) {
return sum;
} else if (K == -1) {
return max;
} else {
for (int[] next : nodes.getOrDefault(curr, Collections.emptyList())) {
if (sum + next[2] < max && !seen[next[1]]) {
seen[next[1]] = true;
max = dfs(nodes, max, sum + next[2], next[1], dst, K - 1, seen);
seen[next[1]] = false;
}
}
}
return max;
}
}
}