class dijkstra: def __init__(self, g): self.g = g def shortest_path(self, start): import heapq n = len(self.g) dist = [float("inf")] * n dist[start] = 0 pq = [(0, start)] while pq: d, u = heapq.heappop(pq) if dist[u] < d: continue for v, w in self.g[u]: nd = d + w if dist[v] > nd: dist[v] = nd heapq.heappush(pq, (nd, v)) return dist