๊ด€๋ฆฌ ๋ฉ”๋‰ด

DOing

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

์•Œ๊ณ ๋ฆฌ์ฆ˜

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

mangdo 2021. 5. 23. 13:58

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

  ๊ฐ€์žฅ ์งง์€ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. ๊ทธ๋ž˜์„œ '๊ธธ์ฐพ๊ธฐ'๋ฌธ์ œ๋ผ๊ณ ๋„ ๋ถˆ๋ฆฐ๋‹ค. ํ•™๋ถ€ ์ˆ˜์ค€์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋‹ค์ต์ŠคํŠธ๋ผ ์ตœ๋‹จ ๊ฒฝ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜, ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ, ๋ฒจ๋งŒ ํฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ด๋ ‡๊ฒŒ 3๊ฐ€์ง€์ด๋‹ค. ์ด ์ค‘ ๋‹ค์ต์ŠคํŠธ๋ผ ์ตœ๋‹จ ๊ฒฝ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ ๋‹ค๋ค„๋ณด๋ ค๊ณ  ํ•œ๋‹ค.

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

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

 

๐Ÿ’ก ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋™์ž‘๊ณผ์ •

1. ์ถœ๋ฐœ ๋…ธ๋“œ๋ฅผ ์„ค์ •ํ•œ๋‹ค

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

-> ๋‹ค๋ฅธ ๋ชจ๋“  ๋…ธ๋“œ๋กœ ๊ฐ€๋Š” ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ '๋ฌดํ•œ'์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค

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

-> ์ด๋•Œ ์„ ํƒ๋œ ๋…ธ๋“œ์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋Š” ํ™•์ •๋œ๋‹ค.

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

5. 3~4 ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

๐Ÿ’ก ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ : ์ˆœ์ฐจํƒ์ƒ‰

  ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ ์ค‘์—์„œ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ€์žฅ ์งง์€ ๋…ธ๋“œ๋ฅผ ์„ ํƒํ•˜๊ธฐ ์œ„ํ•ด ์ˆœ์ฐจ ํƒ์ƒ‰์„ ์ด์šฉํ•˜์—ฌ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋•Œ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(N^2)์ด๋‹ค. ์ด O(N)๋ฒˆ์— ๊ฑธ์ณ ๊ฐ€์žฅ ์งง์€ ๋…ธ๋“œ๋ฅผ ๋งค๋ฒˆ ์„ ํ˜•ํƒ์ƒ‰ํ•ด์•ผํ•˜๊ณ  ํ˜„์žฌ ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋ฅผ ๋งค๋ฒˆ ์ผ์ผํžˆ ํ™•์ธํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋”ฐ๋ผ์„œ ์ „์ฒด ๋…ธ๋“œ์˜ ์ˆ˜๊ฐ€ 5,000๊ฐœ ์ดํ•˜๋ผ๋ฉด ์ˆœ์ฐจํƒ์ƒ‰ ๋ฐฉ๋ฒ•์„ ์จ๋„ ๊ดœ์ฐฎ์ง€๋งŒ ๊ทธ ์ด์ƒ์ด๋ผ๋ฉด ํž™์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฐœ์„ ๋œ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•ด์•ผํ•œ๋‹ค.

import sys

input = sys.stdin.readline # ๋น ๋ฅธ ์ž…๋ ฅ
INF = int(1e9) # ๋ฌดํ•œ์„ ์˜๋ฏธํ•˜๋Š” ๊ฐ’์œผ๋กœ 10์–ต ์„ค์ •

# ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜, ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๊ธฐ
n, m = map(int, input().split())
# ์‹œ์ž‘ ๋…ธ๋“œ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ๋ฐ›๊ธฐ
start = int(input())
# ๋ฐฉ๋ฌธ ์ฒดํฌ
visited = [False]*(n+1)
# ์ตœ๋‹จ๊ฑฐ๋ฆฌ ํ…Œ์ด๋ธ”
distance = [INF]*(n+1)

