-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
16-g0rnn #62
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ°μλ λ€μ΅μ€νΈλΌ λ¬Έμ λλΆμ μ‘°κΈ μλ ¨λ κ² κ°λ€μ κ°μ¬ν©λλ€.
μ λ²μ μ€μ©λ νΌμμμ λ무 μ¬μ© λμ΄κ°μ κ·Έλ°μ§ μ΄λ²μλ... μ’ νλ€κΈ΄ νλλ°μ..
μ΄μ μ§μ§ λ νΌλ°μ€ μλ³΄κ³ ν μ μμ κ² κ°λ€μ
κ·Έλ¦¬κ³ μ’μ μ§λ¬Έλ€μ΄ μμ΄μ λ΅ν΄λ³΄λ €κ³ ν©λλ€.
- λ€μ΅μ€νΈλΌμμ μ°μ μμ νλ₯Ό μ¬μ©νλ©΄ μ’μ μ΄μ
- μ°μ μμ νμ κ³΅κ° λ³΅μ‘λ
- μ΄λ»κ² λ°©λ¬Έν λ Έλλ₯Ό λ°©λ¬Ένμ§ μλμ§
- distλ°°μ΄κ³Ό νμ μ μ₯λ κ°μ μ°¨μ΄
1 : κ°μ€μΉκ° μμ λ
Έλ λ¨Όμ νμνκΈ° λλ¬Έμ κ·Έλ₯ νμλ³΄λ€ μ΅μ λΉμ©μ μ°Ύμκ°λ λ¨κ³κ° λΉκ΅μ μ λ€.
2 : μ΅λλ‘ κ°μ μ κ°μλ§νΌ 곡κ°λ³΅μ‘λκ° μλ€κ³ μκ°ν©λλ€. λ°λΌμ O(E)
3 : μλ‘ λ°©λ¬Έν΄μΌνλ κ³³μ κ°μ€μΉλ³΄λ€ νμ¬μ κ°μ€μΉκ° λμ κ²½μ° νμ λ£μ§ μμλ λ©λλ€.
4 : distλ°°μ΄μλ κ³μν΄μ κ°μ₯ μμ κ°μ€μΉ(νμ¬κΉμ§ νμΈν μ΅μ κ°μ€μΉ)λ‘ μ
λ°μ΄νΈ λμ§λ§, νμλ κ·Έ ν보(νμ λμ§ μμ κ° ?) λ€μ΄ λ€μ΄κ°λ€.
λ§λμ? μκ°μ΄ λ€λ₯΄κ±°λ νλ¦°κ² μλ€λ©΄ λ§μν΄μ£ΌμΈμ~
μ½λλ οΏ½κ±°μ λμΌν©λλ€.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ μ²κΈ°μ€λΉνλ€κ³ 5μΌμ λ λͺ»νλ€κ° 첫문μ λ₯Ό λ€μ΅μ€νΈλΌλ‘ μμνλ μκ°μ΄ μ’ μ€λκ±Έλ Έμ΅λλ€ γ
..
μΈμ 리μ€νΈμ μ°μ μμνλ₯Ό μ¬μ©νμ¬ μλ‘μ΄ κ±°λ¦¬κ° κΈ°μ‘΄ κ±°λ¦¬λ³΄λ€ μ§§μΌλ©΄ μ
λ°μ΄νΈνκ³ νμ μΆκ°νλ λ°©μμΌλ‘ κΈ°μ‘΄κ³Ό λκ°μ΄ μ§ννμμ΅λλ€.
INFλ₯Ό μ§μ ν λ€ μ΄μ΄μ§μ§μμ λ
ΈλμΈ κ²½μ° INFλ₯Ό νμ©νμ¬ μΆλ ₯λκ²λ νμμ΅λλ€.
κΈ°μ‘΄μ½λλ₯Ό μ‘°κΈμ© μ°Έκ³ νμ¬ νμμ΅λλ€!
import sys
import heapq
from collections import defaultdict
input = sys.stdin.read
data = input().splitlines()
V, E = map(int, data[0].split())
K = int(data[1])
graph = defaultdict(list)
for line in data[2:]:
u, v, w = map(int, line.split())
graph[u].append((v, w))
# μ΅λ¨ 거리 λ°°μ΄ μ΄κΈ°ν
distance = [float('inf')] * (V + 1)
distance[K] = 0
# μ°μ μμ ν (heapq μ¬μ©)
pq = []
heapq.heappush(pq, (0, K))
while pq:
current_dist, current_node = heapq.heappop(pq)
if current_dist > distance[current_node]:
continue
for neighbor, weight in graph[current_node]:
new_dist = current_dist + weight
if new_dist < distance[neighbor]:
distance[neighbor] = new_dist
heapq.heappush(pq, (new_dist, neighbor))
for i in range(1, V + 1):
if distance[i] == float('inf'):
print("INF")
else:
print(distance[I])
π λ¬Έμ λ§ν¬
μ΅λ¨κ²½λ‘
βοΈ μμλ μκ°
30m
β¨ μλ μ½λ
μ΄λ² λ¬Έμ λ λ€μ΅μ€νΈλΌμ λλ€. 보μλ©΄ μκ² μ§λ§ μ΄μ μ€μ©λμ μ½λμ 90νΌ μ΄μ μΌμΉνλλ°λ κ°μ λ¬Έμ λ₯Ό λ€μ λΈ μ΄μ λ λ€μ νμ΄λ μ΄λ €μ κΈ° λλ¬Έμ λλ€.. λ¬Έμ νΌμ§ μΌλ§ μ§λμ§ μμκΈ° λλ¬Έμ λ°±μ€μμ λ°λ‘ νμ΄λ³΄λκ² λ μ’μκ±° κ°μμ ;)
κ°μΈμ μΌλ‘ λ€μ΅μ€νΈλΌμ λν΄ μ‘°κΈ λ 곡λΆνκ³ μΆμ΄ μ΄ λ¬Έμ λ₯Ό ννμ΅λλ€.
λ¬Έμ λ₯Ό νκ³ κ³΅λΆνλ©΄μ μμ μ§λ¬Έμ λ΅μ ν μ μλ€λ©΄ μ’μ κ² κ°μ΅λλ€.
π μλ‘κ² μκ²λ λ΄μ©