From bcde3c221fa4044ace5be99f1c6ca7f4fd81987a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EC=84=9D=7CMinseok=20Kim?= Date: Mon, 25 Mar 2024 02:58:29 +0900 Subject: [PATCH] =?UTF-8?q?=EC=B5=9C=EB=8B=A8=EA=B2=BD=EB=A1=9C=20solved?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...34\353\213\250\352\262\275\353\241\234.py" | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 "alstjr7437/\352\267\270\353\236\230\355\224\204/\354\265\234\353\213\250\352\262\275\353\241\234.py" diff --git "a/alstjr7437/\352\267\270\353\236\230\355\224\204/\354\265\234\353\213\250\352\262\275\353\241\234.py" "b/alstjr7437/\352\267\270\353\236\230\355\224\204/\354\265\234\353\213\250\352\262\275\353\241\234.py" new file mode 100644 index 0000000..c50db6d --- /dev/null +++ "b/alstjr7437/\352\267\270\353\236\230\355\224\204/\354\265\234\353\213\250\352\262\275\353\241\234.py" @@ -0,0 +1,40 @@ +from heapq import * +import sys + +input = sys.stdin.readline + +v, e = map(int, input().split()) +k = int(input()) + +# 그래프 만들기 +graph = [[] for _ in range(v + 1)] +for _ in range(e): + a, b, c = map(int, input().split()) + graph[a].append((c, b)) + +# 결과 저장할 변수 만들기 +heap = [] +result = [int(1e9)] * (v + 1) + +# k 넣기 +result[k] = 0 +heappush(heap, (0, k)) + +# 다익스트라 돌리기 +while heap: + weight, now = heappop(heap) + # print(weight,now, heap, result) + if result[now] < weight: + continue + for next_weight, next_node in graph[now]: + total_weight = weight + next_weight + if total_weight < result[next_node]: + result[next_node] = total_weight + heappush(heap, (total_weight, next_node)) + +# 결과 출력하기 +for i in range(1, v + 1): + if result[i] == int(1e9): + print("INF") + else: + print(result[i])