Skip to content

Commit

Permalink
Merge pull request #33 from AlgoLeadMe/9-suhyun113
Browse files Browse the repository at this point in the history
9-suhyun113
  • Loading branch information
suhyun113 authored Jul 17, 2024
2 parents fc2f5e9 + 7136243 commit a4360da
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
3 changes: 2 additions & 1 deletion suhyun113/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
| 5์ฐจ์‹œ | 2024.04.10 | ์Šคํƒ | [ํฌ๋ ˆ์ธ ์ธํ˜• ๋ฝ‘๊ธฐ ๊ฒŒ์ž„](https://school.programmers.co.kr/learn/courses/30/lessons/64061) | [#19](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/19) |
| 6์ฐจ์‹œ | 2024.04.14 | ์Šคํƒ | [์ปจํŠธ๋กค ์ œํŠธ](https://school.programmers.co.kr/learn/courses/30/lessons/120853) | [#21](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/21) |
| 7์ฐจ์‹œ | 2024.05.09 | ํŠธ๋ฆฌ | [์›์ˆญ์ด ๋งค๋‹ฌ๊ธฐ](https://www.acmicpc.net/problem/2716) | [#31](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/31) |
| 8์ฐจ์‹œ | 2024.05.14 | ์ˆ˜ํ•™ | [์–ด๋ฆฐ ์™•์ž](https://www.acmicpc.net/problem/1004) | [#32](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/32) |
| 8์ฐจ์‹œ | 2024.05.14 | ์ˆ˜ํ•™ | [์–ด๋ฆฐ ์™•์ž](https://www.acmicpc.net/problem/1004) | [#32](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/32) |
| 9์ฐจ์‹œ | 2024.05.28 | ๋‹ค์ต์ŠคํŠธ๋ผ | [์ตœ์†Œ๋น„์šฉ ๊ตฌํ•˜๊ธฐ](https://www.acmicpc.net/problem/1916) | [#33](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/33) |
36 changes: 36 additions & 0 deletions suhyun113/๋‹ค์ต์ŠคํŠธ๋ผ/9-suhyun113.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 1916 : ์ตœ์†Œ๋น„์šฉ ๊ตฌํ•˜๊ธฐ

import heapq

INF = float('inf') # ์ตœ๋Œ€๊ฐ’ ์ •์˜

N = int(input()) # ๋„์‹œ์˜ ๊ฐœ์ˆ˜(๋…ธ๋“œ)
M = int(input()) # ๋ฒ„์Šค์˜ ๊ฐœ์ˆ˜(์—์ง€)

graph = [[] for _ in range(N+1)] # ๊ทธ๋ž˜ํ”„ ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋กœ ์ดˆ๊ธฐํ™”(๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ๋“ค)
distance = [INF] * (N+1) # ๊ฐ ๋…ธ๋“œ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ ๋ฌดํ•œ๋Œ€๋กœ ์ดˆ๊ธฐํ™”

for _ in range(M):
x, y, cost = map(int, input().split()) # x -> y ๋„์‹œ๋กœ ๊ฐ€๋Š” ๋ฐ ํ•„์š”ํ•œ ๋น„์šฉ cost
graph[x].append([y, cost]) # ๊ทธ๋ž˜ํ”„์— ์—์ง€ ์ถ”๊ฐ€

start, end = map(int, input().split()) # ์ถœ๋ฐœ, ๋„์ฐฉ ๋…ธ๋“œ ์ž…๋ ฅ ๋ฐ›๊ธฐ

# ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜
def Dijkstra(start):
q = [] # ์šฐ์„ ์ˆœ์œ„ ํ ์ƒ์„ฑ
heapq.heappush(q, [0, start]) # ์ถœ๋ฐœํ•  ๋„์‹œ ํ์— ๋„ฃ๊ธฐ([๊ฑฐ๋ฆฌ, ๋…ธ๋“œ] ํ˜•ํƒœ)
distance[start] = 0 # ์‹œ์ž‘ ๋„์‹œ์˜ ๊ฑฐ๋ฆฌ 0์œผ๋กœ ์ดˆ๊ธฐํ™”

while q: # ํ๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
weight, node = heapq.heappop(q) # ํ˜„์žฌ ๋…ธ๋“œ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ, ํ˜„์žฌ ๋…ธ๋“œ(ํ์—์„œ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’)
if distance[node] < weight: # ํ˜„์žฌ ๋…ธ๋“œ๊ฐ€ ์ด๋ฏธ ์ฒ˜๋ฆฌ๋œ ๋…ธ๋“œ์ธ์ง€ ํ™•์ธ
continue
for n, w in graph[node]:
cost = w + weight # ํ˜„์žฌ ๋…ธ๋“œ๋ฅผ ํ†ตํ•ด ์ธ์ ‘ ๋…ธ๋“œ๊นŒ์ง€ ๊ฐ€๋Š” ์ƒˆ๋กœ์šด ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ
if distance[n] > cost: # ์ƒˆ๋กœ์šด ๊ฑฐ๋ฆฌ๊ฐ€ ๊ธฐ์กด์— ์ €์žฅ๋œ ๊ฑฐ๋ฆฌ๋ณด๋‹ค ์งง์€์ง€ ํ™•์ธ
distance[n] = cost # ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ๊ฐฑ์‹ 
heapq.heappush(q, [cost, n]) # ์ธ์ ‘ ๋…ธ๋“œ๋ฅผ ์šฐ์„ ์ˆœ์œ„ ํ์— ์ถ”๊ฐ€

Dijkstra(start)
print(distance[end])

0 comments on commit a4360da

Please sign in to comment.