Advertisement
cyga

Untitled

Jun 14th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.55 KB | None | 0 0
  1. from collections import deque
  2.  
  3. class Solution:
  4.     def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int:
  5.         g = [[] for _ in range(n)]
  6.         for flight in flights:
  7.             g[flight[0]].append((flight[1],flight[2]))
  8.         q = deque([(src,0,0)])
  9.         path = [float('inf')]*n
  10.         path[src] = 0
  11.         while q:
  12.             cur,stops,price = q.popleft()
  13.  
  14.             if stops < k+1:
  15.                 for nex,cost in g[cur]:
  16.                     if price + cost < path[nex]:
  17.                         path[nex] = price+cost
  18.                         q.append((nex,stops+1,price+cost))
  19.  
  20.         return -1 if path[dst] == float('inf') else path[dst]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement