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

10-suhyun113 #36

Merged
merged 8 commits into from
Jul 24, 2024
Merged

10-suhyun113 #36

merged 8 commits into from
Jul 24, 2024

Conversation

suhyun113
Copy link
Collaborator

@suhyun113 suhyun113 commented Jul 13, 2024

πŸ”— 문제 링크

https://www.acmicpc.net/problem/11404

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

μ•½ 1μ‹œκ°„

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

πŸ˜Άβ€πŸŒ«οΈ1. 문제 μ„ μ • μ΄μœ πŸ˜Άβ€πŸŒ«οΈ
이전 PR둜 μ΅œμ†ŒλΉ„μš©μ„ κ΅¬ν•˜λŠ” 문제λ₯Ό ν’€μ—ˆλ‹€. κ·Έλ•ŒλŠ” λ‹€μ΅μŠ€νŠΈλΌ μ•Œκ³ λ¦¬μ¦˜μ„ μ‚¬μš©ν–ˆλŠ”λ°, κ΅ν™©λ‹˜κ»˜μ„œ 리뷰둜 ν”Œλ‘œμ΄λ“œ-μ›Œμ…œ μ•Œκ³ λ¦¬μ¦˜μ„ μ‚¬μš©ν•΄μ„œλ„ 풀어보라고 μ‘°μ–Έν•΄μ£Όμ…¨λ‹€. κ·Έλž˜μ„œ 이전에 ν’€μ—ˆλ˜ 문제인 μ΅œμ†ŒλΉ„μš© κ΅¬ν•˜κΈ° 문제λ₯Ό ν”Œλ‘œμ΄λ“œ-μ›Œμ…œ μ•Œκ³ λ¦¬μ¦˜μ„ μ΄μš©ν•΄ 문제λ₯Ό ν’€μ΄ν•˜λ©΄μ„œ μ–΄λ–€ μ•Œκ³ λ¦¬μ¦˜μΈμ§€ μ—°μŠ΅μ„ ν•΄ λ³΄μ•˜λ‹€.

πŸ§ν”Œλ‘œμ΄λ“œ-μ›Œμ…œ μ•Œκ³ λ¦¬μ¦˜μ΄λž€?🐧

  • μš©λ„
    λͺ¨λ“  μ΅œλ‹¨ 경둜λ₯Ό κ΅¬ν•˜λŠ” μ•Œκ³ λ¦¬μ¦˜
    -> λ‹€μ΅μŠ€νŠΈλΌ μ•Œκ³ λ¦¬μ¦˜μ΄ ν•˜λ‚˜μ˜ μ •μ μ—μ„œ λ‹€λ₯Έ λͺ¨λ“  μ •μ κΉŒμ§€μ˜ μ΅œλ‹¨ 거리λ₯Ό κ΅¬ν•˜λŠ” μ•Œκ³ λ¦¬μ¦˜μ΄λΌλ©΄, ν”Œλ‘œμ΄λ“œ-μ›Œμ…œ μ•Œκ³ λ¦¬μ¦˜μ€ ν•œ 번 μ‹€ν–‰ν•΄ λͺ¨λ“  λ…Έλ“œ κ°„ μ΅œλ‹¨ 경둜λ₯Ό ꡬ할 수 μžˆλ‹€.

  • κ³΅κ°„λ³΅μž‘λ„
    이차원배열(O(V^2))
    -> λ‹€μ΅μŠ€νŠΈλΌ μ•Œκ³ λ¦¬μ¦˜μ΄ 인접행렬(O(V^2))을 μ΄μš©ν•˜μ—¬ μΈμ ‘λ¦¬μŠ€νŠΈλ₯Ό 주둜 μ‚¬μš©ν•˜λŠ” 반면, ν”Œλ‘œμ΄λ“œ-μ›Œμ…œ μ•Œκ³ λ¦¬μ¦˜μ€ 이차원배열을 μ΄μš©ν•œλ‹€.

  • μ‹œκ°„λ³΅μž‘λ„
    3쀑 forλ¬Έ(V^3)
    -> λ‹€μ΅μŠ€νŠΈλΌ μ•Œκ³ λ¦¬μ¦˜μ€ μΈμ ‘λ¦¬μŠ€νŠΈμ— μš°μ„ μˆœμœ„ 큐λ₯Ό μ΄μš©ν•˜λ―€λ‘œ O(V^2)의 μ‹œκ°„λ³΅μž‘λ„λ₯Ό κ°€μ Έ μ’€ 더 νš¨μœ¨μ μ΄λ‹€. ν”Œλ‘œμ΄λ“œ-와샬 μ•Œκ³ λ¦¬μ¦˜μ€ μ‹œκ°„ λ³΅μž‘λ„κ°€ 맀우 λ†’μ•„ 효율적인 μ½”λ“œ μž‘μ„±μ—μ„œ ν”Όν•˜λŠ” 것이 μ’‹λ‹€. κ·Έλž˜λ„ V의 λ²”μœ„κ°€ μ λ‹Ήν•˜λ‹€λ©΄ λͺ¨λ“  μ •μ μ˜ μ΅œλ‹¨ 경둜λ₯Ό ꡬ할 λ•Œ ν”Œλ‘œμ΄λ“œ-와샬 μ•Œκ³ λ¦¬μ¦˜μ΄ μ’‹λ‹€.

