Skip to content

Latest commit

ย 

History

History
126 lines (78 loc) ยท 4.06 KB

Dijkstra.md

File metadata and controls

126 lines (78 loc) ยท 4.06 KB

์ตœ๋‹จ ๊ฒฝ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜

  • ๊ฐ€์žฅ ์งง์€ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ (๊ธธ ์ฐพ๊ธฐ)

  • ์ฃผ๋กœ ๊ทธ๋ž˜ํ”„๋ฅผ ํ†ตํ•ด ํ‘œํ˜„

  • ๊ฐ ์ง€์ ์€ '๋…ธ๋“œ', ์ง€์ ๊ฐ„ ์—ฐ๊ฒฐ๋œ ๋„๋กœ๋Š” '๊ฐ„์„ '์œผ๋กœ ํ‘œํ˜„

  • ๋‹ค์ต์ŠคํŠธ๋ผ ์ตœ๋‹จ ๊ฒฝ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜, ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ, ๋ฒจ๋งŒ ํฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜

๋‹ค์ต์ŠคํŠธ๋ผ ์ตœ๋‹จ ๊ฒฝ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜

  • ํŠน์ • ๋…ธ๋“œ์—์„œ ๋‹ค๋ฅธ ๋…ธ๋“œ๋กœ ๊ฐ€๋Š” ๊ฐ๊ฐ์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜

  • '์Œ์˜ ๊ฐ„์„ '์ด ์—†์„ ๋•Œ ์ •์ƒ์ ์œผ๋กœ ์ž‘๋™

    • ์Œ์˜ ๊ฐ„์„ : 0๋ณด๋‹ค ์ž‘์€ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๊ฐ„์„ 

'๊ฐ€์žฅ ๋น„์šฉ์ด ์ ์€ ๋…ธ๋“œ'๋ฅผ ์„ ํƒํ•˜๋Š” ์ž„์˜์˜ ๊ณผ์ • ๋ฐ˜๋ณต์ด๋ฏ€๋กœ ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๋ถ„๋ฅ˜

์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์›๋ฆฌ

  1. ์ถœ๋ฐœ ๋…ธ๋“œ๋ฅผ ์„ ํƒ

  2. ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ํ…Œ์ด๋ธ”๋กœ ์ดˆ๊ธฐํ™”

  3. ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ ์ค‘, ๊ฐ€์žฅ ์งง์€ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ๋…ธ๋“œ๋ฅผ ์„ ํƒ

  4. ํ•ด๋‹น ๋…ธ๋“œ๋ฅผ ๊ฑฐ์ณ ๋‹ค๋ฅธ ๋…ธ๋“œ๋กœ ๊ฐ€๋Š” ๋น„์šฉ ๊ณ„์‚ฐ ํ›„ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ๊ฐฑ์‹ 

  5. 3๊ณผ 4 ๊ณผ์ •์„ ๋ฐ˜๋ณต

'ํ˜„์žฌ๊นŒ์ง€ ๊ฐ ๋…ธ๋“œ์— ๋Œ€ํ•œ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ' ์ •๋ณด๋ฅผ ํ•ญ์ƒ 1์ฐจ์› ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•˜๋ฉฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐฑ์‹ 

1. ๊ฐ„๋‹จํ•œ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜

  • O(V^2)์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„ (V: ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜)

  • ํŠน์ • ๋…ธ๋“œ๋ฅผ ์ถœ๋ฐœ์œผ๋กœ ํ•˜๋Š” 1์ฐจ์› ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑ

  • ๊ฐ ๋‹จ๊ณ„๋งˆ๋‹ค '๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ ์ค‘ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๊ฐ€ ์งง์€ ๋…ธ๋“œ๋ฅผ ์„ ํƒ' -> 1์ฐจ์› ๋ฆฌ์ŠคํŠธ ๋ชจ๋“  ์›์†Œ ํƒ์ƒ‰

๊ทธ๋ž˜ํ”„๋ฅผ ํ‘œํ˜„ํ•  ๋•Œ๋Š” ๋ฆฌ์ŠคํŠธ์˜ ํฌ๊ธฐ๋ฅผ (๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜ + 1)๋กœ ํ• ๋‹นํ•˜์ž.

๋…ธ๋“œ์˜ ๋ฒˆํ˜ธ๋ฅผ ์ธ๋ฑ์Šค๋กœ ํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ์— ๋ฐ”๋กœ ์ ‘๊ทผํ•˜๊ธฐ ์ข‹๋‹ค.

  • ์ „์ฒด ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๊ฐ€ 5000๊ฐœ ์ดํ•˜๋ฉด ์ผ๋ฐ˜์ ์œผ๋กœ ํ•ด๋‹น ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

  • ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๊ฐ€ 10000๊ฐœ๋ฅผ ๋„˜์–ด๊ฐ€๋ฉด ๊ฐœ์„ ๋œ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•ด์•ผ ํ•จ

