기록하고 까먹지 말기

5972 본문

전공/백준

5972

yha97 2023. 7. 10. 17:41

날짜 : 2023. 07. 10

사용 언어 : python

 

문제

 

 

코드

import sys
import heapq

def dijkstra(start):  # 시작노드
    queue = list()
    heapq.heappush(queue, (0, start))
    while queue:
        cost, now = heapq.heappop(queue)  # now까지 거리
        for i in graph[now]:
            if dist[i[0]] > cost + i[1]:
                dist[i[0]] = cost + i[1]
                heapq.heappush(queue, (cost + i[1], i[0]))
    return

n, m = map(int, sys.stdin.readline().split())  # 노드, 엣지의 개수
graph = [[] for _ in range(n + 1)]
dist = [int(1e9)] * (n + 1)  # 시작점 : 1
dist[1] = 0

for _ in range(m):
    s, e, c = map(int, sys.stdin.readline().split())
    graph[s].append([e, c])
    graph[e].append([s, c])

dijkstra(1)
print(dist[-1])

 

 

풀이

- 다익스트라를 활용하여 풀이한다.

- 앞선 문제와 달리 시작노드가 1이라는 점을 유의하여 풀이한다.

 

 

알게된 점

 

 

참고 사이트

 

'전공 > 백준' 카테고리의 다른 글

14940  (0) 2023.07.15
16928  (0) 2023.07.13
1446  (0) 2023.07.10
9934  (0) 2023.06.27
2661  (0) 2023.06.26