Search

์ตœ๋‹จ๊ฒฝ๋กœ

Created At
2022/03/13 18:30
Updated At
2023/04/06 10:48
๋ถ„๋ฅ˜
์•Œ๊ณ ๋ฆฌ์ฆ˜

๋‹ค์ต์ŠคํŠธ๋ผ vs ๋ฒจ๋งŒ-ํฌ๋“œ vs ํ”Œ๋กœ์ด๋“œ-์™€์ƒฌ

1.
ํ•œ ๋…ธ๋“œ์—์„œ ๋‹ค๋ฅธ ๋…ธ๋“œ๋“ค๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฒฝ๋กœ (์Œ์ˆ˜ ๊ฐ€์ค‘์น˜ ๋ถˆ๊ฐ€) : ๋‹ค์ต์ŠคํŠธ๋ผ
2.
ํ•œ ๋…ธ๋“œ์—์„œ ๋‹ค๋ฅธ ๋…ธ๋“œ๋“ค๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฒฝ๋กœ (์Œ์ˆ˜ ๊ฐ€์ค‘์น˜ ๊ฐ€๋Šฅ) : ๋ฒจ๋งŒ-ํฌ๋“œ
3.
๋ชจ๋“  ๋…ธ๋“œ์—์„œ ๋‹ค๋ฅธ ๋…ธ๋“œ๋“ค๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฒฝ๋กœ (์Œ์ˆ˜ ๊ฐ€์ค‘์น˜ ๊ฐ€๋Šฅ) : ํ”Œ๋กœ์ด๋“œ-์™€์ƒฌ

๋‹ค์ต์ŠคํŠธ๋ผ

