-
Notifications
You must be signed in to change notification settings - Fork 43
/
cheapest-flights-within-k-stops.py
465 lines (385 loc) · 16.5 KB
/
cheapest-flights-within-k-stops.py
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
"""
787. Cheapest Flights Within K Stops
Medium
There are n cities connected by some number of flights. You are given an array flights where flights[i] = [fromi, toi, pricei] indicates that there is a flight from city fromi to city toi with cost pricei.
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.
Example 1:
Input: n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 1
Output: 200
Explanation: The graph is shown.
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, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 0
Output: 500
Explanation: The graph is shown.
The cheapest price from city 0 to city 2 with at most 0 stop costs 500, as marked blue in the picture.
Constraints:
1 <= n <= 100
0 <= flights.length <= (n * (n - 1) / 2)
flights[i].length == 3
0 <= fromi, toi < n
fromi != toi
1 <= pricei <= 104
There will not be any multiple flights between two cities.
0 <= src, dst, k < n
src != dst
"""
# V0
# V1
# https://leetcode.com/problems/cheapest-flights-within-k-stops/solution/
# IDEA : Dijkstra
import heapq
class Solution:
def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, K: int) -> int:
# Build the adjacency matrix
adj_matrix = [[0 for _ in range(n)] for _ in range(n)]
for s, d, w in flights:
adj_matrix[s][d] = w
# Shortest distances array
distances = [float("inf") for _ in range(n)]
current_stops = [float("inf") for _ in range(n)]
distances[src], current_stops[src] = 0, 0
# Data is (cost, stops, node)
minHeap = [(0, 0, src)]
while minHeap:
cost, stops, node = heapq.heappop(minHeap)
# If destination is reached, return the cost to get here
if node == dst:
return cost
# If there are no more steps left, continue
if stops == K + 1:
continue
# Examine and relax all neighboring edges if possible
for nei in range(n):
if adj_matrix[node][nei] > 0:
dU, dV, wUV = cost, distances[nei], adj_matrix[node][nei]
# Better cost?
if dU + wUV < dV:
distances[nei] = dU + wUV
heapq.heappush(minHeap, (dU + wUV, stops + 1, nei))
current_stops[nei] = stops
elif stops < current_stops[nei]:
# Better steps?
heapq.heappush(minHeap, (dU + wUV, stops + 1, nei))
return -1 if distances[dst] == float("inf") else distances[dst]
# V1
# https://leetcode.com/problems/cheapest-flights-within-k-stops/solution/
# IDEA : Depth-First-Search with Memoization
class Solution:
def __init__(self):
self.adj_matrix = None
self.memo = {}
def findShortest(self, node, stops, dst, n):
# No need to go any further if the destination is reached
if node == dst:
return 0
# Can't go any further if no stops left
if stops < 0:
return float("inf")
# If the result of this state is already cached, return it
if (node, stops) in self.memo:
return self.memo[(node, stops)]
# Recursive calls over all the neighbors
ans = float("inf")
for neighbor in range(n):
if self.adj_matrix[node][neighbor] > 0:
ans = min(ans, self.findShortest(neighbor, stops-1, dst, n) + self.adj_matrix[node][neighbor])
# Cache the result
self.memo[(node, stops)] = ans
return ans
def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, K: int) -> int:
self.adj_matrix = [[0 for _ in range(n)] for _ in range(n)]
self.memo = {}
for s, d, w in flights:
self.adj_matrix[s][d] = w
result = self.findShortest(src, K, dst, n)
return -1 if result == float("inf") else result
# V1
# https://leetcode.com/problems/cheapest-flights-within-k-stops/solution/
# IDEA : Bellman-Ford
class Solution:
def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, K: int) -> int:
# We use two arrays for storing distances and keep swapping
# between them to save on the memory
distances = [[float('inf')] * n for _ in range(2)]
distances[0][src] = distances[1][src] = 0
# K + 1 iterations of Bellman Ford
for iterations in range(K + 1):
# Iterate over all the edges
for s, d, wUV in flights:
# Current distance of node "s" from src
dU = distances[1 - iterations&1][s]
# Current distance of node "d" from src
# Note that this will port existing values as
# well from the "previous" array if they didn't already exist
dV = distances[iterations&1][d]
# Relax the edge if possible
if dU + wUV < dV:
distances[iterations&1][d] = dU + wUV
return -1 if distances[K&1][dst] == float("inf") else distances[K&1][dst]
# V1
# https://leetcode.com/problems/cheapest-flights-within-k-stops/solution/
# IDEA : Breadth First Search
class Solution:
def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, K: int) -> int:
# Build the adjacency matrix
adj_matrix = [[0 for _ in range(n)] for _ in range(n)]
for s, d, w in flights:
adj_matrix[s][d] = w
# Shortest distances dictionary
distances = {}
distances[(src, 0)] = 0
# BFS Queue
bfsQ = deque([src])
# Number of stops remaining
stops = 0
ans = float("inf")
# Iterate until we exhaust K+1 levels or the queue gets empty
while bfsQ and stops < K + 1:
# Iterate on current level
length = len(bfsQ)
for _ in range(length):
node = bfsQ.popleft()
# Loop over neighbors of popped node
for nei in range(n):
if adj_matrix[node][nei] > 0:
dU = distances.get((node, stops), float("inf"))
dV = distances.get((nei, stops + 1), float("inf"))
wUV = adj_matrix[node][nei]
# No need to update the minimum cost if we have already exhausted our K stops.
if stops == K and nei != dst:
continue
if dU + wUV < dV:
distances[nei, stops + 1] = dU + wUV
bfsQ.append(nei)
# Shortest distance of the destination from the source
if nei == dst:
ans = min(ans, dU + wUV)
stops += 1
return -1 if ans == float("inf") else ans
# V1
# IDEA : Dijkstra
# https://leetcode.com/problems/cheapest-flights-within-k-stops/discuss/267200/Python-Dijkstra
# IDEA
# -> To implement Dijkstra, we need a priority queue to pop out the lowest weight node for next search. In this case, the weight would be the accumulated flight cost. So my node takes a form of (cost, src, k). cost is the accumulated cost, src is the current node's location, k is stop times we left as we only have at most K stops. I also convert edges to an adjacent list based graph g.
# -> Use a vis array to maintain visited nodes to avoid loop. vis[x] record the remaining steps to reach x with the lowest cost. If vis[x] >= k, then no need to visit that case (start from x with k steps left) as a better solution has been visited before (more remaining step and lower cost as heappopped beforehand). And we should initialize vis[x] to 0 to ensure visit always stop at a negative k.
# -> Once k is used up (k == 0) or vis[x] >= k, we no longer push that node x to our queue. Once a popped cost is our destination, we get our lowest valid cost.
# -> For Dijkstra, there is not need to maintain a best cost for each node since it's kind of greedy search. It always chooses the lowest cost node for next search. So the previous searched node always has a lower cost and has no chance to be updated. The first time we pop our destination from our queue, we have found the lowest cost to our destination.
import collections
import math
class Solution:
def findCheapestPrice(self, n, flights, src, dst, K):
graph = collections.defaultdict(dict)
for s, d, w in flights:
graph[s][d] = w
pq = [(0, src, K+1)]
vis = [0] * n
while pq:
w, x, k = heapq.heappop(pq)
if x == dst:
return w
if vis[x] >= k:
continue
vis[x] = k
for y, dw in graph[x].items():
heapq.heappush(pq, (w+dw, y, k-1))
return -1
# V1'
# IDEA : Dijkstra
# https://leetcode.com/problems/cheapest-flights-within-k-stops/discuss/209730/Python-solution
class Solution(object):
def findCheapestPrice(self, n, flights, src, dst, K):
"""
:type n: int
:type flights: List[List[int]]
:type src: int
:type dst: int
:type K: int
:rtype: int
"""
graph = {}
for flight in flights:
if flight[0] in graph:
graph[flight[0]][flight[1]] = flight[2]
else:
graph[flight[0]] = {flight[1]:flight[2]}
rec = {}
heap = [(0, -1, src)]
heapq.heapify(heap)
while heap:
cost, stops, city = heapq.heappop(heap)
if city == dst:
return cost
if stops == K or rec.get((city, stops), float('inf')) < cost:
continue
if city in graph:
for nei, price in graph[city].items():
summ = cost + price
if rec.get((nei, stops+1), float('inf')) > summ:
rec[(nei, stops+1)] = summ
heapq.heappush(heap, (summ, stops+1, nei))
return -1
#--------------------------------------------
# TODO : FIX BELOW APPROACHES (TLE)
#--------------------------------------------
# V1''
# https://blog.csdn.net/fuxuemingzhu/article/details/83307822
# IDEA :DFS
# DEMO : collections.defaultdict
# In [41]: flights
# Out[41]: [[0, 1, 100], [1, 2, 100], [0, 2, 500]]
#
# In [42]: for u, v, e in flights:
# ...: graph[u][v]=e
# ...:
#
# In [43]: graph
# Out[43]: defaultdict(dict, {0: {1: 100, 2: 500}, 1: {2: 100}})
class Solution(object):
def findCheapestPrice(self, n, flights, src, dst, K):
graph = collections.defaultdict(dict)
for u, v, e in flights:
graph[u][v] = e
visited = [0] * n
ans = [float('inf')]
self.dfs(graph, src, dst, K + 1, 0, visited, ans)
return -1 if ans[0] == float('inf') else ans[0]
def dfs(self, graph, src, dst, k, cost, visited, ans):
if src == dst:
ans[0] = cost
return
if k == 0:
return
for v, e in graph[src].items():
if visited[v]:
continue
if cost + e > ans[0]:
continue
visited[v] = 1
self.dfs(graph, v, dst, k - 1, cost + e, visited, ans)
visited[v] = 0
### Test case : dev
# V1''''
# https://blog.csdn.net/fuxuemingzhu/article/details/83307822
# IDEA : BFS
class Solution(object):
def findCheapestPrice(self, n, flights, src, dst, K):
"""
:type n: int
:type flights: List[List[int]]
:type src: int
:type dst: int
:type K: int
:rtype: int
"""
graph = collections.defaultdict(dict)
for u, v, e in flights:
graph[u][v] = e
ans = float('inf')
que = collections.deque()
que.append((src, 0))
step = 0
while que:
size = len(que)
for i in range(size):
cur, cost = que.popleft()
if cur == dst:
ans = min(ans, cost)
for v, w in graph[cur].items():
if cost + w > ans:
continue
que.append((v, cost + w))
if step > K: break
step += 1
return -1 if ans == float('inf') else ans
# V1''''''
# https://leetcode.com/problems/cheapest-flights-within-k-stops/discuss/267200/Python-Dijkstra
# IDEA : Dijkstra algorithm
class Solution(object):
def findCheapestPrice(self,n, flights, src, dst, K):
pq, g = [(0,src,K+1)], collections.defaultdict(dict)
for s,d,c in flights: g[s][d] = c
while pq:
cost, s, k = heapq.heappop(pq)
if s == dst: return cost
if k:
for d in g[s]:
heapq.heappush(pq, (cost+g[s][d], d, k-1))
return -1
# V1'''''''
# https://leetcode.com/problems/cheapest-flights-within-k-stops/discuss/317262/2-Clean-Python-Solution-(BFS-Dijkstra-Explained)
# IDEA : Dijkstra algorithm
class Solution(object):
def findCheapestPrice(self, n, flights, src, dst, K):
graph = collections.defaultdict(list)
pq = []
for u, v, w in flights: graph[u].append((w, v))
heapq.heappush(pq, (0, K+1, src))
while pq:
price, stops, city = heapq.heappop(pq)
if city is dst: return price
if stops>0:
for price_to_nei, nei in graph[city]:
heapq.heappush(pq, (price+price_to_nei, stops-1, nei))
return -1
# V1''''
# https://leetcode.com/problems/cheapest-flights-within-k-stops/discuss/317262/2-Clean-Python-Solution-(BFS-Dijkstra-Explained)
# IDEA : BFS
class Solution(object):
def findCheapestPrice(self, n, flights, src, dst, K):
graph = collections.defaultdict(list)
q = collections.deque()
min_price = float('inf')
for u, v, w in flights:
graph[u].append((w, v))
q.append((src, 0, 0))
while q:
city, stops, price = q.popleft()
if city==dst:
min_price = min(min_price, price)
continue
if stops<=K and price<=min_price:
for price_to_nei, nei in graph[city]:
q.append((nei, stops+1, price+price_to_nei))
return min_price if min_price!=float('inf') else -1
# V1'''''
# https://leetcode.com/problems/cheapest-flights-within-k-stops/discuss/115541/JavaPython-Priority-Queue-Solution
# IDEA : Priority Queue
class Solution(object):
def findCheapestPrice(self, n, flights, src, dst, k):
f = collections.defaultdict(dict)
for a, b, p in flights:
f[a][b] = p
heap = [(0, src, k + 1)]
while heap:
p, i, k = heapq.heappop(heap)
if i == dst:
return p
if k > 0:
for j in f[i]:
heapq.heappush(heap, (p + f[i][j], j, k - 1))
return -1
# V2
# Time: O((|E| + |V|) * log|V|) = O(|E| * log|V|),
# if we can further to use Fibonacci heap, it would be O(|E| + |V| * log|V|)
# Space: O(|E| + |V|) = O(|E|)
import collections
import heapq
class Solution(object):
def findCheapestPrice(self, n, flights, src, dst, K):
adj = collections.defaultdict(list)
for u, v, w in flights:
adj[u].append((v, w))
best = collections.defaultdict(lambda: collections.defaultdict(lambda: float("inf")))
min_heap = [(0, src, K+1)]
while min_heap:
result, u, k = heapq.heappop(min_heap)
if k < 0 or best[u][k] < result:
continue
if u == dst:
return result
for v, w in adj[u]:
if result+w < best[v][k-1]:
best[v][k-1] = result+w
heapq.heappush(min_heap, (result+w, v, k-1))
return -1