[BOJ][๐ŸŸก3][๋ฐฑ์ค€#01238] ํŒŒํ‹ฐ

์ž‘์„ฑ:    

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

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

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #1238


๋ฌธ์ œ

N๊ฐœ์˜ ์ˆซ์ž๋กœ ๊ตฌ๋ถ„๋œ ๊ฐ๊ฐ์˜ ๋งˆ์„์— ํ•œ ๋ช…์˜ ํ•™์ƒ์ด ์‚ด๊ณ  ์žˆ๋‹ค. ์–ด๋Š ๋‚  ์ด N๋ช…์˜ ํ•™์ƒ์ด X (1 โ‰คย X โ‰ค N)๋ฒˆ ๋งˆ์„์— ๋ชจ์—ฌ์„œ ํŒŒํ‹ฐ๋ฅผ ๋ฒŒ์ด๊ธฐ๋กœ ํ–ˆ๋‹ค. ์ด ๋งˆ์„ ์‚ฌ์ด์—๋Š” ์ด M๊ฐœ์˜ ๋‹จ๋ฐฉํ–ฅ ๋„๋กœ๋“ค์ด ์žˆ๊ณ  i๋ฒˆ์งธ ๊ธธ์„ ์ง€๋‚˜๋Š”๋ฐ Ti(1 โ‰ค Ti โ‰ค 100)์˜ ์‹œ๊ฐ„์„ ์†Œ๋น„ํ•œ๋‹ค. ๊ฐ๊ฐ์˜ ํ•™์ƒ๋“ค์€ ํŒŒํ‹ฐ์— ์ฐธ์„ํ•˜๊ธฐ ์œ„ํ•ด ๊ฑธ์–ด๊ฐ€์„œ ๋‹ค์‹œ ๊ทธ๋“ค์˜ ๋งˆ์„๋กœ ๋Œ์•„์™€์•ผ ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์ด ํ•™์ƒ๋“ค์€ ์›Œ๋‚™ ๊ฒŒ์„๋Ÿฌ์„œ ์ตœ๋‹จ ์‹œ๊ฐ„์— ์˜ค๊ณ  ๊ฐ€๊ธฐ๋ฅผ ์›ํ•œ๋‹ค. ์ด ๋„๋กœ๋“ค์€ ๋‹จ๋ฐฉํ–ฅ์ด๊ธฐ ๋•Œ๋ฌธ์— ์•„๋งˆ ๊ทธ๋“ค์ด ์˜ค๊ณ  ๊ฐ€๋Š” ๊ธธ์ด ๋‹ค๋ฅผ์ง€๋„ ๋ชจ๋ฅธ๋‹ค. N๋ช…์˜ ํ•™์ƒ๋“ค ์ค‘ ์˜ค๊ณ  ๊ฐ€๋Š”๋ฐ ๊ฐ€์žฅ ๋งŽ์€ ์‹œ๊ฐ„์„ ์†Œ๋น„ํ•˜๋Š” ํ•™์ƒ์€ ๋ˆ„๊ตฌ์ผ์ง€ ๊ตฌํ•˜์—ฌ๋ผ.


์ž…๋ ฅ

์ฒซ์งธ ์ค„์— N(1 โ‰คย N โ‰ค 1,000), M(1 โ‰ค M โ‰ค 10,000), X๊ฐ€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์ž…๋ ฅ๋œ๋‹ค. ๋‘ ๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ M+1๋ฒˆ์งธ ์ค„๊นŒ์ง€ i๋ฒˆ์งธ ๋„๋กœ์˜ ์‹œ์ž‘์ , ๋์ , ๊ทธ๋ฆฌ๊ณ  ์ด ๋„๋กœ๋ฅผ ์ง€๋‚˜๋Š”๋ฐ ํ•„์š”ํ•œ ์†Œ์š”์‹œ๊ฐ„ Ti๊ฐ€ ๋“ค์–ด์˜จ๋‹ค. ์‹œ์ž‘์ ๊ณผ ๋์ ์ด ๊ฐ™์€ ๋„๋กœ๋Š” ์—†์œผ๋ฉฐ, ์‹œ์ž‘์ ๊ณผ ํ•œ ๋„์‹œ A์—์„œ ๋‹ค๋ฅธ ๋„์‹œ B๋กœ ๊ฐ€๋Š” ๋„๋กœ์˜ ๊ฐœ์ˆ˜๋Š” ์ตœ๋Œ€ 1๊ฐœ์ด๋‹ค. ๋ชจ๋“  ํ•™์ƒ๋“ค์€ ์ง‘์—์„œ X์— ๊ฐˆ์ˆ˜ ์žˆ๊ณ , X์—์„œ ์ง‘์œผ๋กœ ๋Œ์•„์˜ฌ ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋งŒ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„๋‹ค.


์ถœ๋ ฅ

์ฒซ ๋ฒˆ์งธ ์ค„์— N๋ช…์˜ ํ•™์ƒ๋“ค ์ค‘ ์˜ค๊ณ  ๊ฐ€๋Š”๋ฐ ๊ฐ€์žฅ ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋Š” ํ•™์ƒ์˜ ์†Œ์š”์‹œ๊ฐ„์„ ์ถœ๋ ฅํ•œ๋‹ค.


์˜ˆ์ œ

์ž…๋ ฅ

4 8 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3


์ถœ๋ ฅ

10


My Sol

import sys
input = sys.stdin.readline
from heapq import heappop, heappush

def dijk(S, T):
    visit = [0]*N
    D = [0xffffff]*N
    D[S] = 0
    Q = [(D[S], S)]

    while Q:
        val, u = heappop(Q)
        if visit[u]: continue
        visit[u] = 1

        for v, w in G[u]:
            if not visit[v] and D[v] > D[u]+w:
                D[v] = D[u]+w
                heappush(Q, (D[v], v))
    return D[T]

N, M, X = map(int, input().split())
X = X-1
G = [[] for _ in range(N)]
for _ in range(M):
    s, e, w = map(int, input().split())
    G[s-1].append((e-1, w))

maxDis = 0
for S in range(N):
    maxDis = max(maxDis, dijk(S, X) + dijk(X, S))
print(maxDis)

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ณ ์ฐจ์›์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์ด์—ˆ๋‹ค. ๊ฐ ๋งˆ์„์„ ์‹œ์ž‘์ ์œผ๋กœ ํ•˜๊ณ  ํŒŒํ‹ฐ์žฅ์„ ๋„์ฐฉ์ ์œผ๋กœ ํ•˜๋Š” ๋‹ค์ต์ŠคํŠธ๋ผ์™€, ๋ฐ˜๋Œ€์˜ ๊ฒฝ์šฐ๋ฅผ ๊ฐ๊ฐ ์ตœ์†Œ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•ด ๋”ํ•œ ๊ฐ’์ด ํŒŒํ‹ฐ์žฅ์„ ์˜ค๊ฐ€๋Š” ์ตœ๋‹จ๊ฑฐ๋ฆฌ์ด๋‹ค.

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


๊ฒฐ๊ณผ

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


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

์ถœ์ฒ˜

import sys, heapq


def dijkstra(start, graph):
    min_T = [inf] * (N+1)
    news = [(0, start)]
    min_T[start] = 0

    while news:
        T, now = heapq.heappop(news)
        if T <= min_T[now]:
            for new_village, t in graph[now]:
                new_T = min_T[now] + t
                if new_T < min_T[new_village]:
                    min_T[new_village] = new_T
                    heapq.heappush(news, (new_T, new_village))

    return min_T


N, M, X = map(int, sys.stdin.readline().rstrip().split())
graph = [[] for _ in range(N+1)]
graph_reverse = [[] for _ in range(N+1)]
inf = float('inf')
for _ in range(M):
    s, e, t = map(int, sys.stdin.readline().rstrip().split())
    graph[s].append((e, t))
    graph_reverse[e].append((s, t))

min_T_go = dijkstra(X, graph_reverse)
min_T_back = dijkstra(X, graph)
ans = 0
for i in range(1, N+1):
    T = min_T_go[i] + min_T_back[i]
    if T > ans:
        ans = T
print(ans)

๋‚ด๊ฐ€ 1200ms๋Œ€์˜ ์—ฐ์‚ฐ์‹œ๊ฐ„์ด ๋‚˜์™”๋Š”๋ฐ, 80ms์งœ๋ฆฌ ํ’€์ด์ด๋‹ค. ๊ธธ์€ ๋‹จ๋ฐฉํ–ฅ ๋„๋กœ์ธ๋ฐ, ๊ผญ ์–‘๋ฐฉํ–ฅ์ฒ˜๋Ÿผ ๋‘ ๊ฐœ์˜ 3์ฐจ์› ๋ฐฐ์—ด graph์™€ graph_reverse๋ฅผ ๋งŒ๋“ค์–ด ๊ฐ’์„ ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์‚ฌ๋žŒ๋งˆ๋‹ค ํ•˜๋‚˜ํ•˜๋‚˜ ๊ธธ์ด๋ฅผ ๊ตฌํ•œ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ํŒŒํ‹ฐ์žฅ์„ ๊ธฐ์ค€์œผ๋กœ ๊ฐ ๋งˆ์„ ๋„๋‹ฌํ•˜๋Š” ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•ด์„œ ์˜ค๊ฐ€๋Š” ์‹œ๊ฐ„์„ ์ธ๋ฑ์Šค๋ฅผ ์ด์šฉํ•ด 2๊ฐœ์˜ ๋ฐฐ์—ด์—์„œ ๊ฐ’์„ ๊บผ๋‚ด์˜ค๋Š” ๋ฐฉ์‹์ด๋‹ค.

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

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