From aee3b085c2ee5bc06e96d5aa726ed7fc53522e47 Mon Sep 17 00:00:00 2001 From: seongwon030 <105052068+seongwon030@users.noreply.github.com> Date: Thu, 22 Aug 2024 00:17:23 +0900 Subject: [PATCH] =?UTF-8?q?2024-08-22=201504=20=ED=8A=B9=EC=A0=95=ED=95=9C?= =?UTF-8?q?=20=EC=B5=9C=EB=8B=A8=20=EA=B2=BD=EB=A1=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\353\213\250 \352\262\275\353\241\234.py" | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 "seongwon030/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/\355\212\271\354\240\225\355\225\234 \354\265\234\353\213\250 \352\262\275\353\241\234.py" diff --git "a/seongwon030/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/\355\212\271\354\240\225\355\225\234 \354\265\234\353\213\250 \352\262\275\353\241\234.py" "b/seongwon030/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/\355\212\271\354\240\225\355\225\234 \354\265\234\353\213\250 \352\262\275\353\241\234.py" new file mode 100644 index 0000000..b59b068 --- /dev/null +++ "b/seongwon030/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/\355\212\271\354\240\225\355\225\234 \354\265\234\353\213\250 \352\262\275\353\241\234.py" @@ -0,0 +1,51 @@ +import heapq +import sys + +input = sys.stdin.readline + +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 di(start,size): + # 사용한 길 계속 사용 가능 하므로 계속 초기화 한다. + distance = [float('inf')] * (size+1) + q = [] + heapq.heappush(q, [0,start]) + distance[start] = 0 + + while q: + dist, node = heapq.heappop(q) + + if distance[node] < dist: + continue + for n,w in graph[node]: + cost = dist + w + + if distance[n] > cost: + distance[n]= cost + heapq.heappush(q,[cost,n]) + + return distance + +#각각 1,v1,v2가 시작점으로 각 노드에 대한 최단 경로를 담고 있다. +d1 = di(1,n) +d2 = di(v1,n) +d3 = di(v2,n) +# v1을 먼저 들리거나 v2를 먼저 들리거나 비교해서 더 짧은 거리를 사용한다. +result = min(d1[v2]+d2[n]+d3[v1],d1[v1]+d2[v2]+d3[n]) + + +if result == float('inf'): + print(-1) +else: + print(result) \ No newline at end of file