์ฐธ๊ณ ์ž๋ฃŒ : ์ด๊ฒƒ์ด ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ๋‹ค (๋‚˜๋™๋นˆ ์ €)

1. ๊ฐ€์žฅ ๋น ๋ฅธ ๊ธธ ์ฐพ๊ธฐ

๊ฐ€์žฅ ๋น ๋ฅด๊ฒŒ ๋„๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•

  • ์ตœ๋‹จ ๊ฒฝ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜ : ๊ฐ€์žฅ ์งง์€ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜, ๊ธธ์ฐพ๊ธฐ ๋ฌธ์ œ
  • ๋ณดํ†ต ๊ทธ๋ž˜ํ”„๋กœ ํ‘œํ˜„ํ•˜๋ฉฐ, ๊ฐ ์ง€์ ์€ ๊ทธ๋ž˜ํ”„์—์„œ โ€˜๋…ธ๋“œโ€™๋กœ ํ‘œํ˜„๋˜๊ณ  ์ง€์  ๊ฐ„ ์—ฐ๊ฒฐ๋œ ๋„๋กœ๋Š” โ€˜๊ฐ„์„ โ€™์œผ๋กœ ํ‘œํ˜„๋จ
  • ํ•™๋ถ€์ƒ ์ˆ˜์ค€์œผ๋กœ๋Š” ๋‹ค์ต์ŠคํŠธ๋ผ ์ตœ๋‹จ ๊ฒฝ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜, ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ, ๋ฒจ๋งŒ ํฌ๋“œ 3๊ฐ€์ง€๋ฅผ ๋‹ค๋ฃฌ๋‹ค

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

  • ๊ทธ๋ž˜ํ”„์—์„œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋…ธ๋“œ๊ฐ€ ์žˆ์„ ๋•Œ, ํŠน์ •ํ•œ ๋…ธ๋“œ์—์„œ ์ถœ๋ฐœํ•˜์—ฌ ๋‹ค๋ฅธ ๋…ธ๋“œ๋กœ ๊ฐ€๋Š” ๊ฐ๊ฐ์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•ด์ฃผ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜
  • ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์Œ์˜ ๊ฐ„์„ ์ด ์—†์„ ๋•Œ ์ •์ƒ์ ์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค. (GPS ์†Œํ”„ํŠธ์›จ์–ด ๊ธฐ๋ณธ ์•Œ๊ณ ๋ฆฌ์ฆ˜)
  • ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๋ถ„๋ฅ˜๋˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.
  1. ์ถœ๋ฐœ ๋…ธ๋“œ๋ฅผ ์„ค์ •ํ•œ๋‹ค.
  2. ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ํ…Œ์ด๋ธ”์„ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.
  3. ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ ์ค‘์—์„œ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ€์žฅ ์งง์€ ๋…ธ๋“œ๋ฅผ ์„ ํƒํ•œ๋‹ค.
  4. ํ•ด๋‹น ๋…ธ๋“œ๋ฅผ ๊ฑฐ์ณ ๋‹ค๋ฅธ ๋…ธ๋“œ๋กœ ๊ฐ€๋Š” ๋น„์šฉ์„ ๊ณ„์‚ฐํ•˜์—ฌ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ํ…Œ์ด๋ธ”์„ ๊ฐฑ์‹ ํ•œ๋‹ค.
  5. ์œ„ ๊ณผ์ •์—์„œ 3๊ณผ 4๋ฒˆ์„ ๋ฐ˜๋ณตํ•œ๋‹ค.
  • ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•˜๋Š” ๊ณผ์ •์—์„œ ๊ฐ ๋…ธ๋“œ์— ๋Œ€ํ•œ ํ˜„์žฌ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ์ •๋ณด๋ฅผ ํ•ญ์ƒ 1์ฐจ์› ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•˜๋ฉฐ ๋ฆฌ์ŠคํŠธ(์ตœ๋‹จ ๊ฑฐ๋ฆฌ ํ…Œ์ด๋ธ”)๋ฅผ ๊ณ„์† ๊ฐฑ์‹ ํ•œ๋‹ค๋Š” ํŠน์ง•์ด ์žˆ๋‹ค.
  • ๋งค๋ฒˆ ํ˜„์žฌ ์ฒ˜๋ฆฌํ•˜๊ณ  ์žˆ๋Š” ๋…ธ๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ฃผ๋ณ€ ๊ฐ„์„ ์„ ํ™•์ธํ•œ๋‹ค. ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ ์ค‘์—์„œ ํ˜„์žฌ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ€์žฅ ์งง์€ ๋…ธ๋“œ๋ฅผ ํ™•์ธํ•ด ๊ทธ ๋…ธ๋“œ์— ๋Œ€ํ•˜์—ฌ 4๋ฒˆ ๊ณผ์ •์„ ์ˆ˜ํ–‰ํ•˜๋ฏ€๋กœ ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