1.
์ถœ๋ฐœ ๋…ธ๋“œ๋ฅผ ์„ค์ •ํ•œ๋‹ค.
2.
์ตœ๋‹จ ๊ฑฐ๋ฆฌ ํ…Œ์ด๋ธ”์„ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.
3.
๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ๋“ค ์ค‘, ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ€์žฅ ์งง์€ ๋…ธ๋“œ๋ฅผ ์„ ํƒํ•œ๋‹ค. (๊ทธ๋ฆฌ๋””)
โ‡’ ์šฐ์„ ์ˆœ์œ„ ํ(์ตœ์†Œํž™) ๋ฅผ ์“ฐ๋ƒ, ๋ฆฌ์ŠคํŠธ๋ฅผ ์“ฐ๋ƒ์— ๋”ฐ๋ผ ๋‹ค๋ฆ„
4.
์„ ํƒํ•œ ๋…ธ๋“œ๋ฅผ ๊ฑฐ์ณ์„œ ๋‹ค๋ฅธ ๋…ธ๋“œ๋กœ ๊ฐ€๋Š” ๋น„์šฉ์„ ๊ณ„์‚ฐํ•˜์—ฌ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ํ…Œ์ด๋ธ”์„ ๊ฐฑ์‹ ํ•œ๋‹ค. (dp)
5.
3-4๋ฒˆ ๋ฐ˜๋ณตํ•œ๋‹ค.
๋‹ค์ต์ŠคํŠธ๋ผ์—์„œ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ๋“ค ์ค‘, ์ตœ๋‹จ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ€์žฅ ์งง์€ ๋…ธ๋“œ๋ฅผ ์„ ํƒํ•ด์•ผ ํ•œ๋‹ค.
๋ฐฉ๋ฒ•1. ์ตœ๋‹จ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ€์žฅ ์งง์€ ๋…ธ๋“œ๋ฅผ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ํ…Œ์ด๋ธ”(๋ฆฌ์ŠคํŠธ)์—์„œ ๋งค๋ฒˆ ์ฐพ์•„๋ƒ„ : O(V)O(V)
๋ฐฉ๋ฒ•2. ์ตœ๋‹จ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ€์žฅ ์งง์€ ๋…ธ๋“œ๋ฅผ ์ตœ์†Œ ํž™(์šฐ์„ ์ˆœ์œ„ ํ)์—์„œ ์ฐพ์•„๋ƒ„ : O(logV)O(logV)
๋ฐฉ๋ฒ•1 ๋กœ ์ตœ์ข… ๊ตฌํ˜„ ์‹œ โ‡’ O(V2)O(V^2)
๋ฐฉ๋ฒ•2 ๋กœ ์ตœ์ข… ๊ตฌํ˜„ ์‹œ โ‡’ O(Eโˆ—logV)O(E *logV) ( E : ๊ฐ„์„ ๊ฐœ์ˆ˜ , V : ๋…ธ๋“œ๊ฐœ์ˆ˜ )
๋‹น์—ฐํžˆ ๋ฐฉ๋ฒ•2 ๊ฐ€ ์ข‹์œผ๋ฉฐ, ๊ตฌํ˜„์ด ๋ฐฉ๋ฒ•1 ๋ณด๋‹ค ์กฐ๊ธˆ ์–ด๋ ค์›Œ์ง€๊ธด ํ•˜์ง€๋งŒ ๋ฐฉ๋ฒ•2๋ฅผ ์•Œ์•„์•ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๋ฐฉ๋ฒ•2 ์ฝ”๋“œ๋งŒ ์ž‘์„ฑํ–ˆ๋‹ค.
ํ•„์š”ํ•œ ๋ณ€์ˆ˜ ๋ฐ ๋ฆฌ์ŠคํŠธ
โ€ข
๊ทธ๋ž˜ํ”„ (์‹œ์ž‘,๋„์ฐฉ,๊ฐ€์ค‘์น˜)
โ€ข
์ตœ๋‹จ๊ฑฐ๋ฆฌ ํ…Œ์ด๋ธ” (1์ฐจ์› ๋ฆฌ์ŠคํŠธ)
โ€ข
ํŠœํ”Œํ˜•์‹์˜ (๊ฑฐ๋ฆฌ,๋…ธ๋“œ) ๋ฅผ ์ €์žฅํ•˜๋Š” ์šฐ์„ ์ˆœ์œ„ ํ
import heapq as hq import sys read = sys.stdin.readline v, e = map(int, read().split()) # ์ •์ ๊ฐœ์ˆ˜, ๊ฐ„์„ ๊ฐœ์ˆ˜ start = int(read()) # ์‹œ์ž‘ ์ •์  graph = dict() for i in range(1, v + 1): graph[i] = [] for _ in range(e): a, b, w = map(int, read().split()) # ์‹œ์ž‘,๋„์ฐฉ,๊ฐ€์ค‘์น˜ graph[a].append((b, w)) MAX = int(1e9) distance = [MAX] * (v + 1) # ์ตœ๋‹จ๊ฑฐ๋ฆฌ ํ…Œ์ด๋ธ” ๋ฌดํ•œ๋Œ€๋กœ ์ดˆ๊ธฐํ™” def dijkstra(start): q = [] # (์‹œ์ž‘์ •์ ์œผ๋กœ๋ถ€ํ„ฐ์˜ ๊ฑฐ๋ฆฌ, ๋…ธ๋“œ๋ฒˆํ˜ธ) ๋ฅผ ์ €์žฅํ•  ์ตœ์†Œ ํž™ # pop ์‹œ ๊ฑฐ๋ฆฌ๊ฐ€ ์ž‘์€ ์ˆœ์œผ๋กœ pop ๋จ # ์‹œ์ž‘์  ์„ธํŒ… distance[start] = 0 # ์‹œ์ž‘์ ์˜ ๊ฑฐ๋ฆฌ๋Š” 0 hq.heappush(q, (0, start)) # ํž™์— ์‹œ์ž‘์  push while q: dist, cur = hq.heappop(q) # ์ตœ์†Œ ํž™์—์„œ (๊ฑฐ๋ฆฌ,๋…ธ๋“œ๋ฒˆํ˜ธ) pop # ํ˜„์žฌ ๋…ธ๋“œ๊ฐ€ ์ฒ˜๋ฆฌ๋˜์ง€ ์•Š์€ ๋…ธ๋“œ์—ฌ์•ผํ•จ if dist <= distance[cur]: # popํ•œ ๋…ธ๋“œ์˜ ๊ฑฐ๋ฆฌ <= ์ตœ๋‹จ๊ฑฐ๋ฆฌ ํ…Œ์ด๋ธ” ์ƒ์— ๊ธฐ๋ก๋œ ๊ฑฐ๋ฆฌ for i in graph[cur]: # i[0] : ์ธ์ ‘๋…ธ๋“œ๋ฒˆํ˜ธ, i[1] : ๊ฑฐ๋ฆฌ cost = dist + i[1] # ํ˜„์žฌ๋…ธ๋“œ๋ฅผ ๊ฑฐ์ณ๊ฐˆ ๊ฒฝ์šฐ์˜ ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ if cost < distance[i[0]]: # ์ƒˆ๋กญ๊ฒŒ ๊ณ„์‚ฐํ•œ ๊ฑฐ๋ฆฌ๊ฐ€ ์ž‘์„๋•Œ๋งŒ ๊ฐฑ์‹  distance[i[0]] = cost # ๊ฑฐ๋ฆฌํ…Œ์ด๋ธ” ๊ฐฑ์‹  hq.heappush(q, (cost, i[0])) # ๊ฐฑ์‹  ๋…ธ๋“œ์˜ ๊ฑฐ๋ฆฌ์™€ ๋…ธ๋“œ๋ฒˆํ˜ธ๋ฅผ heap ์— push dijkstra(start) for i in range(1, v + 1): if distance[i] == MAX: print("INF") else: print(distance[i])
Python
๋ณต์‚ฌ

Reference