https://www.acmicpc.net/problem/5996
전형적인 최단경로 찾기 문제입니다.
import heapq
t,c,ts,te =map(int,input().split())
gra =[[] for _ in range(t+1)]
inf = 1e9
def dijk(start,end):
dp=[inf]*(t+1)
dp[start] = 0
heap=[]
heapq.heappush(heap,(0,start))
while heap:
wei,now =heapq.heappop(heap)
if wei > dp[now] :continue
for nextnode,cost in gra[now]:
if dp[nextnode] > wei+cost :
dp[nextnode] = wei+cost
heapq.heappush(heap, (wei+cost, nextnode))
return dp[end]
for _ in range(c):
a,b,c=map(int, input().split())
gra[a].append((b,c))
gra[b].append((a,c))
print(dijk(ts,te))
'알고리즘 문제풀이' 카테고리의 다른 글
아이들과 선물상자[백준 23757] (2) | 2024.09.16 |
---|---|
강의실[백준 1374] (0) | 2024.09.11 |
Road to Savings[백준 27617] (0) | 2024.09.10 |
안정적인 문자열[백준 4889] (0) | 2024.07.22 |
치킨 TOP N [백준 11582] (0) | 2024.07.21 |