~๋ชฉ์ฐจ~
๋ฌธ์
https://www.acmicpc.net/problem/1916
๋ฌธ์ ํด๊ฒฐ ํฌ์ธํธ
- ๋ฐ์ ์ ๋ ฅ์ด ๋ง์ผ๋ฏ๋ก sys.stdin.realine ์ ์ฌ์ฉํ๋ค. (์๊ฐ ์ด๊ณผ ํด๊ฒฐ)
- ์ ํ์ ์ธ ๋ค์ต์คํธ๋ผ ๋ฌธ์ ์ ํ์ด๋ค
- ์ฐ์ ์์ ํ(์ต์ํ)๋ฅผ ์ฌ์ฉํ์ฌ ์ต์ ๊ฑฐ๋ฆฌ ๋ ธ๋๋ฅผ ์ ํ
- distance : start ๋ ธ๋๋ก๋ถํฐ ๊ฐ ๋ ธ๋์ ๊ฑฐ๋ฆฌ๋ฅผ ์ ์ฅ (INF ๋ก ์ด๊ธฐํ)
- graph : ๊ฐ ๋ ธ๋์ ์ฐ๊ฒฐ ๋ฆฌ์คํธ
๋ค์ต์คํธ๋ผ ์ ๋ฆฌ ๊ธ ์ฐธ๊ณ
์์ฑ ์ฝ๋
import heapq
import sys
input = sys.stdin.readline
INF = int(1e9)
N = int(input())
M = int(input())
graph = [[] for _ in range(N+1)]
distance = [INF]*(N+1)
for _ in range(M):
v1, v2, w = map(int, input().split())
graph[v1].append((v2, w))
def dijkstra(start):
distance[start] = 0
q = []
heapq.heappush(q, (0, start))
while q:
dist, now = heapq.heappop(q)
if distance[now] < dist:
continue
for i in graph[now]:
cost = dist + i[1]
if distance[i[0]] > cost:
distance[i[0]] = cost
heapq.heappush(q, (cost, i[0]))
A, B = map(int, input().split())
dijkstra(A)
print(distance[B])
๋์์ด ๋์ จ๋ค๋ฉด ์ข์์ ๋๋ฌ์ฃผ์ธ์๐