# ๋…ธ๋“œ ์—ฐ๊ฒฐ์ •๋ณด
graph = [[] for i in range(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 j in graph[start]:
    distance[j[0]] = j[1]

  # ๋ชจ๋“  ๋…ธ๋“œ์— ๋Œ€ํ•ด ๋ฐ˜๋ณต
  for i in range(n-1):
    # ํ˜„์žฌ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ€์žฅ ์งง์€ ๋…ธ๋“œ๋ฅผ ๊บผ๋‚ด์„œ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ
    now = get_smallest_node()
    visited[now] = True
    # ์„ ํƒ๋œ ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋œ ๋‹ค๋ฅธ ๋…ธ๋“œ๋ฅผ ํ™•์ธ
    for j in graph[now]:
      # ์„ ํƒ๋œ ๋…ธ๋“œ๋ฅผ ํ†ตํ•ด ๊ฐ€๋Š” ๋น„์šฉ์„ ๋‹ค์‹œ ๊ณ„์‚ฐ
      # ์„ ํƒ๋œ ๋…ธ๋“œ์˜ ๋น„์šฉ + ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋กœ ๊ฐ€๋Š” ๋น„์šฉ
      cost = distance[now] + j[1]
      # ์„ ํƒ๋œ ๋…ธ๋“œ๋ฅผ ๊ฑฐ์ณ์„œ ๊ฐ€๋Š” ๋น„์šฉ์ด ๋” ์งง์€ ๊ฒฝ์šฐ
      if cost<distance[j[0]]:
        distance[j[0]] = cost # ์ตœ๋‹จ๊ฑฐ๋ฆฌ ํ…Œ์ด๋ธ” ๊ฐฑ์‹ 
  
#๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ˆ˜ํ–‰
dijkstra(start)

# ๋ชจ๋“  ๋…ธ๋“œ๋กœ ๊ฐ€๊ธฐ ์œ„ํ•œ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ์ถœ๋ ฅ
for i in range(1, n+1):
  # ๋„๋‹ฌํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ
  if distance[i] == INF:
    print("infinity")
  else:
    print(distance[i])

 

๐Ÿ’ก ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ : ์ตœ์†Œํž™

 ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ ์ค‘ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ€์žฅ ์งง์€ ๋…ธ๋“œ๋ฅผ ์„ ํƒํ•˜๊ธฐ ์œ„ํ•ด ์ตœ์†Œ ํž™์„ ์‚ฌ์šฉํ•œ๋‹ค.

ํŒŒ์ด์ฌ์˜ heapq๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์›์†Œ๋กœ ํŠœํ”Œ์„ ๋ฐ›์œผ๋ฉด ํŠœํ”Œ์˜ ์ฒซ๋ฒˆ์งธ ์›์†Œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ๊ตฌ์„ฑํ•œ๋‹ค. ๋”ฐ๋ผ์„œ (๊ฑฐ๋ฆฌ, ๋…ธ๋“œ ๋ฒˆํ˜ธ) ์ˆœ์„œ๋Œ€๋กœ ํŠœํ”Œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌ์„ฑํ•ด ์šฐ์„ ์ˆœ์œ„ ํ์— ๋„ฃ์œผ๋ฉด ๊ฑฐ๋ฆฌ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ๋‹ค. ํž™์—์„œ ๋…ธ๋“œ๋ฅผ ๊บผ๋ƒˆ๋Š”๋ฐ ๋งŒ์•ฝ ํ•ด๋‹น ๋…ธ๋“œ๋ฅผ ์ด๋ฏธ ์ฒ˜๋ฆฌํ•œ์ ์ด ์žˆ๋‹ค๋ฉด ๋ฌด์‹œํ•˜๊ณ  ์•„์ง ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ์— ๋Œ€ํ•ด์„œ๋งŒ ์ฒ˜๋ฆฌํ•˜๋ฉด๋œ๋‹ค.

import heapq
import sys

input = sys.stdin.readline # ๋น ๋ฅธ ์ž…๋ ฅ
INF = int(1e9) # ๋ฌดํ•œ์„ ์˜๋ฏธํ•˜๋Š” ๊ฐ’์œผ๋กœ 10์–ต ์„ค์ •

# ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜, ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๊ธฐ
n, m = map(int, input().split())
# ์‹œ์ž‘ ๋…ธ๋“œ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ๋ฐ›๊ธฐ
start = int(input())
# ์ตœ๋‹จ๊ฑฐ๋ฆฌ ํ…Œ์ด๋ธ”
distance = [INF]*(n+1)

# ๋…ธ๋“œ ์—ฐ๊ฒฐ์ •๋ณด
graph = [[] for i in range(n+1)]

for _ in range(m):
  # a๋ฒˆ๋…ธ๋“œ์—์„œ b๋ฒˆ ๋…ธ๋“œ๋กœ ๊ฐ€๋Š” ๋น„์šฉc
  a,b,c = map(int, input().split())
  graph[a].append((b,c))

# ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜(์ตœ์†Œํž™ ์ด์šฉ))
def dijkstra(start):
  q=[]
  # ์‹œ์ž‘ ๋…ธ๋“œ
  heapq.heappush(q, (0,start))
  distance[start] = 0

  while q:
    # ๊ฐ€์žฅ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๊ฐ€ ์งง์€ ๋…ธ๋“œ์— ๋Œ€ํ•œ ์ •๋ณด ๊บผ๋‚ด๊ธฐ
    dist, now = heapq.heappop(q)

    # ์ด๋ฏธ ์ฒ˜๋ฆฌ๋œ ๋…ธ๋“œ์˜€๋‹ค๋ฉด ๋ฌด์‹œ
    # ๋ณ„๋„์˜ visited ํ…Œ์ด๋ธ”์ด ํ•„์š”์—†์ด, ์ตœ๋‹จ๊ฑฐ๋ฆฌํ…Œ์ด๋ธ”์„ ์ด์šฉํ•ด ๋ฐฉ๋ฌธ์—ฌ๋ถ€ ํ™•์ธ
    if distance[now] < dist:
      continue
    # ์„ ํƒ๋œ ๋…ธ๋“œ์™€ ์ธ์ ‘ํ•œ ๋…ธ๋“œ๋ฅผ ํ™•์ธ
    for i in graph[now]:
      cost = dist + i[1]
      # ์„ ํƒ๋œ ๋…ธ๋“œ๋ฅผ ๊ฑฐ์ณ์„œ ์ด๋™ํ•˜๋Š” ๊ฒƒ์ด ๋” ๋น ๋ฅธ ๊ฒฝ์šฐ
      if cost < distance[i[0]]:
        distance[i[0]] = cost
        heapq.heappush(q, (cost,i[0]))
  
# ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ˆ˜ํ–‰
dijkstra(start)

# ๋ชจ๋“  ๋…ธ๋“œ๋กœ ๊ฐ€๊ธฐ ์œ„ํ•œ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ์ถœ๋ ฅ
for i in range(1, n+1):
  # ๋„๋‹ฌํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ
  if distance[i] == INF:
    print("infinity")
  else:
    print(distance[i])

 

๐Ÿ’ก ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์˜ˆ์‹œ

Q1. ์ „๋ณด

  ์–ด๋–ค ๋‚˜๋ผ์—๋Š” N๊ฐœ์˜ ๋„์‹œ๊ฐ€ ์žˆ๋‹ค. ๊ฐ ๋„์‹œ๋Š” ๋ณด๋‚ด๊ณ ์ž ํ•˜๋Š” ๋ฉ”์‹œ์ง€๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ, ๋‹ค๋ฅธ ๋„์‹œ๋กœ ์ „๋ณด๋ฅผ ๋ณด๋‚ด์„œ ๋‹ค๋ฅธ ๋„์‹œ๋กœ ํ•ด๋‹น๋ฉ”์‹œ์ง€๋ฅผ ์ „์†กํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ X๋ผ๋Š” ๋„์‹œ์—์„œ Y๋ผ๋Š” ๋„์‹œ๋กœ ์ „๋ณด๋ฅผ ๋ณด๋‚ด๊ณ ์ž ํ•œ๋‹ค๋ฉด, ๋„์‹œ X์—์„œ Y๋กœ ํ–ฅํ•˜๋Š” ํ†ต๋กœ๊ฐ€ ์„ค์น˜๋˜์–ด์žˆ์–ด์•ผํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด X์—์„œ Y๋กœ ํ–ฅํ•˜๋Š” ํ†ต๋กœ๋Š” ์žˆ์ง€๋งŒ Y์—์„œ X๋กœ ํ–ฅํ•˜๋Š” ํ†ต๋กœ๊ฐ€ ์—†๋‹ค๋ฉด Y๋Š” X๋กœ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ผ ์ˆ˜ ์—†๋‹ค. ๋˜ํ•œ ํ†ต๋กœ๋ฅผ ๊ฑฐ์ณ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ผ ๋•Œ๋Š” ์ผ์ •ํ•œ ์‹œ๊ฐ„์ด ์†Œ์š”๋œ๋‹ค.

 ์–ด๋Š๋‚  C๋ผ๋Š” ๋„์‹œ์—์„œ ์œ„๊ธ‰์ƒํ™ฉ์ด ๋ฐœ์ƒํ–ˆ๋‹ค. ์ตœ๋Œ€ํ•œ ๋งŽ์€ ๋„์‹œ๋กœ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ด๊ณ ์ž ํ•œ๋‹ค. ๋ฉ”์‹œ์ง€๋Š” ๋„์‹œ C์—์„œ ์ถœ๋ฐœํ•˜์—ฌ ๊ฐ ๋„์‹œ ์‚ฌ์ด์— ์„ค์น˜๋œ ํ†ต๋กœ๋ฅผ ๊ฑฐ์ณ ์ตœ๋Œ€ํ•œ ๋งŽ์ด ํผ์ ธ๋‚˜๊ฐ€์•ผํ•œ๋‹ค. ๊ฐ ๋„์‹œ์˜ ๋ฒˆํ˜ธ์™€ ํ†ต๋กœ๊ฐ€ ์„ค์น˜๋˜์–ด์žˆ๋Š” ์ •๋ณด๊ฐ€ ์ฃผ์–ด์กŒ์„๋•Œ ๋„์‹œ C์—์„œ ๋ณด๋‚ธ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐ›๊ฒŒ๋˜๋Š” ๋„์‹œ์˜ ๊ฐœ์ˆ˜๋Š” ์ด ๋ช‡๊ฐœ์ด๋ฉฐ, ๋„์‹œ๋“ค์ด ๋ชจ๋‘ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐ›๋Š”๋ฐ๊นŒ์ง€ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์€ ์–ผ๋งˆ์ธ์ง€ ๊ณ„์‚ฐํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

*์ž…๋ ฅ์กฐ๊ฑด

- ์ฒซ์งธ์ค„์— ๋„์‹œ์˜ ๊ฐฏ์ˆ˜N, ํ†ต๋กœ์˜ ๊ฐฏ์ˆ˜ M, ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ด๊ณ ์ž ํ•˜๋Š” ๋„์‹œC๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

(1<=N<=30,000, 1<=M<=200,000, 1<=C<=N)

-๋‘˜์งธ์ค„๋ถ€ํ„ฐ M+1๋ฒˆ์งธ ์ค„์— ๊ฑธ์ณ์„œ ํ†ต๋กœ์— ๋Œ€ํ•œ ์ •๋ณด X,Y,X๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด๋Š” ํŠน์ • ๋„์‹œ X์— ๋‹ค๋ฅธ ํŠน์ • ๋„์‹œ Y๋กœ ์ด์–ด์ง€๋Š” ํ†ต๋กœ๊ฐ€ ์žˆ์œผ๋ฉฐ ๋ฉ”์‹œ์ง€๊ฐ€ ์ „๋‹ฌ๋˜๋Š” ์‹œ๊ฐ„์ด Z๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค.

(1=X,Y<=N, 1<=Z<=10,000)

*์ถœ๋ ฅ์กฐ๊ฑด

- ์ฒซ์งธ์ค„์— ๋„์‹œ C์—์„œ ๋ณด๋‚ธ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐ›๋Š” ๋„์‹œ์˜ ์ด ๊ฐœ์ˆ˜์™€ ์ด ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์„ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ถœ๋ ฅํ•œ๋‹ค.

์ž…๋ ฅ ์˜ˆ์‹œ ์ถœ๋ ฅ ์˜ˆ์‹œ
6 11
1
1 2 2
1 3 5
1 4 1
2 3 3
2 4 2
3 2 3
3 6 5
4 3 3
4 5 1
5 3 1
5 6 2

0
2
3
1
2
4

 

[๋ฌธ์ œ ์•„์ด๋””์–ด]

 ํ•œ ๋„์‹œ์—์„œ ๋‹ค๋ฅธ ๋„์‹œ๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ ๋ฌธ์ œ๋ฅผ ๋ฌป๋Š” ๋ฌธ์ œ์ด๋‹ค. ์ด๋•Œ N,M์˜ ๋ฒ”์œ„๊ฐ€ ํฌ๊ธฐ ๋•Œ๋ฌธ์— ํž™์„ ์‚ฌ์šฉํ•œ ๋‹ค์ต์ŠคํŠธ๋ผ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ํ•ด๊ฒฐํ•ด์•ผํ•œ๋‹ค. 

 

[์ฝ”๋“œ]

import heapq
import sys

input = sys.stdin.readline # ๋น ๋ฅธ ์ž…๋ ฅ
INF = int(1e9) # ๋ฌดํ•œ์„ ์˜๋ฏธํ•˜๋Š” ๊ฐ’์œผ๋กœ 10์–ต ์„ค์ •

# ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜, ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜, ์‹œ์ž‘๋…ธ๋“œ๋ฅผ ์ž…๋ ฅ๋ฐ›๊ธฐ
n, m, start = map(int, input().split())

# ์ตœ๋‹จ๊ฑฐ๋ฆฌ ํ…Œ์ด๋ธ”
distance = [INF]*(n+1)

# ๋…ธ๋“œ ์—ฐ๊ฒฐ์ •๋ณด
graph = [[] for i in range(n+1)]

for _ in range(m):
  # x๋ฒˆ๋…ธ๋“œ์—์„œ y๋ฒˆ ๋…ธ๋“œ๋กœ ๊ฐ€๋Š” ๋น„์šฉz
  x,y,z = map(int, input().split())
  graph[x].append((y,z))

# ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜(์ตœ์†Œํž™ ์ด์šฉ))
def dijkstra(start):
  q=[]
  # ์‹œ์ž‘ ๋…ธ๋“œ
  heapq.heappush(q, (0,start))
  distance[start] = 0

  while q:
    # ๊ฐ€์žฅ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๊ฐ€ ์งง์€ ๋…ธ๋“œ์— ๋Œ€ํ•œ ์ •๋ณด ๊บผ๋‚ด๊ธฐ
    dist, now = heapq.heappop(q)

    # ์ด๋ฏธ ์ฒ˜๋ฆฌ๋œ ๋…ธ๋“œ์˜€๋‹ค๋ฉด ๋ฌด์‹œ
    # ๋ณ„๋„์˜ visited ํ…Œ์ด๋ธ”์ด ํ•„์š”์—†์ด ์ตœ๋‹จ๊ฑฐ๋ฆฌํ…Œ์ด๋ธ”์„ ์ด์šฉํ•ด ๋ฐฉ๋ฌธ์—ฌ๋ถ€ ํ™•์ธ
    if distance[now]<dist:
      continue
    # ์„ ํƒ๋œ ๋…ธ๋“œ์™€ ์ธ์ ‘ํ•œ ๋…ธ๋“œ๋ฅผ ํ™•์ธ
    for i in graph[now]:
      cost = dist + i[1]
      # ์„ ํƒ๋œ ๋…ธ๋“œ๋ฅผ ๊ฑฐ์ณ์„œ ์ด๋™ํ•˜๋Š” ๊ฒƒ์ด ๋” ๋น ๋ฅธ ๊ฒฝ์šฐ
      if cost < distance[i[0]]:
        distance[i[0]] = cost
        heapq.heappush(q, (cost,i[0]))
  
#๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ˆ˜ํ–‰
dijkstra(start)

count = 0
max_distance = 0

for d in distance:
  if d!= INF:
    count+=1
    max_distance = max(max_distance, d)

## ์‹œ์ž‘๋…ธ๋“œ๋Š” ์ œ์™ธํ•ด์•ผํ•จ์œผ๋กœ count-1
print(count-1, max_distance)