Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import deque
- class Solution:
- def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int:
- g = [[] for _ in range(n)]
- for flight in flights:
- g[flight[0]].append((flight[1],flight[2]))
- q = deque([(src,0,0)])
- path = [float('inf')]*n
- path[src] = 0
- while q:
- cur,stops,price = q.popleft()
- if stops < k+1:
- for nex,cost in g[cur]:
- if price + cost < path[nex]:
- path[nex] = price+cost
- q.append((nex,stops+1,price+cost))
- return -1 if path[dst] == float('inf') else path[dst]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement