๋ฌธ์
๋ฐฑ์ค ์จ๋ผ์ธ ์ ์ง - 1916๋ฒ
ํ์ด ๊ณผ์
ํ ์ ์ ์ผ๋ก๋ถํฐ ๋ค๋ฅธ ๋ชจ๋ ์ ์ ๊น์ง์ ๊ฒฝ๋ก ์ ๋ณด๋ฅผ ์ฌ์ฉํด ์ต๋จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๋ ๋ฌธ์ ์
๋๋ค.
์ต์
์ ๊ฒฝ์ฐ V = 1000์ด๊ณ E = 100,000 ์ด๋ฏ๋ก O(ElgV)
์ ์๊ฐ ๋ณต์ก๋๋ฅผ ๊ฐ์ง๋ ๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ
์ ์ฌ์ฉํด์ ๋ต์ ๊ตฌํ ์ ์์ต๋๋ค.
์ฝ๋
import sys
import heapq
N = int(input())
M = int(input())
adj = [[] for _ in range(1001)]
for _ in range(M):
frm, to, cost = list(map(int, sys.stdin.readline().split()))
adj[frm].append([to, cost])
start, goal = list(map(int, sys.stdin.readline().split()))
def dijkstra():
pq = []
dist = [sys.maxsize] * (N+1)
dist[start] = 0
heapq.heappush(pq, (0, start))
while pq:
here = heapq.heappop(pq)
if dist[here[1]] < here[0]:
continue
for near in adj[here[1]]:
next_dist = near[1] + here[0]
if next_dist < dist[near[0]]:
dist[near[0]] = next_dist
heapq.heappush(pq, (next_dist, near[0]))
return dist[goal]
def solution():
answer = dijkstra()
return answer
print(solution())
๋ฐ์ํ
'๐ algorithm > boj' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ 2252 - ์ค ์ธ์ฐ๊ธฐ (0) | 2021.06.07 |
---|---|
BOJ 2231 - ๋ถํดํฉ (0) | 2021.06.07 |
BOJ 15684 - ์ฌ๋ค๋ฆฌ ์กฐ์ (0) | 2021.06.07 |
BOJ 1781 - ์ปต๋ผ๋ฉด (0) | 2021.06.07 |
BOJ 6118 - ์จ๋ฐ๊ผญ์ง (0) | 2021.06.07 |
๐ฌ ๋๊ธ