๋ฐฉ๋ฒ• 1. ๊ฐ„๋‹จํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ : ์‹œ๊ฐ„๋ณต์žก๋„ O(V^2)

import sys
input = sys.stdin.readline
INF = int(1e9) # ๋ฌดํ•œ์„ ์˜๋ฏธ

n, m = map(int, input().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 not visited[i] and distance[i] < min_value  :
            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]

    # ์‹œ์ž‘ ๋…ธ๋“œ๋ฅผ ์ œ์™ธํ•œ ์ „์ฒด n-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])
  • ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๋‚˜ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋งŽ์„ ๋•Œ๋Š” ๊ฐœ์„ ๋œ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

๋ฐฉ๋ฒ• 2. ๊ฐœ์„ ๋œ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ : ์‹œ๊ฐ„๋ณต์žก๋„ O(ElogV)

Heap ์ž๋ฃŒ๊ตฌ์กฐ

  • ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ ์ค‘ ํ•˜๋‚˜
  • ์šฐ์„ ์ˆœ์œ„ ํ : ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๊ฐ€์žฅ ๋†’์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋จผ์ € ์‚ญ์ œํ•œ๋‹ค๋Š” ํŠน์ง•์ด ์žˆ๋Š” ํ
  • ์ตœ์†Œ ํž™ : ๊ฐ’์ด ๋‚ฎ์€ ๋ฐ์ดํ„ฐ ๋จผ์ € ์‚ญ์ œ ์ตœ๋Œ€ ํž™ : ๊ฐ’์ด ๋†’์€ ๋ฐ์ดํ„ฐ ๋จผ์ € ์‚ญ์ œ
  • ๋ฐฉ๋ฒ• 1์˜ ์ฝ”๋“œ์—์„œ ํ˜„์žฌ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๋…ธ๋“œ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ชฉ์ ์œผ๋กœ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์ถ”๊ฐ€๋กœ ์ด์šฉํ•œ๋‹ค๋Š” ์ ์ด ๋‹ค๋ฆ„
import heapq
import sys
input = sys.stdin.readline
INF = int(1e9) # ๋ฌดํ•œ์„ ์˜๋ฏธ

n, m = map(int, input().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 dijkstra(start):
    q = []
    # ์‹œ์ž‘ ๋…ธ๋“œ ์ดˆ๊ธฐํ™”
    heapq.heappush(q, (0, start))
    distance[start] = 0
    while q: # ํ๊ฐ€ ๋น„์–ด์žˆ์ง€ ์•Š๋‹ค๋ฉด
        dist, now = heapq. heappop(q)
        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])

ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ : ์‹œ๊ฐ„๋ณต์žก๋„ O(N^3)

  • ๋ชจ๋“  ์ง€์ ์—์„œ ๋‹ค๋ฅธ ๋ชจ๋“  ์ง€์ ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๋ชจ๋‘ ๊ตฌํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜
  • ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ DP๋ผ๋Š” ํŠน์ง•์ด ์žˆ์Œ
  • ์ ํ™”์‹ D(ab) = min(D(ab), D(ak) + D(kb))
  • 3์ค‘ ๋ฐ˜๋ณต๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ์ ํ™”์‹์— ๋”ฐ๋ผ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ํ…Œ์ด๋ธ”์„ ๊ฐฑ์‹ ํ•œ๋‹ค.
INF = int(1e9)

n = int(input())
m = int(input())

graph = [[INF] * (n+1) for _ in range(n+1)]

# ์ž๊ธฐ ์ž์‹ ์—์„œ ์ž๊ธฐ ์ž์‹ ์œผ๋กœ ๊ฐ€๋Š” ๋น„์šฉ์€ 0์œผ๋กœ ์ดˆ๊ธฐํ™”
for a in range(1, n+1):
    for b in range(1, n+1):
        if a == b : graph[a][b] = 0

# ๊ฐ ๊ฐ„์„ ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ๊ทธ ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”
for _ in range(m):
    a, b, c = map(int, input().split())
    graph[a][b] = c

# ์ ํ™”์‹์— ๋”ฐ๋ผ ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ˆ˜ํ–‰
for k in range(1, n+1) :
    for a in range(1, n+1) :
        for b in range(1, n+1):
            graph[a][b] = min(graph[a][b], graph[a][k] + graph[k][b])

