๋ฌธ์
https://www.acmicpc.net/problem/1916
ํ์ด
# ๋ฐฑ์ค 1916๋ฒ ๋ฌธ์ - ์ต์๋น์ฉ ๊ตฌํ๊ธฐ
import sys
import heapq
input = sys.stdin.readline
n, m= int(input()), int(input())
graph = [[] for _ in range(n+1)]
INF = 1e9
distance = [INF] * (n+1)
for _ in range(m):
a, b, c = map(int, input().split())
graph[a].append((b,c))
start, end = map(int, input().split())
def dijkstra(start):
q = []
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]))
dijkstra(start)
print(distance[end])
์ต๋จ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ ๋ ์ฌ์ฉํ๋ ๋ค ์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ์ ์ฌ์ฉํ์๋ค.
์ฒ์์ ์๊ฐ์ด๊ณผ๊ฐ ๋ฐ์ํด์ ๊ตฌ๊ธ๋ง์ ํด๋ดค๋๋ฐ ์ฝ๋๊ฐ ๋๊ฐ์๋ค. ๊ทธ๋์ sys ๋ชจ๋์ ์ฌ์ฉํ์๋๋ ํต๊ณผ๋์๋ค.
๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ๊ณผ ๊ด๋ จ๋ ๋ฌธ์ ๋ฅผ ๋ง์ด ํ์ด๋ณด๋๋ก ํด์ผ๊ฒ ๋ค.
'๐๏ธ Algorithm > ๐ฉ ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ฉ [๋ฐฑ์ค] [Python] [Gold3] 2638๋ฒ_์น์ฆ (1) | 2023.04.28 |
---|---|
๐ฉ [๋ฐฑ์ค] [Python] [Gold5] 2225๋ฒ_ํฉ ๋ถํด (0) | 2023.04.27 |
๐ฉ [๋ฐฑ์ค] [Python] [Gold5] 5582๋ฒ_๊ณตํต ๋ถ๋ถ ๋ฌธ์์ด (0) | 2023.04.26 |
๐ฉ [๋ฐฑ์ค] [Python] [Silver1] 6588๋ฒ_๊ณจ๋๋ฐํ์ ์ถ์ธก (0) | 2023.04.25 |
๐ฉ [๋ฐฑ์ค] [Python] [Gold1] 2042๋ฒ_๊ตฌ๊ฐ ํฉ ๊ตฌํ๊ธฐ (0) | 2023.04.24 |