Skip to content

Instantly share code, notes, and snippets.

@kachayev
kachayev / dijkstra.py
Last active January 24, 2024 00:40
Dijkstra shortest path algorithm based on python heapq heap implementation
from collections import defaultdict
from heapq import *
def dijkstra(edges, f, t):
g = defaultdict(list)
for l,r,c in edges:
g[l].append((c,r))
q, seen, mins = [(0,f,())], set(), {f: 0}
while q: