λ°μν
π€λ¬Έμ ν΄κ²°
- ν¬λ£¨μ€μΉΌ
- Cμμ μΆλ°! κ° μ μλ κ³³μ μ°Ύμμ μ°μ μμ νμ λ£λλ€.
- μ°μ μμ νμμ λμ΄κ° κ°μ₯ λμ λ μμ λ½λλ€.
- κ±°κΈ°μ λ κ° μ μλ κ³³μ μ°Ύμμ νμ λ£λλ€. (λ¨, μ¬λ°©λ¬Έ X)
- Vλ₯Ό λ§λλ©΄ λ!
λ Έλλ₯Ό μ΄λνλ λμ μ΅μκ°μ κ³μ κ°±μ ν΄μ€λ€. (λ΅μ μ°ΎμμΌνλ―λ‘)
π»μμ€ μ½λ
import sys
from collections import defaultdict
import heapq
input = sys.stdin.readline
P, W = map(int, input().split()) # Pκ°μ μ§μ , Wκ°μ κΈΈ
C, V = map(int, input().split()) # C λ°±μ€μλ, V νλΈμλ
# μΈμ λ
Έλ λ§λ€κΈ°
nodes = [input() for _ in range(W)]
adj = defaultdict(list)
for node in nodes:
a, b, c = map(int, node.split())
adj[a].append([b, c])
adj[b].append([a, c])
# μ°μ μμ ν
visited = [0] * P
pq = []
heapq.heappush(pq, [-1e9, C])
answer = 1e9 # μ΅μκ° μ°ΎκΈ°
while pq:
cur_cost, cur_node = heapq.heappop(pq)
cur_cost *= -1
if visited[cur_node]:
continue
visited[cur_node] = 1
answer = min(answer, cur_cost)
if cur_node == V:
print(answer)
break
for next_node, next_cost in adj[cur_node]:
heapq.heappush(pq, [-next_cost, next_node])
πλ¬Έμ νμΈ
μΆμ²: BACKJOON ONLINE JUDGE
λ°μν
'Algorithm Problem > Python' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[python] λ°±μ€ - 1347. λ―Έλ‘ λ§λ€κΈ° (0) | 2021.09.14 |
---|---|
[python] λ°±μ€ - 12764. μΈμ§λ°©μ κ° μ€ν (0) | 2021.09.11 |
[python] λ°±μ€ - 19598. μ΅μ νμμ€ κ°μ (0) | 2021.09.09 |
[python] λ°±μ€ - 1013. Contact (0) | 2021.09.08 |
[python] λ°±μ€ - 1174. μ€μ΄λλ μ«μ (0) | 2021.09.07 |