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 Moff
from collections import defaultdict
def cheapest_flight(costs, start: str, end: str) -> int:
graph = defaultdict(dict)
for a, b, w in costs:
graph[a][b] = w
graph[b][a] = w
seen = set()
distance = {start: 0}
while any(k not in seen for k in distance):
v = min(((val, key) for key, val in distance.items() if key not in seen))[1]
seen.add(v)
for b, w in graph[v].items():
distance[b] = min(distance.get(b, float('inf')), distance[v] + w)
return distance.get(end, 0)
April 8, 2019
Comments: