Cheapest Flights Within K Stops LeetCode solution in Python
1class Solution:2def findCheapestPrice(3self, n: int, flights: List[List[int]], src: int, dst: int, k: int4) -> int:5graph = defaultdict(list)6for u, v, w in flights:7graph[u].append((v, w))89pq = [(0, src, 0)]10dis = [float("inf")] * n1112while pq:13c, n, l = heappop(pq)14if n == dst:15return c16if l > k or l >= dis[n]:17continue18dis[n] = l19for v, w in graph[n]:20heappush(pq, (c + w, v, l + 1))21return -1