[BOJ][๐ŸŸก4][๋ฐฑ์ค€#01504] ํŠน์ •ํ•œ ์ตœ๋‹จ ๊ฒฝ๋กœ

์ž‘์„ฑ:    

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

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

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #1504


๋ฌธ์ œ

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


์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ N๊ณผ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ E๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (2 โ‰ค N โ‰ค 800, 0 โ‰ค E โ‰ค 200,000) ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ E๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ์„œ ์„ธ ๊ฐœ์˜ ์ •์ˆ˜ a, b, c๊ฐ€ ์ฃผ์–ด์ง€๋Š”๋ฐ, a๋ฒˆ ์ •์ ์—์„œ b๋ฒˆ ์ •์ ๊นŒ์ง€ ์–‘๋ฐฉํ–ฅ ๊ธธ์ด ์กด์žฌํ•˜๋ฉฐ, ๊ทธ ๊ฑฐ๋ฆฌ๊ฐ€ c๋ผ๋Š” ๋œป์ด๋‹ค. (1 โ‰ค c โ‰ค 1,000) ๋‹ค์Œ ์ค„์—๋Š” ๋ฐ˜๋“œ์‹œ ๊ฑฐ์ณ์•ผ ํ•˜๋Š” ๋‘ ๊ฐœ์˜ ์„œ๋กœ ๋‹ค๋ฅธ ์ •์  ๋ฒˆํ˜ธ v1๊ณผ v2๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (v1 โ‰  v2, v1 โ‰  N,ย v2 โ‰  1) ์ž„์˜์˜ ๋‘ ์ •์  u์™€ v์‚ฌ์ด์—๋Š” ๊ฐ„์„ ์ด ์ตœ๋Œ€ 1๊ฐœ ์กด์žฌํ•œ๋‹ค.


์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ๋‘ ๊ฐœ์˜ ์ •์ ์„ ์ง€๋‚˜๋Š” ์ตœ๋‹จ ๊ฒฝ๋กœ์˜ ๊ธธ์ด๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๊ทธ๋Ÿฌํ•œ ๊ฒฝ๋กœ๊ฐ€ ์—†์„ ๋•Œ์—๋Š” -1์„ ์ถœ๋ ฅํ•œ๋‹ค.


์˜ˆ์ œ

์ž…๋ ฅ

4 6
1 2 3
2 3 3
3 4 1
1 3 5
2 4 5
1 4 4
2 3


์ถœ๋ ฅ

7


My Sol

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

def dijk(S, E):
    global G

    visit = [0]*(V+1)
    D = [inf]*(V+1)
    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 -1 if D[E] == inf else D[E]


def main(S, A, B, T):
    R1 = dijk(S, A)
    if R1==-1: return -1
    R2 = dijk(A, B)
    if R2==-1: return -1
    R3 = dijk(B, T)
    if R3==-1: return -1
    return R1+R2+R3


V, E = map(int, input().split())
G = [[] for _ in range(V+1)]
for _ in range(E):
    u, v, w = map(int, input().split())
    G[u].append((v, w))
    G[v].append((u, w))

C1, C2 = map(int, input().split())
ret1 = main(1, C1, C2, V)
ret2 = main(1, C2, C1, V)
ret3 = {ret1, ret2} - {-1}
print(min(ret3) if ret3 else -1)

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋ณ€ํ˜•ํ•œ ๋ฐฉ์‹์ด๋‹ค. ๋ฐ˜๋“œ์‹œ ์ง€๋‚˜์•ผํ•˜๋Š” ๋‘ ์ •์ ์„ C1๊ณผ C2๋กœ ์ €์žฅํ•˜๊ณ , ์ด 3๋ฒˆ์˜ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ ์šฉํ•˜์—ฌ 1๋ฒˆ ์ •์ ๋ถ€ํ„ฐ V๋ฒˆ ์ •์ ๊นŒ์ง€ ๋„๋‹ฌํ•˜๋Š” ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ํ™•์ธํ•œ๋‹ค. ์ด๋•Œ C2๋ฅผ ๋จผ์ € ๋ฐฉ๋ฌธํ•  ๊ฒƒ์ธ์ง€, C1์„ ๋จผ์ € ๋ฐฉ๋ฌธํ•  ๊ฒƒ์ธ์ง€๋ฅผ ์™„์ „ ํƒ์ƒ‰์„ ํ†ตํ•ด ํ™•์ธํ•˜์—ฌ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

์ด๋•Œ ๋‹ค์ต์ŠคํŠธ๋ผ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ์ผ์ • ๊ตฌ๊ฐ„์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ์˜ ๊ฐ’์„ ๊ตฌํ–ˆ๋Š”๋ฐ, ์ด ๊ฐ’์ด -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋ฉด ๋‘ ์ •์ ์ด ์ด์–ด์ง€์ง€ ์•Š์€ ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋‘ ๊ฐ€์ง€ ๊ฒฝ์šฐ ๋ชจ๋‘ C1๊ณผ C2๋Š” ์ง€๋‚˜๋ฏ€๋กœ, ์ด๋ฅผ ๋จผ์ € ํ™•์ธํ•˜์—ฌ ๋ฐฑํŠธ๋ž˜ํ‚น์ฒ˜๋Ÿผ ๊ฒฝ์šฐ๋ฅผ ์ž˜๋ผ์คฌ์–ด๋„ ๋๊ฒ ๋‹ค. ๋ถˆํ•„์š”ํ•œ ๊ฒฝ๋กœ ๊ณ„์‚ฐ์„ ํ•œ ๋ฒˆ ๋” ํ•œ ๊ฒƒ ๊ฐ™๋‹ค.


๊ฒฐ๊ณผ

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

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