πŸ˜Άβ€πŸŒ«οΈ2. 문제 풀이 λ°©λ²•πŸ˜Άβ€πŸŒ«οΈ

πŸ§ν”Œλ‘œμ΄λ“œ-μ›Œμ…œ μ•Œκ³ λ¦¬μ¦˜μ˜ κ³Όμ •πŸ§
Screenshot_20240713_104240_Samsung Notes
=> 초기 κ·Έλž˜ν”„λ₯Ό μΈμ ‘ν–‰λ ¬λ‘œ λ‚˜νƒ€λ‚΄μ—ˆκ³ , INFλŠ” ν•΄λ‹Ή λ…Έλ“œμ—μ„œ νŠΉμ • λ…Έλ“œκΉŒμ§€ κ°€λŠ” 길이 μ—†λ‹€λŠ” λœ»μ΄λ‹€. μ—¬λŸ¬ λΌμš΄λ“œλ‘œ κ΅¬μ„±λ˜λŠ”λ°, 각 λΌμš΄λ“œλ§ˆλ‹€ 각 κ²½λ‘œμ—μ„œ μƒˆλ‘œμš΄ 쀑간 λ…Έλ“œλ‘œ μ‚¬μš©ν•  수 μžˆλŠ” λ…Έλ“œλ₯Ό μ„ νƒν•˜κ³ , 더 짧은 길이λ₯Ό μ„ νƒν•˜μ—¬ μ€„μ΄λŠ” 과정을 λ°˜λ³΅ν•œλ‹€.

πŸ§ν”Œλ‘œμ΄λ“œ-μ›Œμ…œ μ•Œκ³ λ¦¬μ¦˜μ˜ 상세 κ³Όμ •πŸ§
  1. λΌμš΄λ“œ1 : 1번 λ…Έλ“œλ₯Ό μƒˆλ‘œμš΄ 쀑간 λ…Έλ“œλ‘œ μ„€μ •
    20240713_104734

  2. λΌμš΄λ“œ2 : 2번 λ…Έλ“œλ₯Ό μƒˆλ‘œμš΄ 쀑간 λ…Έλ“œλ‘œ μ„€μ •
    20240713_104752

  3. λΌμš΄λ“œ3 : 3번 λ…Έλ“œλ₯Ό μƒˆλ‘œμš΄ 쀑간 λ…Έλ“œλ‘œ μ„€μ •
    Screenshot_20240713_104247_Samsung Notes

  4. λΌμš΄λ“œ4 : 4번 λ…Έλ“œλ₯Ό μƒˆλ‘œμš΄ 쀑간 λ…Έλ“œλ‘œ μ„€μ •
    20240713_104825

  5. λΌμš΄λ“œ5 : 5번 λ…Έλ“œλ₯Ό μƒˆλ‘œμš΄ 쀑간 λ…Έλ“œλ‘œ μ„€μ •
    Screenshot_20240713_104256_Samsung Notes (1)

μ΅œμ’… ν–‰λ ¬
Screenshot_20240713_104957_Samsung Notes

πŸ˜Άβ€πŸŒ«οΈ3. μ΅œμ’… μ½”λ“œπŸ˜Άβ€πŸŒ«οΈ

import sys

input = sys.stdin.readline # input속도 쀄이기
INF = float('inf') # μ΅œλŒ€κ°’ μ •μ˜