2. ๊ฐœ์„ ๋œ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜

  • O(ElogV)์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„ (V: ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜, E: ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜)

  • ํž™ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ์˜ ๋…ธ๋“œ๋ฅผ ๋”์šฑ ๋น ๋ฅด๊ฒŒ ์ฐพ์„ ์ˆ˜ ์žˆ์Œ

    • ์ด์ „ 1์ฐจ์› ๋ฆฌ์ŠคํŠธ ์ธ๋ฑ์Šค๋ฅผ ๋ชจ๋‘ ๋„๋Š” ์„ ํ˜• ์‹œ๊ฐ„์ด ์•„๋‹Œ ๋กœ๊ทธ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆผ
์šฐ์„ ์ˆœ์œ„ ํ ๊ตฌํ˜„ ๋ฐฉ์‹ ์‚ฝ์ž… ์‹œ๊ฐ„ ์‚ญ์ œ ์‹œ๊ฐ„
๋ฆฌ์ŠคํŠธ O(1) O(N)
ํž™ O(logN) O(logN)

PriorityQueue๋ณด๋‹ค ํ†ต์ƒ์ ์œผ๋กœ heapq๊ฐ€ ๋น ๋ฅด๋‹ค.

  • ์ดํ›„ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ๊ฐฑ์‹ ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ 1๋ฒˆ ๋ฐฉ๋ฒ•๊ณผ ๋™์ผํ•˜๋‹ค.

์ถ”๊ฐ€๋กœ 2์ฐจ์› ๋ฐฐ์—ด์„ ํ†ตํ•ด ๊ฐ„์„ ๊ณผ ์‹œ๊ฐ„์„ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐฉ๋ฒ•๋ณด๋‹ค ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ†ตํ•˜๋ฉด ๋” ๋น ๋ฅด๊ฒŒ ๊ณ„์‚ฐ ๊ฐ€๋Šฅ


๋‹ค์ต์ŠคํŠธ๋ผ ์˜ˆ์ œ

ํŒŒํ‹ฐ_1238

import sys
import heapq
input = sys.stdin.readline

N, M, X = map(int,input().split())

graph = [[] for _ in range(N+1)]
for _ in range(M):
    a,b,c = map(int, input().split())
    # ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ†ตํ•ด์„œ ์ •๋ณด ์ฒ˜๋ฆฌ
    graph[a].append((b,c))

def dijkstra(start, g):
    # ์‹œ์ž‘์ ์—์„œ ๋ชจ๋“  ๊ฒฝ๋กœ ์ดˆ๊ธฐํ™”
    dist = [float('inf')] * (N+1)
    dist[start] = 0
    q = [(0, start)]

    while q:
        now_t, now = heapq.heappop(q)
        if now_t > dist[now]:
            continue
        for next, weight in g[now]:
            total_t = now_t + weight
            if total_t < dist[next]:
                dist[next] = total_t
                heapq.heappush(q, (total_t, next))

    return dist

# X์—์„œ ๋ชจ๋“  ์ •์ ์œผ๋กœ์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ
all_from_X = dijkstra(X, graph)

# ๋ชจ๋“  ์ •์ ์—์„œ X๋กœ์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ
all_to_X = [0] * (N+1)
for i in range(1, N+1):
    if i != X:
        all_to_X[i] = dijkstra(i, graph)[X]

max_t = max(all_from_X[i] + all_to_X[i] for i in range(1, N+1) if i != X)
print(max_t)

๋ชจ๋“  ๋…ธ๋“œ์—์„œ ํŠน์ • ๋…ธ๋“œ X๋กœ ํ–ฅํ•˜๋Š” ์ตœ๋‹จ์‹œ๊ฐ„๊ณผ

ํŠน์ • ๋…ธ๋“œ X์—์„œ ๋ชจ๋“  ๋…ธ๋“œ๋กœ ํ–ฅํ•˜๋Š” ์ตœ๋‹จ์‹œ๊ฐ„ ๋‘ ๊ฐ€์ง€ ๊ฒฝ์šฐ๋ฅผ ๊ณ„์‚ฐํ•ด์•ผํ•œ๋‹ค.

ํ•ด๋‹น ๋ฌธ์ œ์—์„œ ๋™์ผํ•œ ๋‹ค์ต์ŠคํŠธ๋ผ ๋ฉ”์„œ๋“œ dijkstra(start, g) ์„ ์‚ฌ์šฉํ•ด ๋‘ ๊ฐ€์ง€ ๊ฒฝ์šฐ๋ฅผ ๊ณ„์‚ฐํ–ˆ๋‹ค.

2์ฐจ์› ๋ฐฐ์—ด ๋Œ€์‹  ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ†ตํ•ด์„œ ์ฒ˜๋ฆฌํ•˜๊ฒŒ๋˜๋ฉด ํฌ์†Œ ๊ทธ๋ž˜ํ”„(๊ฐ„์„ ์˜ ๊ฐ’์ด ์—†๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์€ ๊ทธ๋ž˜ํ”„)์— ๋Œ€ํ•ด์„œ ๋” ํšจ์œจ์ ์œผ๋กœ ๊ฐ’์„ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅํ•˜๋‹ค.