[BOJ][๐ŸŸก5][๋ฐฑ์ค€#05972] ํƒ๋ฐฐ ๋ฐฐ์†ก

์ž‘์„ฑ:    

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #5972


๋ฌธ์ œ

๋†๋ถ€ ํ˜„์„œ๋Š” ๋†๋ถ€ ์ฐฌํ™์ด์—๊ฒŒ ํƒ๋ฐฐ๋ฅผ ๋ฐฐ๋‹ฌํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ง€๊ธˆ,ย ๊ฐˆ ์ค€๋น„๋ฅผ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ‰ํ™”๋กญ๊ฒŒ ๊ฐ€๋ ค๋ฉด ๊ฐ€๋Š” ๊ธธ์— ๋งŒ๋‚˜๋Š” ๋ชจ๋“  ์†Œ๋“ค์—๊ฒŒ ๋ง›์žˆ๋Š” ์—ฌ๋ฌผ์„ ์ค˜์•ผย ํ•ฉ๋‹ˆ๋‹ค. ๋ฌผ๋ก  ํ˜„์„œ๋Š” ๊ตฌ๋‘์‡ ๋ผ์„œ ์ตœ์†Œํ•œ์˜ย ์†Œ๋“ค์„ ๋งŒ๋‚˜๋ฉด์„œ ์ง€๋‚˜๊ฐ€๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค. ๋†๋ถ€ ํ˜„์„œ์—๊ฒŒ๋Š” ์ง€๋„๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.ย Nย (1 <= N <= 50,000) ๊ฐœ์˜ ํ—›๊ฐ„๊ณผ, ์†Œ๋“ค์˜ ๊ธธ์ธ M (1 <= Mย <= 50,000) ๊ฐœ์˜ ์–‘๋ฐฉํ–ฅ ๊ธธ์ด ๊ทธ๋ ค์ ธ ์žˆ๊ณ ,ย ๊ฐ๊ฐ์˜ ๊ธธ์€ C_i (0 <= C_iย <= 1,000) ๋งˆ๋ฆฌ์˜ ์†Œ๊ฐ€ย ์žˆ์Šต๋‹ˆ๋‹ค. ์†Œ๋“ค์˜ ๊ธธ์€ ๋‘ ๊ฐœ์˜ ๋–จ์–ด์ง„ ํ—›๊ฐ„์ธย A_i ์™€ย B_i (1 <= A_i <= N; 1 <= B_i <= N; A_i != B_i)๋ฅผ ์ž‡์Šต๋‹ˆ๋‹ค. ๋‘ย ๊ฐœ์˜ ํ—›๊ฐ„์€ ํ•˜๋‚˜ ์ด์ƒ์˜ ๊ธธ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋†๋ถ€ ํ˜„์„œ๋Š” ํ—›๊ฐ„ 1์— ์žˆ๊ณ  ๋†๋ถ€ ์ฐฌํ™์ด๋Š” ํ—›๊ฐ„ N์— ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ์ง€๋„๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”.

       [2]---
      / |    \
     /1 |     \ 6
    /   |      \
 [1]   0|    --[3]
    \   |   /     \2
    4\  |  /4      [6]
      \ | /       /1
       [4]-----[5] 
            3   ๋†๋ถ€ ํ˜„์„œ๊ฐ€ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š”ย ์ตœ์„ ์˜ ํ†ต๋กœ๋Š”ย 1 -> 2 -> 4 -> 5 -> 6 ์ž…๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด ์—ฌ๋ฌผ์˜ ์ดํ•ฉ์ด 1 + 0 + 3 + 1 = 5 ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋†๋ถ€ ํ˜„์„œ์˜ ์ง€๋„๊ฐ€ ์ฃผ์–ด์ง€๊ณ ,ย ์ง€๋‚˜๊ฐ€๋Š” ๊ธธ์— ์†Œ๋ฅผ ๋งŒ๋‚˜๋ฉด ์ค˜์•ผํ•  ์—ฌ๋ฌผ์˜ ๋น„์šฉ์ด ์ฃผ์–ด์งˆ ๋•Œ ์ตœ์†Œ ์—ฌ๋ฌผ์€ ์–ผ๋งˆ์ผ๊นŒ์š”?ย ๋†๋ถ€ ํ˜„์„œ๋Š”ย ๊ฐ€๋Š” ๊ธธ์˜ ๊ธธ์ด๋Š” ๊ณ ๋ คํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.


์ž…๋ ฅ

์ฒซ์งธ ์ค„์— N๊ณผ M์ด ๊ณต๋ฐฑ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ M+1๋ฒˆ์งธ ์ค„๊นŒ์ง€ ์„ธ ๊ฐœ์˜ ์ •์ˆ˜ย A_i, B_i,ย C_i๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.


์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ๋†๋ถ€ ํ˜„์„œ๊ฐ€ ๊ฐ€์ ธ๊ฐ€์•ผ ๋  ์ตœ์†Œ ์—ฌ๋ฌผ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.


์˜ˆ์ œ

์ž…๋ ฅ

6 8
4 5 3
2 4 0
4 1 4
2 1 1
5 6 1
3 6 2
3 2 6
3 4 4


์ถœ๋ ฅ

5


My Sol

import sys
input = sys.stdin.readline
from collections import deque

N, M = map(int, input().split())
rels = [[] for _ in range(N+1)]
minns = [1e8]*(N+1)
minns[1] = 0
Q = deque([1])
for _ in range(M):
    u, v, k = map(int, input().split())
    rels[u].append((v, k))
    rels[v].append((u, k))

while Q:
    u = Q.popleft()
    for v, k in rels[u]:
        if minns[u]+k < minns[v]:
            minns[v] = minns[u]+k
            Q.append(v)

print(minns[N])

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•ด ์ตœ์†Œ ๋น„์šฉ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์˜€์ง€๋งŒ, ํ’€์ด๊ฐ€ ์ƒ๊ฐ์ด ๋‚˜์ง€ ์•Š์•„ ํšจ์œจ์ ์œผ๋กœ ํ’€์ง€๋Š” ๋ชปํ–ˆ๋˜ ๋ฌธ์ œ์˜€๋‹ค. ์ผ๋‹จ ๊ธฐ๋ณธ ์ปจ์…‰์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ’€๊ธด ํ–ˆ์ง€๋งŒ, ๋ฐฉ๋ฌธํ–ˆ๋˜ ๋…ธ๋“œ๋ฅผ ๋„ˆ๋ฌด ์ž์ฃผ ๋ฐฉ๋ฌธํ•˜๋Š” ๊ตฌ์กฐ์ด๊ธฐ ๋•Œ๋ฌธ์— ์—ฐ์‚ฐ์‹œ๊ฐ„์ด ๊ธธ๊ฒŒ ๋‚˜์˜ค๋Š” ํ’€์ด์ด๋‹ค.

  1. ์ž…๋ ฅ์„ ๋ฐ›์•„ ์–‘๋ฐฉํ–ฅ ๋…ธ๋“œ์˜ ๋ฐฐ์—ด์— ๊ฐ€์ค‘์น˜์™€ ํ•จ๊ป˜ ๋„ฃ๋Š”๋‹ค.
  2. ํ•ด๋‹น ๋…ธ๋“œ๊นŒ์ง€์˜ ๊ฐ€์ค‘์น˜ ์ €์žฅ ๋ฐฐ์—ด์ธ minns๋ฅผ 1e8๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. ์ตœ๋Œ“๊ฐ’์€ 5000๋งŒ์ด๋ฉฐ, ํŽธ์˜์ƒ 1์–ต์˜ ํฌ๊ธฐ๋กœ ๊ฐ€์ค‘์น˜๋ฅผ ์ดˆ๊ธฐํ™”ํ–ˆ๋‹ค.
  3. ์‹œ์ž‘๋…ธ๋“œ์ธ 1๋ฒˆ ๋…ธ๋“œ์˜ ๊ฐ€์ค‘์น˜์ธ minns[1]์„ 0์œผ๋กœ ๋‘๊ณ , ํ•ด๋‹น ๋…ธ๋“œ๋ถ€ํ„ฐ ๊ฐ€์ค‘์น˜๋งŒํผ ๋”ํ•ด ์—ฐ๊ฒฐ๋œ ์ด์›ƒ ๋…ธ๋“œ๋“ค์˜ ํ˜„์žฌ ๊ฐ€์ค‘์น˜ ๊ฐ’๊ณผ ๋น„๊ตํ•ด ํ˜„์žฌ ๋…ธ๋“œ์—์„œ ์ถœ๋ฐœํ•˜๋Š” ๊ฒƒ์ด ๋” ํšจ์œจ์ ์ด๋ผ๋ฉด ๊ฐ€์ค‘์น˜ ๋ˆ„์ ๊ฐ’์„ ๊ฐฑ์‹ ํ•˜๊ณ  Q์— ๋„ฃ๋Š”๋‹ค.
  4. ์ด๋ฅผ Q๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ์ง„ํ–‰ํ•œ๋‹ค.


๊ฒฐ๊ณผ

๋งž์•˜์Šต๋‹ˆ๋‹ค!!


๋ชจ๋ฒ”๋‹ต์•ˆ

์ถœ์ฒ˜

import heapq
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
arr = [[]for _ in range(N+1)]
for _ in range(M):
    a, b, cost = map(int, input().split())
    arr[a].append((cost, b))
    arr[b].append((cost, a))


def dijkstra():
    q = []
    heapq.heappush(q, (0, 1))
    total = [sys.maxsize] * (N+1)
    total[1] = 0
    while q:
        cost, node = heapq.heappop(q)
        if node == N:
            return total[node]
        if total[node] < cost:
            continue
        for ncost, nnode in arr[node]:
            if cost+ncost < total[nnode]:
                total[nnode] = cost+ncost
                heapq.heappush(q, (cost+ncost, nnode))

print(dijkstra())

๋‹ค์ต์ŠคํŠธ๋ผ๋ฅผ ์ •์„์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉฐ ์—ฐ์‚ฐ์‹œ๊ฐ„์ด ํ›จ์”ฌ ์งง์€ ํ’€์ด๋ฅผ ๋ถ„์„ํ•ด๋ณด๋ ค ํ•œ๋‹ค. ๋น„๊ฒฐ์€ heap์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€์ค‘์น˜๊ฐ€ ์ž‘์€ ์—ฐ๊ฒฐ๋ถ€๋ถ€ํ„ฐ ์ €์žฅํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

  1. ์ดˆ๊ธฐํ™”์˜ ๊ฐ’์€ sys.maxsize๋ผ๋Š” ๊ฐ’์œผ๋กœ ์„ค์ •ํ•œ๋‹ค.
  2. ๊ฐ€์ค‘์น˜์ธ cost๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœํ•˜๋Š” heapq์— ์ €์žฅํ•œ๋‹ค.
  3. ๊ฐ€์ค‘์น˜๊ฐ€ ์ ์€ ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋ถ€ํ„ฐ heappopํ•˜๋ฉฐ ๋ˆ„์  ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐฑ์‹ ํ•ด๊ฐ„๋‹ค.
  4. ๋…ธ๋“œ๊ฐ€ ์ฒ˜์Œ์œผ๋กœ N์„ ๋ฐ”๋ผ๋ณด๋Š”, ์ฆ‰ ์ตœ์†Œ ๊ฒฝ๋กœ๋กœ๋งŒ ์ด๋™ํ•˜๋ฉฐ N์„ ๋งˆ์ฃผ์นœ ๊ฒฝ์šฐ ๊ฐ€์žฅ ์ ์€ ๊ฐ€์ค‘์น˜ ๋ˆ„์ ๊ฐ’์„ ๊ฐ€์ง€๋ฏ€๋กœ ์ด๋•Œ์˜ total[node]๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