For Programmer
백준 1753번 파이썬 문제풀이(최단 경로) 본문
728x90
단순한 다익스트라 문제이다.
import heapq
import sys
input = sys.stdin.readline
V, E = map(int, input().split())
K = int(input())
graph = [[] for _ in range(V + 1)]
for i in range(E):
a, b, cost = map(int, input().split())
graph[a].append((b, cost))
INF = 1e10
distance = [INF] * (V + 1)
def dijkstra(s):
q = []
distance[s] = 0
heapq.heappush(q, (0, s))
while q:
cost, cur = heapq.heappop(q)
if cost > distance[cur]:
continue
for nxt, nxt_cost in graph[cur]:
if nxt_cost + cost < distance[nxt]:
distance[nxt] = nxt_cost + cost
heapq.heappush(q, (distance[nxt], nxt))
dijkstra(K)
for i in range(1, len(distance)):
if distance[i] != INF:
print(distance[i])
else:
print('INF')
728x90
'코팅테스트 > 백준 문제 모음' 카테고리의 다른 글
백준 13910번 파이썬 문제풀이(개업) (0) | 2022.04.21 |
---|---|
백준 1504번 파이썬 문제풀이(특정한 최단 경로) (0) | 2022.04.20 |
백준 1238번 파이썬 문제풀이(파티) (0) | 2022.04.20 |
백준 2636번 파이썬 문제풀이(치즈) - (bfs + 구현 or 다익스트라) (0) | 2022.04.19 |
백준 9328번 파이썬 문제풀이(열쇠 ) - (bfs + 구현) (0) | 2022.04.18 |
Comments