Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

16-g0rnn #62

wants to merge 1 commit into from

Conversation

g0rnn
Copy link
Collaborator

@g0rnn g0rnn commented Jan 18, 2025

πŸ”— 문제 링크

μ΅œλ‹¨κ²½λ‘œ

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

30m

✨ μˆ˜λ„ μ½”λ“œ

이번 λ¬Έμ œλŠ” λ‹€μ΅μŠ€νŠΈλΌμž…λ‹ˆλ‹€. λ³΄μ‹œλ©΄ μ•Œκ² μ§€λ§Œ 이전 μ€€μš©λ‹˜μ˜ μ½”λ“œμ™€ 90퍼 이상 μΌμΉ˜ν•˜λŠ”λ°λ„ 같은 문제λ₯Ό λ‹€μ‹œ λ‚Έ μ΄μœ λŠ” λ‹€μ‹œ 풀어도 μ–΄λ €μ› κΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€.. 문제 푼지 μ–Όλ§ˆ μ§€λ‚˜μ§€ μ•Šμ•˜κΈ° λ•Œλ¬Έμ— λ°±μ€€μ—μ„œ λ°”λ‘œ ν’€μ–΄λ³΄λŠ”κ²Œ 더 쒋을거 κ°™μ•„μš” ;)

개인적으둜 λ‹€μ΅μŠ€νŠΈλΌμ— λŒ€ν•΄ 쑰금 더 κ³΅λΆ€ν•˜κ³  μ‹Άμ–΄ 이 문제λ₯Ό νƒν–ˆμŠ΅λ‹ˆλ‹€.

1. λ‹€μ΅μŠ€νŠΈλΌμ—μ„œ μš°μ„ μˆœμœ„ 큐λ₯Ό μ‚¬μš©ν•˜λ©΄ 쒋은 이유
2. μš°μ„ μˆœμœ„ 큐의 곡간 λ³΅μž‘λ„
3. μ–΄λ–»κ²Œ λ°©λ¬Έν•œ λ…Έλ“œλ₯Ό λ°©λ¬Έν•˜μ§€ μ•ŠλŠ”μ§€
4. distλ°°μ—΄κ³Ό 큐에 μ €μž₯된 κ°’μ˜ 차이

문제λ₯Ό ν’€κ³  κ³΅λΆ€ν•˜λ©΄μ„œ μœ„μ˜ μ§ˆλ¬Έμ— 닡을 ν•  수 μžˆλ‹€λ©΄ 쒋을 것 κ°™μŠ΅λ‹ˆλ‹€.

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

Copy link
Collaborator

@kangrae-jo kangrae-jo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ—°μ†λœ λ‹€μ΅μŠ€νŠΈλΌ 문제 덕뢄에 쑰금 μˆ™λ ¨λœ 것 κ°™λ„€μš” κ°μ‚¬ν•©λ‹ˆλ‹€.

μ €λ²ˆμ— μ€€μš©λ‹˜ ν”Όμ•Œμ—μ„œ λ„ˆλ¬΄ 슬쩍 λ„˜μ–΄κ°€μ„œ κ·ΈλŸ°μ§€ μ΄λ²ˆμ—λ„... μ’€ νž˜λ“€κΈ΄ ν–ˆλŠ”λ°μš”..
이젠 μ§„μ§œ 레퍼런슀 μ•ˆλ³΄κ³  ν•  수 μžˆμ„ 것 κ°™λ„€μš”

그리고 쒋은 μ§ˆλ¬Έλ“€μ΄ μžˆμ–΄μ„œ 닡해보렀고 ν•©λ‹ˆλ‹€.

  1. λ‹€μ΅μŠ€νŠΈλΌμ—μ„œ μš°μ„ μˆœμœ„ 큐λ₯Ό μ‚¬μš©ν•˜λ©΄ 쒋은 이유
  2. μš°μ„ μˆœμœ„ 큐의 곡간 λ³΅μž‘λ„
  3. μ–΄λ–»κ²Œ λ°©λ¬Έν•œ λ…Έλ“œλ₯Ό λ°©λ¬Έν•˜μ§€ μ•ŠλŠ”μ§€
  4. distλ°°μ—΄κ³Ό 큐에 μ €μž₯된 κ°’μ˜ 차이

1 : κ°€μ€‘μΉ˜κ°€ μž‘μ€ λ…Έλ“œ λ¨Όμ € νƒμƒ‰ν•˜κΈ° λ•Œλ¬Έμ— κ·Έλƒ₯ 탐색보닀 μ΅œμ†Œ λΉ„μš©μ„ μ°Ύμ•„κ°€λŠ” 단계가 비ꡐ적 적닀.
2 : μ΅œλŒ€λ‘œ κ°„μ„ μ˜ 개수만큼 κ³΅κ°„λ³΅μž‘λ„κ°€ μžˆλ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€. λ”°λΌμ„œ O(E)
3 : μƒˆλ‘œ λ°©λ¬Έν•΄μ•Όν•˜λŠ” 곳의 κ°€μ€‘μΉ˜λ³΄λ‹€ ν˜„μž¬μ˜ κ°€μ€‘μΉ˜κ°€ 높을 경우 큐에 넣지 μ•Šμ•„λ„ λ©λ‹ˆλ‹€.
4 : distλ°°μ—΄μ—λŠ” κ³„μ†ν•΄μ„œ κ°€μž₯ μž‘μ€ κ°€μ€‘μΉ˜(ν˜„μž¬κΉŒμ§€ ν™•μΈν•œ μ΅œμ†Œ κ°€μ€‘μΉ˜)둜 μ—…λ°μ΄νŠΈ λ˜μ§€λ§Œ, νμ—λŠ” κ·Έ 후보(ν™•μ •λ˜μ§€ μ•Šμ€ κ°’ ?) 듀이 λ“€μ–΄κ°„λ‹€.

λ§žλ‚˜μš”? 생각이 λ‹€λ₯΄κ±°λ‚˜ ν‹€λ¦°κ²Œ μžˆλ‹€λ©΄ λ§μ”€ν•΄μ£Όμ„Έμš”~

μ½”λ“œλŠ” �거의 λ™μΌν•©λ‹ˆλ‹€.

Copy link
Collaborator

@wnsmir wnsmir left a 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])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants