Open LLancelot opened 4 years ago
from heapq import *
class Solution(object):
def findCheapestPrice(self, n, flights, src, dst, total_stops):
dic = defaultdict(list)
for start, end, price in flights:
dic[start].append((end, price))
stop = 0
heap = [(0, -1, src)]
while heap:
cur_price, cur_stop, cur_city = heappop(heap)
if cur_city == dst:
return cur_price
if cur_stop < total_stops:
for nb, nb_price in dic[cur_city]:
heappush(heap, (cur_price + nb_price, cur_stop + 1, nb))
return -1
https://leetcode.com/problems/cheapest-flights-within-k-stops/
代码
c++
Python