Skip to content

Commit 37e864e

Browse files
author
hangyeol
committed
106차 2번 문제풀이
1 parent ff9aede commit 37e864e

1 file changed

Lines changed: 25 additions & 4 deletions

File tree

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,35 @@
1+
import heapq
12
from collections import defaultdict
23

34
def main():
45
n, m = map(int, input().split())
56

6-
countryMap = defaultdict(list)
7+
cityMap = defaultdict(list)
78

89
for _ in range(m):
9-
A_i, B_i, C_i = map(int, input().strip().split())
10-
countryMap[A_i].append(C_i)
11-
countryMap[B_i].append(C_i)
10+
a, b, c = map(int, input().split())
11+
cityMap[a].append((b, c))
12+
cityMap[b].append((a, c))
13+
14+
dist = [float('inf')] * (n + 1)
15+
dist[1] = 0
16+
17+
pq = []
18+
heapq.heappush(pq, (0, 1))
19+
20+
while pq:
21+
cost, now = heapq.heappop(pq)
22+
23+
if dist[now] < cost:
24+
continue
25+
26+
for next_node, weight in cityMap[now]:
27+
new_cost = cost + weight
28+
if new_cost < dist[next_node]:
29+
dist[next_node] = new_cost
30+
heapq.heappush(pq, (new_cost, next_node))
31+
32+
print(dist[n])
1233

1334
if __name__ == "__main__":
1435
main()

0 commit comments

Comments
 (0)