Skip to content

Commit 263674e

Browse files
committed
[BOJ]최단 경로 / 골드 5 / 45분
https://www.acmicpc.net/problem/1753
1 parent cd7d516 commit 263674e

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import heapq
2+
import sys
3+
4+
input = sys.stdin.readline
5+
6+
V, E = map(int, input().split())
7+
start = int(input())
8+
graph = [[] for _ in range(V + 1)]
9+
10+
for _ in range(E):
11+
u, v, w = map(int, input().split())
12+
13+
graph[u].append((v, w))
14+
15+
16+
def dijkstra(start):
17+
distances = [float("inf")] * (V + 1)
18+
distances[start] = 0
19+
q = []
20+
heapq.heappush(q, (distances[start], start))
21+
22+
while q:
23+
cnt_distance, node = heapq.heappop(q)
24+
25+
if distances[node] < cnt_distance:
26+
continue
27+
28+
for adjacency_node, distance in graph[node]:
29+
cal_distance = distances[node] + distance
30+
31+
if cal_distance < distances[adjacency_node]:
32+
distances[adjacency_node] = cal_distance
33+
heapq.heappush(q, (cal_distance, adjacency_node))
34+
35+
return distances
36+
37+
38+
result = dijkstra(start)
39+
40+
for i in range(1, len(result)):
41+
print("INF" if result[i] == float("inf") else result[i])

0 commit comments

Comments
 (0)