Skip to content

Commit 70117b3

Browse files
committed
106차 2번 문제풀이 (참고)
1 parent 15dd873 commit 70117b3

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import sys
2+
import heapq
3+
4+
input = sys.stdin.readline
5+
6+
n, m = map(int, input().split())
7+
graph = [[] for _ in range(n + 1)] # 인접리스트 (인접 행렬로 하면 메모리 초과)
8+
9+
for _ in range(m):
10+
a, b, c = map(int, input().split())
11+
graph[a].append((b, c))
12+
graph[b].append((a, c))
13+
14+
cost = [float('inf')] * (n + 1)
15+
cost[1] = 0
16+
heap = [(0, 1)] # (최소비용, 시작지점)
17+
18+
# 가중치가 변동이 있다면 우선순위 큐 사용한 다익스트라
19+
while heap:
20+
cur_cost, x = heapq.heappop(heap)
21+
if cur_cost > cost[x]:
22+
continue
23+
for nx, w in graph[x]:
24+
total = cur_cost + w
25+
if total < cost[nx]:
26+
cost[nx] = total
27+
heapq.heappush(heap, (total, nx))
28+
29+
print(cost[n])

0 commit comments

Comments
 (0)