From 892fbb6b112ec77b50fa4f2ff16c0d68896ada40 Mon Sep 17 00:00:00 2001 From: wnsmir <163817041+wnsmir@users.noreply.github.com> Date: Fri, 4 Oct 2024 15:54:49 +0900 Subject: [PATCH] =?UTF-8?q?=EC=B5=9C=EC=86=8C=EB=B9=84=EC=9A=A9=EA=B5=AC?= =?UTF-8?q?=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wnsmir/.DS_Store | Bin 6148 -> 6148 bytes ...51\352\265\254\355\225\230\352\270\260.py" | 47 ++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 "wnsmir/\352\267\270\353\246\254\353\224\224/\353\260\261\354\244\2001916\354\265\234\354\206\214\353\271\204\354\232\251\352\265\254\355\225\230\352\270\260.py" diff --git a/wnsmir/.DS_Store b/wnsmir/.DS_Store index d87bc9f37b786e582a9a85b18ac6ecabbd57ca17..b19b93fe01a915cdae0823c9726c8618a90cbfee 100644 GIT binary patch delta 150 zcmZoMXfc@JFUrioz`)4BAi%)j%izkO$B@gA;+eBKk$E{|GDwP(AqXh!$l!u3ou8bP zlb-~X1?pm055#)^!2rl&U|?SRW9OQ!BAz)YsvNPank>U~d~*u(VwQ;w`kUD~{_+C= Dhx98k delta 193 zcmZoMXfc@JFUrcmz`)4BAiyBVELfUUUR;orlb^KNk$E|zAV`W$kU_9mkX5i0Bt1Eg z>3BVgNKU$8aB_Zb0Z=6jpvdRuySRW11WIxw-aq(`C*+tTkd3A$g`k>(446Heotc-i KY-Z>9%MSo_)GOWq diff --git "a/wnsmir/\352\267\270\353\246\254\353\224\224/\353\260\261\354\244\2001916\354\265\234\354\206\214\353\271\204\354\232\251\352\265\254\355\225\230\352\270\260.py" "b/wnsmir/\352\267\270\353\246\254\353\224\224/\353\260\261\354\244\2001916\354\265\234\354\206\214\353\271\204\354\232\251\352\265\254\355\225\230\352\270\260.py" new file mode 100644 index 0000000..8aa5fba --- /dev/null +++ "b/wnsmir/\352\267\270\353\246\254\353\224\224/\353\260\261\354\244\2001916\354\265\234\354\206\214\353\271\204\354\232\251\352\265\254\355\225\230\352\270\260.py" @@ -0,0 +1,47 @@ +import heapq + +INF = int(1e9) + +n = int(input()) +m = int(input()) + +graph = [[] for _ in range(n + 1)] + +#모든 간선 정보 입력받기 +for _ in range(m): + start, end, cost = map(int, input().split()) + graph[start].append((end, cost)) + +#시작점, 도착점 입력받기 (m+3번째 줄) +start_city, end_city = map(int, input().split()) + +# 최단 거리 테이블을 무한대로 초기화 +distance = [INF] * (n + 1) + +# 다익스트라 정의 +def dijkstra(start): + # 우선순위 큐(Heap) 사용 + q = [] + # 시작 지점 비용 0으로 초기화하고 큐에 삽입 + heapq.heappush(q, (0, start)) + distance[start] = 0 + + while q: + # 가장 비용이 적은 노드 꺼내기 + dist, now = heapq.heappop(q) + + # 이미 처리된 노드라면 무시 + if distance[now] < dist: + continue + + # 현재 노드와 연결된 다른 노드들 확인 + for next_city, next_cost in graph[now]: + cost = dist + next_cost + # 현재 노드를 거쳐 다른 노드로 가는 비용이 더 적다면 갱신 + if cost < distance[next_city]: + distance[next_city] = cost + heapq.heappush(q, (cost, next_city)) + +dijkstra(start_city) +print(distance[end_city]) +