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

9-suhyun113 #33

Merged
merged 9 commits into from
Jul 17, 2024
Merged

9-suhyun113 #33

merged 9 commits into from
Jul 17, 2024

Conversation

suhyun113
Copy link
Collaborator

@suhyun113 suhyun113 commented Jul 1, 2024

πŸ”— 문제 링크

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

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

μ•½ 2μ‹œκ°„

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

🩡1. 문제 μ„ μ • 이유🩡

  • μ΄μ‚°μˆ˜ν•™ μ‹œκ°„μ— μ΅œμ†Œ λΉ„μš© νŠΈλ¦¬μ— λŒ€ν•΄ λ°°μ› λ‹€. κ·Έλž˜μ„œ μ΅œμ†Œ λΉ„μš© 트리 문제λ₯Ό ν’€ λ•Œμ—λŠ” λ‹€μ΅μŠ€νŠΈλΌ μ•Œκ³ λ¦¬μ¦˜μ„ μ΄μš©ν•  수 μžˆλ‹€λŠ” 것을 μ•Œκ³ μžˆμ–΄μ„œ 이 문제λ₯Ό ν•œ 번 풀어보고 μ‹Άμ—ˆλ‹€.

🩡2. λ‹€μ΅μŠ€νŠΈλΌ μ•Œκ³ λ¦¬μ¦˜ ν’€μ΄πŸ©΅
Screenshot_20240701_191256_Samsung Notes

🩡3. μ½”λ“œ μ„€λͺ…πŸ©΅
🐧1) νž™ 자료ꡬ쑰

import heapq # νž™ 자료ꡬ쑰 톡해 μ΅œμ†Œ/μ΅œλŒ€ 값을 λΉ λ₯΄κ²Œ μ°Ύκ³  μ •λ ¬ κ°€λŠ₯
heap = [] # 빈 νž™ 생성

# νž™μ— μš”μ†Œ μΆ”κ°€
heapq.heappush(heap, 10)
heapq.heappush(heap, 2) 

# νž™μ—μ„œ κ°€μž₯ μž‘μ€ μš”μ†Œ 제거 및 λ°˜ν™˜
min_result = heapq.heappop(heap)
print(min_result) 

🐧2) 큐 반볡 ꡬ쑰

while q: # 큐가 빌 λ•ŒκΉŒμ§€ 반볡
    weight, node = heapq.heappop(q) # ν˜„μž¬ λ…Έλ“œκΉŒμ§€μ˜ 거리, ν˜„μž¬ λ…Έλ“œ(νμ—μ„œ κ°€μž₯ μž‘μ€ κ°’)
    if distance[node] < weight: # ν˜„μž¬ λ…Έλ“œκ°€ 이미 처리된 λ…Έλ“œμΈμ§€ 확인
        continue

-> λ§Œμ•½ ν˜„μž¬ λ…Έλ“œκΉŒμ§€μ˜ μ €μž₯된 거리 nodeκ°€ wdight보닀 μž‘λ‹€λ©΄, 이미 더 짧은 κ²½λ‘œκ°€ μ‘΄μž¬ν•¨μ„ 의미
-> ν˜„μž¬ λ…Έλ“œλ₯Ό μ²˜λ¦¬ν•  ν•„μš”x

    for n, w in graph[node]:
        cost = w + weight # ν˜„μž¬ λ…Έλ“œλ₯Ό 톡해 인접 λ…Έλ“œκΉŒμ§€ κ°€λŠ” μƒˆλ‘œμš΄ 거리 계산
        if distance[n] > cost: # μƒˆλ‘œμš΄ 거리가 기쑴에 μ €μž₯된 거리보닀 짧은지 확인
            distance[n] = cost # μ΅œλ‹¨ 거리 κ°±μ‹ 
            heapq.heappush(q, [cost, n]) # 인접 λ…Έλ“œλ₯Ό μš°μ„ μˆœμœ„ 큐에 μΆ”κ°€

-> 큐 반볡 ꡬ쑰 μ•„λž˜μ˜ κ·Έλ¦Ό μ°Έκ³ !
Screenshot_20240701_191131_Samsung Notes

🩡4. μ΅œμ’… μ½”λ“œπŸ©΅

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])

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

λ‹€μ΅μŠ€νŠΈλΌ μ•Œκ³ λ¦¬μ¦˜μ„ 문제둜만 풀어봀지 μ½”λ“œλ‘œ ν‘Έλ €κ³  ν•˜λ‹ˆκΉŒ μ–΄λ €μ› λ‹€. κ·Έλƒ₯ λ‹¨μˆœνžˆ μ•Œκ³ λ¦¬μ¦˜μ„ μ΄ν•΄ν•˜κΈ°λŠ” ν–ˆμ§€λ§Œ, 이λ₯Ό μš°μ„ μˆœμœ„ 큐λ₯Ό μ΄μš©ν•΄ ν‘Έλ €κ³  ν•˜λ‹ˆ 생각보닀 μ‹œκ°„μ΄ 더 κ±Έλ Έλ‹€.
κ·Έλž˜ν”„ 인접 리슀트둜 μ΄ˆκΈ°ν™”λ₯Ό ν•˜λŠ” 방법도 μ•Œκ²Œ λ˜μ—ˆκ³ , 문제λ₯Ό ν’€ λ•Œ 인접 λ…Έλ“œκ°€ μ•„λ‹Œ 뢀뢄은 λ¬΄ν•œμœΌλ‘œ μ •μ˜ν–ˆλŠ”λ°, 이 뢀뢄을 μ²˜μŒλΆ€ν„° 각 λ…Έλ“œκΉŒμ§€μ˜ 거리λ₯Ό λ¬΄ν•œλŒ€λ‘œ μ΄ˆκΈ°ν™”ν•˜λŠ” 것을 μ•Œκ²Œλ˜μ—ˆλ‹€.

