Algorithm/boj

[파이썬] 1504 특정한 최단 경로

takeU 2022. 5. 13. 20:58
728x90
import heapq
import sys
input = sys.stdin.readline
INF = int(1e9)

n, e = map(int, input().split())
graph = [[] for _ in range(n + 1)]

for _ in range(e):
    a, b, c = map(int, input().split())
    graph[a].append((b, c))
    graph[b].append((a, c))

v1, v2 = map(int, input().split())


def dijkstra(start):
    q = []
    distance = [INF] * (n + 1)
    heapq.heappush(q, (0, start))
    distance[start] = 0
    while q:
        dist, now = heapq.heappop(q)
        if distance[now] < dist:
            continue

        for i in graph[now]:
            cost = dist + i[1]
            if cost < distance[i[0]]:
                distance[i[0]] = cost
                heapq.heappush(q, (cost, i[0]))
    return distance

g1 = dijkstra(1)
g2 = dijkstra(v1)
g3 = dijkstra(v2)
ans = min(g1[v1] + g2[v2] + g3[n], g1[v2] + g3[v1] + g2[n])
print(ans if ans < INF else -1)

다익스트라

728x90