dijkstra

๋…ธ๋“œ๋ณ„ distance ๋ฆฌ์ŠคํŠธ

๋…ธ๋“œ ๋ณ„ ์—ฐ๊ฒฐ ๋…ธ๋“œ ์ •๋ณด

์ˆ˜ํ–‰ํ–ˆ๋Š”์ง€ Visited ๋ฆฌ์ŠคํŠธ์— ์Œ“์•„๊ฐ€๋ฉฐ ์ˆ˜ํ–‰

ํ๋ฆ„

  1. ์ถœ๋ฐœ๋…ธ๋“œ ์„ค์ •

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

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

    ์ถœ๋ฐœ๋…ธ๋“œ๋กœ๋ถ€ํ„ฐ ๋งค ๋ฒˆ ๊ฐ€์žฅ ์งง์€ ๊ฑฐ๋ฆฌ๋ฅผ ์šฐ์„ ์ ์œผ๋กœ ์„ ํƒํ•ด์™”๊ธฐ ๋•Œ๋ฌธ์—, ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ๊ฐ€ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๊ฐ€ ๋œ๋‹ค.

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

  5. 3, 4 ๋ฒˆ ๊ณผ์ • ๋ฐ˜๋ณต

๊ฐ„๋‹จํ•œ ๊ตฌํ˜„, ์†๋„ ๋Š๋ฆผ

import sys

INF = int(1e9)

# ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜, ๊ฐ„์„ ์˜ ๊ฐœ
n, m = map(int,sys.stdin.readline().split())
# ์‹œ์ž‘ ๋…ธ๋“œ ๋ฒˆํ˜ธ ์ž…๋ ฅ
start = int(input())
# ๊ฐ ๋…ธ๋“œ์— ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ๋…ธ๋“œ์— ๋Œ€ํ•œ ์ „๋ณด๋ฅผ ๋‹ด๋Š” ๋ฆฌ์ŠคํŠธ
graph = [[] for i in range(n + 1)]
# ๋ฐฉ๋ฌธ ๋ฆฌ์ŠคํŠธ
visited = [False] * (n + 1)
# ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ํ…Œ์ด๋ธ” ๋ชจ๋‘ ๋ฌดํ•œ์œผ๋กœ ์ดˆ๊ธฐํ™”
distance = [INF] * (n + 1)
# ๋ชจ๋“  ๊ฐ„์„  ์ •๋ณด ์ž…๋ ฅ๋ฐ›๊ธฐ
for _ in range(m):
    # a ๋…ธ๋“œ์—์„œ b ๋…ธ๋“œ๋กœ ๊ฐ€๋Š” ๋น„์šฉ์ด c
    a, b, c = map(int,input().split())
    graph[a].append((b,c))

def get_smallest_node():
    min_value = INF
    index = 0
    for i in range(1, n+1):
        if distance[i] < min_value and not visited[i]:
            min_value = distance[i]
            index = i
    return index

def dijkstra(start):
    distance[start] = 0
    visited[start] = True

    for node_info in graph[start]:
        distance[node_info[0]] = node_info[1]

    for i in range(n-1):

        now = get_smallest_node()
        visited[now] = True

        for node_info in graph[now]:
            cost = distance[now] + node_info[1]
            if cost < distance[node_info[0]]:
                distance[node_info[0]] = cost

์šฐ์„ ์ˆœ์œ„ํ(heapq) ๋ฅผ ์ด์šฉ

import heapq
import sys

# ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜, ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ ์ž…๋ ฅ๋ฐ›๊ธฐ
n, m = map(int, input().split())
# ์‹œ์ž‘ ๋…ธ๋“œ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ๋ฐ›๊ธฐ
start = int(input())
# ๊ฐ ๋…ธ๋“œ์— ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ๋…ธ๋“œ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋‹ด๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค๊ธฐ
graph = [[] for i in range(n + 1)]
# ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ํ…Œ์ด๋ธ” ์ดˆ๊ธฐํ™”
INF = int(1e9)
distance = [INF] * (n + 1)

# ๊ฐ„์„  ์ •๋ณด ์ž…๋ ฅ๋ฐ›๊ธฐ
for _ in range(m):
  a, b, c = map(int, input().split())
  graph[a].append((c, b))

def dijkstra(start):
  q = []
  # 1. ์‹œ์ž‘ ๋…ธ๋“œ๋กœ ๊ฐ€๊ธฐ ์œ„ํ•œ ์ตœ๋‹จ ๊ฒฝ๋กœ๋Š” 0 ์œผ๋กœ ์„ค์ •ํ•˜์—ฌ, ํ์— ์‚ฝ์ž…
  heapq.heappush(q, (0, start))
  distance[start] = 0
  while q:
    dist, now = heapq.heappop(q)
    # 2. ํ˜„์žฌ ๋…ธ๋“œ๊ฐ€ ์ด๋ฏธ ์ฒ˜๋ฆฌ๋œ ์ ์ด ์žˆ๊ณ ! ๊ฐ€ ์ค‘์š”ํ•จ, (๋ฐฉ๋ฌธ ์•ˆ ๋˜์–ด ์žˆ์œผ๋ฉด INF ์ด๊ธฐ ๋•Œ๋ฌธ์—, ๋ฌด์กฐ๊ฑด ํผ) ๊ทธ ๊ฐ’์ด ๋” ์ž‘๋‹ค๋ฉด, ์ธ์ ‘๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•˜์ง€ ์•Š์Œ
    if distance[now] < dist:
      continue
    # ์ธ์ ‘ ๋…ธ๋“œ ํ™•์ธ
    for dis, node in graph[now]:
      cost = dist + dis
      if cost < distance[node]:
        distance[node] = cost
        heapq.heappush(q, (cost, node))

Last updated