Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Dijkstra, sort of solution in Clear category for The Cheapest Flight by Calen
from typing import List
def cheapest_flight(costs: List, a: str, b: str):
nodes = {}
for i in costs:
nodes.update({i[0]: 9e9})
nodes.update({i[1]: 9e9})
for _ in range(len(nodes)):
for fl in costs:
if fl[0] == a:
nodes[fl[1]] = min(nodes[fl[1]], fl[2])
elif fl[1] == a:
nodes[fl[0]] = min(nodes[fl[0]], fl[2])
else:
nodes[fl[1]] = min(nodes[fl[1]], nodes[fl[0]] + fl[2])
nodes[fl[0]] = min(nodes[fl[0]], nodes[fl[1]] + fl[2])
return nodes[b] if nodes[b] != 9e9 else 0
March 10, 2021