# λ…Έλ“œμ˜ 개수(n)κ³Ό κ°„μ„ μ˜ 개수(m) μž…λ ₯
n = int(input()) # λ„μ‹œμ˜ 개수 n
m = int(input()) # λ²„μŠ€μ˜ 개수 m

# 2차원 리슀트 (κ·Έλž˜ν”„ ν‘œν˜„) λ§Œλ“€κ³ , λ¬΄ν•œλŒ€λ‘œ μ΄ˆκΈ°ν™”(ν”Œλ‘œμ΄λ“œ-μ›Œμ…œ = 이차원 λ°°μ—΄)
graph = [[INF] * (n + 1) for _ in range(n + 1)]

# iμ—μ„œ j둜 갈 수 μ—†λŠ” 경우, 
# 자기 μžμ‹ μ—μ„œ 자기 μžμ‹ μœΌλ‘œ κ°€λŠ” λΉ„μš©μ€ 0으둜 μ΄ˆκΈ°ν™”(λŒ€κ°μ„ μ— ν•΄λ‹Ήν•˜λŠ” λΆ€λΆ„))
for i in range(1, n + 1):
    for j in range(1, n + 1):
        if i == j:
            graph[i][j] = 0

# 각 간선에 λŒ€ν•œ 정보λ₯Ό μž…λ ₯λ°›μ•„, κ·Έ κ°’μœΌλ‘œ μ΄ˆκΈ°ν™”
for _ in range(m):
    # A -> B둜 κ°€λŠ” λΉ„μš©μ„ C라고 μ„€μ •
    i, j, cost = map(int, input().split())
    if graph[i][j] > cost: # μ‹œμž‘ λ„μ‹œμ™€ 도착 λ„μ‹œ μ—°κ²°ν•˜λŠ” λ…Έμ„  ν•˜λ‚˜κ°€ x
        graph[i][j] = cost

# 점화식에 따라 ν”Œλ‘œμ΄λ“œ μ›Œμ…œ μ•Œκ³ λ¦¬μ¦˜μ„ μˆ˜ν–‰(3쀑 forλ¬Έ)
for k in range(1, n + 1):
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j])

# μˆ˜ν–‰λœ κ²°κ³Όλ₯Ό 좜λ ₯
for i in range(1, n + 1):
    for j in range(1, n + 1):
        if graph[i][j] == INF:
            print(0, end=' ')
        else:
            print(graph[i][j], end=' ')
    print()

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

Copy link
Collaborator

@pu2rile pu2rile left a comment

Choose a reason for hiding this comment

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

ν”Œλ‘œμ΄λ“œ-μ›Œμ…œ μ•Œκ³ λ¦¬μ¦˜μ€ μ•„λ¬΄λž˜λ„ 3쀑 for 루프 λ•Œλ¬Έμ— μ‹œκ°„ λ³΅μž‘λ„κ°€ 크죠... μ•Œκ³ λ¦¬μ¦˜μ˜ λ³΅μž‘λ„μ™€ μ‚¬μš© 사둀에 λŒ€ν•œ 비ꡐλ₯Ό 잘 정리해 μ£Όμ…”μ„œ 이해가 μž˜λμ–΄μš”! λ‹€μ΅μŠ€νŠΈλΌμ™€ ν”Œλ‘œμ΄λ“œ μ›Œμ…œκΉŒμ§€ 도전해 λ΄€μœΌλ‹ˆ μ΄λ²ˆμ—λŠ” 벨만-ν¬λ“œ ν•œλ²ˆ??

Copy link
Member

@oesnuj oesnuj left a comment

Choose a reason for hiding this comment

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

λ“€μ–΄λ§Œ 봀던 μ•Œκ³ λ¦¬μ¦˜μΈλ° μˆ˜ν˜„λ‹˜μ˜ 단계별 μ„€λͺ…보고 κ΅¬ν˜„κΉŒμ§€ μ‰½κ²Œ μ΄ν•΄ν–ˆλ„€μš”
μ½”λ“œκ°€ μ£Όμ„μœΌλ‘œ 정리가 잘 λ˜μ–΄μžˆμ–΄μ„œ 읽기 μ’‹μ•˜μ–΄μš”. 이번 PR도 μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€!!

@suhyun113 suhyun113 merged commit d4f81c0 into main Jul 24, 2024
1 check passed
@suhyun113 suhyun113 deleted the 10-suhyun113 branch July 24, 2024 18:09
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