Copy link

@9kyo-hwang 9kyo-hwang 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 _ in range(M):
x, y, cost = map(int, input().split()) # x -> y λ„μ‹œλ‘œ κ°€λŠ” 데 ν•„μš”ν•œ λΉ„μš© cost
graph[x].append([y, cost]) # κ·Έλž˜ν”„μ— 에지 μΆ”κ°€

Choose a reason for hiding this comment

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

μ‚¬μ†Œν•œ 팁인데, graph의 μ •λ³΄λŠ” "λΆˆλ³€"μ΄λ―€λ‘œ (인접 λ…Έλ“œ, λΉ„μš©) 정보λ₯Ό list둜 넣지 말고 tuple둜 λ„£μœΌλ©΄ λ©”λͺ¨λ¦¬μ™€ 속도 μΈ‘λ©΄μ—μ„œ μ•½κ°„ κ°œμ„ λ©λ‹ˆλ‹€ :)

graph[x].append((y, cost))

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.

κ΅ν™©λ‹˜ 리뷰 보고 μ €λŠ” ν”Œλ‘œμ΄λ“œ-μ›Œμ…œ λ°©μ‹μœΌλ‘œ ν’€μ–΄ λ΄€λŠ”λ°μš”,
μ •μ€λ‹˜ μ•Œκ³ λ¦¬μ¦˜ λ…ΈνŠΈμ— μ„€λͺ…이 μž˜λ˜μ–΄ μžˆμ–΄μ„œ μ°Έκ³ ν–ˆμŠ΅λ‹ˆλ‹€!

μ½”λ“œ
#include <stdio.h>
#define INF 987654321

int map[1001][1001];

void floydWarshall(int N)
{
    // λͺ¨λ“  정점 μŒμ„ κ²€μ‚¬ν•˜μ—¬ μ΅œλ‹¨ 거리 κ°±μ‹ 
    for (int k = 1; k <= N; k++)
    {
        for (int i = 1; i <= N; i++)
        {
            for (int j = 1; j <= N; j++)
            {
                if (map[i][j] > map[i][k] + map[k][j])
                {
                    map[i][j] = map[i][k] + map[k][j];
                }
            }
        }
    }
}

int main(void)
{
    int N, M;
    // μ •μ μ˜ 수 Nκ³Ό κ°„μ„ μ˜ 수 M을 μž…λ ₯λ°›μŒ
    scanf("%d", &N);
    scanf("%d", &M);

    // 맡 λ°°μ—΄ μ΄ˆκΈ°ν™”: 자기 μžμ‹ μœΌλ‘œμ˜ κ±°λ¦¬λŠ” 0, κ·Έ μ™ΈλŠ” INF둜 μ„€μ •
    for (int i = 1; i <= N; i++)
    {
        for (int j = 1; j <= N; j++)
        {
            if (i == j)
                map[i][j] = 0;
            else
                map[i][j] = INF;
        }
    }

    int start, end, weight;
    // κ°„μ„  정보 μž…λ ₯λ°›μ•„ 맡 배열에 반영
    for (int i = 0; i < M; i++)
    {
        scanf("%d %d %d", &start, &end, &weight);
        if (map[start][end] > weight)
        {
            map[start][end] = weight;
        }
    }

    floydWarshall(N);

    int start, end;
    
    scanf("%d %d", &start, &end);

    // μ΅œλ‹¨ 거리 좜λ ₯
    printf("%d\n", map[start][end]);

    return 0;
}

μ €λŠ” 아직 c둜 κ΅¬ν˜„ν•˜λŠ” 게 νŽΈν•΄μ„œ c둜 ν’€μ–΄ λ΄€λŠ”λ° for문을 3쀑 μ€‘μ²©μœΌλ‘œ λͺ¨λ“  정점 쌍 κ°„μ˜ μ΅œλ‹¨ 거리λ₯Ό κ΅¬ν•˜λ‹€ λ³΄λ‹ˆ μ—­μ‹œ μ‹œκ°„ 초과 κ°€^^......;; γ… γ…  파이썬으둜 λ‹€μ‹œ ν’€μ–΄ λ΄μ•Όκ² μŠ΅λ‹ˆλ‹€... ν’€κ²Œ 되면 리뷰 λ‹€μ‹œ λ‚¨κΈΈκ²Œμš”! μˆ˜κ³ ν•˜μ…¨μ–΄μš”~

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 κ³„κΈ°λ‘œ κ΅¬ν˜„ν•΄λ³΄λ‹ˆκΉŒ ν•  λ§Œν•˜λ„€μš”.
저도 쑰금만 μžˆλ‹€κ°€ λ‹€μ΅μŠ€νŠΈλΌλ‚˜, ν”Œλ‘œμ΄λ“œ-μ›Œμ…œλ“±λ“± μ•Œκ³ λ¦¬μ¦˜ λ¬Έμ œλ„ 많이 ν’€μ–΄λ΄μ•Όκ² λ„€μš”.
PR μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€πŸ‘

@suhyun113 suhyun113 merged commit a4360da into main Jul 17, 2024
1 check passed
@suhyun113 suhyun113 deleted the 9-suhyun113 branch July 17, 2024 19:46
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.

4 participants