# ์ˆ˜ํ–‰๋œ ๊ฒฐ๊ณผ ์ถœ๋ ฅ
for a in range(1, n+1) :
    for b in range(1, n+1):
        if graph[a][b] == INF:
            print("INFINITY", end=" ")
        else : print(graph[a][b], end= " ")

2. ๋ฏธ๋ž˜ ๋„์‹œ

๋‚œ์ด๋„ ์ค‘

๋ฌธ์ œ

๊ณต์ค‘ ๋ฏธ๋ž˜๋„์‹œ์—๋Š” 1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ๊นŒ์ง€์˜ ํšŒ์‚ฌ๊ธฐ ์žˆ๋Š”๋ฐ ํŠน์ • ํšŒ์‚ฌ๋“ค๋ผ๋ฆฌ๋Š” ๋„๋กœ๋ฅผ ํ†ตํ•ด ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค. ๋ฐฉ๋ฌธ ํŒ๋งค์› A๋Š” ํ˜„์žฌ 1๋ฒˆ ํšŒ์‚ฌ์— ์œ„์น˜ํ•ด ์žˆ์œผ๋ฉฐ, X๋ฒˆ ํšŒ์‚ฌ์— ๋ฐฉ๋ฌธํ•ด ๋ฌผ๊ฑด์„ ํŒ๋งคํ•˜๊ณ ์ž ํ•œ๋‹ค. ๊ณต์ค‘ ๋ฏธ๋ž˜ ๋„์‹œ์—์„œ ํŠน์ • ํšŒ์‚ฌ์— ๋„์ฐฉํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•์€ ํšŒ์‚ฌ๋ผ๋ฆฌ ์—ฐ๊ฒฐ๋œ ๋„๋กœ๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์œ ์ผํ•˜๋ฉฐ ์—ฐ๊ฒฐ๋œ 2๊ฐœ ํšŒ์‚ฌ๋Š” ์–‘๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋‹ค. ํŠน์ •ํšŒ์‚ฌ์™€ ๋‹ค๋ฅธ ํšŒ์‚ฌ๊ฐ€ ๋„๋กœ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค๋ฉด ์ •ํ™•ํžˆ 1๋งŒํผ์˜ ์‹œ๊ฐ„์œผ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋‹ค.

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

ํ•ด์„ค

์ „ํ˜•์ ์ธ ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ์ด๋‹ค. N์˜ ๋ฒ”์œ„๊ฐ€ 100์ดํ•˜๋กœ ๋งค์šฐ ํ•œ์ •์ ์ด๋ฏ€๋กœ ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ๋„ ๋น ๋ฅด๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋‹ค. ์ด ๋ฌธ์ œ์˜ ํ•ต์‹ฌ ์•„์ด๋””์–ด๋Š” 1๋ฒˆ ๋…ธ๋“œ์—์„œ X๋ฅผ ๊ฑฐ์ณ K๋กœ ๊ฐ€๋Š” ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋Š” (1๋ฒˆ ๋…ธ๋“œ์—์„œ X๊นŒ์ง€ ์ตœ๋‹จ๊ฑฐ๋ฆฌ + X์—์„œ K๊นŒ์ง€ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ)์ด๋‹ค.

์ฝ”๋“œ

INF = int(1e9)
n, m = map(int, input().split())
graph = [[INF] * (n+1) for _ in range(n+1)]

for a in range(1, n+1):
    for b in range(1, n+1):
        if a == b:
            graph[a][b] = 0

for _ in range(m):
    a, b= map(int, input().split()) # ๋น„์šฉ์€ 1์ด๋ผ๊ณ  ์ฃผ์–ด์ง
    graph[a][b] = 1
    graph[b][a] = 1

X, K = map(int, input().split())

for k in range(1, n+1):
    for a in range(1, n+1):
        for b in range(1, n+1):
            graph[a][b] = min(graph[a][b], graph[a][k] + graph[k][b])

distance = graph[1][K] + graph[K][X]

if distance >= INF : print('-1')
else: print(distance)

3. ์ „๋ณด

๋‚œ์ด๋„ ์ƒ

๋ฌธ์ œ

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

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

ํ•ด์„ค

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

์ฝ”๋“œ

import heapq
import sys

input = sys.stdin.readline
INF = int(1e9)

n, m, start = map(int, input().split())
graph = [[] for i in range(n+1)]
distance = [INF] * (n+1)

for _ in range(m):
    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)
        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_dist = 0
for d in instance:
    if d != INF :
        count += 1
        max_dist = max(max_dist, d)

print(count-1, max_dist)