Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Cheapest Flight by eugene100372
from typing import List
from collections import defaultdict
def cheapest_flight(costs: List, a: str, b: str) -> int:
d1=defaultdict(dict)
for el in costs:
d1[el[0]][el[1]]=el[2]
d1[el[1]][el[0]]=el[2]
d2={el:float('inf') for el in d1.keys()}
d2[a]=0
cite,cost=a,d2.pop(a)
while cite!=b:
for key in d1[cite]:
if key in d2: d2[key]=min(d2[key],d1[cite][key]+cost)
cite=min((el for el in d2),key=lambda x:d2[x])
cost=d2.pop(cite)
if cost==float('inf'): return 0
return cost
Aug. 12, 2020