def dijkstra(edges, start, end): g = [[] for _ in range(n)] for x, y, w in edges: g[x].append((y, w)) g[y].append((x, w)) dist = [inf] * n dist[start] = 0 h = [(0, start)]
while h: dx, x = heappop(h) if dist[x] != dx: continue for y, w in g[x]: if dx + w < dist[y]: dist[y] = dx + w heappush(h, (dx + w, y)) return dist[end] if dist[end] != inf else -1
Floyd 模板
1 2 3 4 5 6 7 8 9 10 11 12
def floyd(edges): w = [[inf] * n for _ in range(n)] for x, y, wt in edges: w[x][y] = wt w[y][x] = wt f = w for k in range(n): for i in range(n): for j in range(n): f[i][j] = min(f[i][j], f[i][k] + f[k][j]) return f