[BOJ][๐ŸŸก4][๋ฐฑ์ค€#22865] ๊ฐ€์žฅ ๋จผ ๊ณณ

์ž‘์„ฑ:    

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

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

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #22865


๋ฌธ์ œ

$N$๊ฐœ์˜ ๋•… ์ค‘์—์„œ ํ•œ ๊ณณ์— ์ž์ทจ๋ฅผ ํ•˜๋ ค๊ณ  ์ง‘์„ ์•Œ์•„๋ณด๊ณ  ์žˆ๋‹ค. ์„ธ ๋ช…์˜ ์นœ๊ตฌ $A$, $B$, $C$๊ฐ€ ์žˆ๋Š”๋ฐ ์ด ์นœ๊ตฌ๋“ค์˜ ์‚ด๊ณ  ์žˆ๋Š” ์ง‘์œผ๋กœ๋ถ€ํ„ฐ ๊ฐ€์žฅ ๋จผ ๊ณณ์— ์ง‘์„ ๊ตฌํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ์ด๋•Œ, ๊ฐ€์žฅ ๋จผ ๊ณณ์€ ์„ ํƒํ• ย ์ง‘์—์„œ ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์นœ๊ตฌ์˜ ์ง‘๊นŒ์ง€์˜ย ๊ฑฐ๋ฆฌ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ€์žฅ ๋จผ ๊ณณ์„ ์˜๋ฏธํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, $X$ ์œ„์น˜์— ์žˆ๋Š” ์ง‘์—์„œ ์นœ๊ตฌ $A$, $B$, $C$์˜ ์ง‘๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ๊ฐ 3, 5, 4์ด๋ผ ๊ฐ€์ •ํ•˜๊ณ  $Y$ ์œ„์น˜์— ์žˆ๋Š” ์ง‘์—์„œ ์นœ๊ตฌ $A$, $B$, $C$์˜ ์ง‘๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ๊ฐ 5, 7, 2๋ผ๊ณ  ํ•˜์ž. ์ด๋•Œ, ์นœ๊ตฌ๋“ค์˜ ์ง‘์œผ๋กœ๋ถ€ํ„ฐ ๋•… $X$์™€ ๋•… $Y$ ์ค‘ ๋” ๋จผ ๊ณณ์€ $X$ ์œ„์น˜์— ์žˆ๋Š” ์ง‘์ด ๋œ๋‹ค. ์™œ๋ƒํ•˜๋ฉด $X$์—์„œ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šดย ์นœ๊ตฌ์˜ ์ง‘๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋Š” 3์ด๊ณ , $Y$์—์„œ๋Š” $2$์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์นœ๊ตฌ๋“ค์ด ์‚ด๊ณ  ์žˆ๋Š” ์ง‘์œผ๋กœ๋ถ€ํ„ฐ ๊ฐ€์žฅ ๋จผ ๊ณณ์„ ๊ตฌํ•ด๋ณด์ž.


์ž…๋ ฅ

์ฒซ๋ฒˆ์งธ ์ค„์— ์ž์ทจํ•  ๋•… ํ›„๋ณด์˜ ๊ฐœ์ˆ˜ $N$์ด ์ฃผ์–ด์ง„๋‹ค. 2๋ฒˆ์งธ ์ค„์—๋Š” ์นœ๊ตฌ $A$, $B$, $C$๊ฐ€ ์‚ฌ๋Š” ์œ„์น˜๊ฐ€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์ฃผ์–ด์ง„๋‹ค. ์ด๋•Œ ์นœ๊ตฌ๋“ค์€ $N$๊ฐœ์˜ ๋•… ์ค‘ ํ•˜๋‚˜์— ์‚ฌ๋Š” ๊ฒƒ์„ ๋ณด์žฅํ•œ๋‹ค. (๊ฐ™์€ ์œ„์น˜์—์„œ ์‚ด ์ˆ˜ ์žˆ๋‹ค.) 3๋ฒˆ์งธ ์ค„์—๋Š” ๋•…๊ณผ ๋•… ์‚ฌ์ด๋ฅผ ์ž‡๋Š” ๋„๋กœ์˜ ๊ฐœ์ˆ˜ $M$์ด ์ฃผ์–ด์ง„๋‹ค. ๊ทธ ๋‹ค์Œ์ค„๋ถ€ํ„ฐ $M + 3$๋ฒˆ์งธ ์ค„๊นŒ์ง€ย ๋•… $D$, ๋•… $E$, ๋•… $D$์™€ ๋•… $E$์™€ ์‚ฌ์ด๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๋„๋กœ์˜ ๊ธธ์ด $L$์ด ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ดย ์ฃผ์–ด์ง„๋‹ค. ์ด ๋„๋กœ๋Š” ์–‘๋ฑกํ•ญ ํ†ตํ–‰์ด ๊ฐ€๋Šฅํ•˜๋‹ค.


์ถœ๋ ฅ

์นœ๊ตฌ๋“ค์ด ์‚ด๊ณ  ์žˆ๋Š” ์ง‘์œผ๋กœ๋ถ€ํ„ฐ ๊ฐ€์žฅ ๋จผ ๊ณณ์˜ ๋•… ๋ฒˆํ˜ธ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๋งŒ์•ฝ ๊ฐ€์žฅ ๋จผ ๊ณณ์ด ์—ฌ๋Ÿฌ ๊ณณ์ด๋ผ๋ฉด ๋ฒˆํ˜ธ๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ๋•…์˜ ๋ฒˆํ˜ธ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.


์ œํ•œ

$ 1 โ‰ค N โ‰ค 100,000$ $N - 1 โ‰ค M โ‰ค 500,000$ $1 โ‰ค A, B, C, D, E โ‰ค N$ $1 โ‰ค L โ‰ค 10,000$


์˜ˆ์ œ

์ž…๋ ฅ

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


์ถœ๋ ฅ

3


My Sol

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

def main():
    def make_rels():
        rels = [[] for _ in range(N+1)]
        M = int(input())
        for _ in range(M):
            u, v, w = map(int, input().split())
            rels[u].append((v, w))
            rels[v].append((u, w))
        return rels

    def find_max_len():
        mi, mv = 0, 0
        Ds = [find_max_len_sub(f) for f in friends]
        for i in range(1, N+1):
            smv = 1e9
            for j in range(3):
                smv = min(smv, Ds[j][i])
            if Ds[j][i] == 1e9: continue
            if smv > mv:
                mi, mv = i, smv
        return mi


    def find_max_len_sub(n):
        visited = [0]*(N+1)
        D = [1e9 for _ in range(N+1)]
        D[n] = 0
        H = [(0, n)]
        while H:
            d, u = heappop(H)
            if visited[u]: continue
            visited[u] = 1
            for v, w in rels[u]:
                if visited[v]: continue
                if D[v] > D[u]+w:
                    D[v] = D[u]+w
                    heappush(H, (D[v], v))
        return D

    N = int(input())
    friends = set(map(int, input().split()))
    rels = make_rels()
    return find_max_len()

print(main())

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฌธ์ œ์˜€๋‹ค.

  1. ์ž…๋ ฅ์„ ๋ฐ›์•„ ๋…ธ๋“œ๊ฐ„ ์—ฃ์ง€๋ฅผ ์ €์žฅํ•œ๋‹ค. make_rels๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์„œ ๊ตฌํ˜„ํ–ˆ๋‹ค.
  2. 3๋ช…์˜ ์นœ๊ตฌ๋“ค์˜ ์œ„์น˜๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ ์ „์ฒด ๋…ธ๋“œ์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•œ๋‹ค. find_max_len์ด๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ–ˆ๋Š”๋ฐ, ์ด๋Š” find_max_len_sub๋ผ๋Š” ํ•˜์œ„ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด friends์˜ ๊ฐ ์‹œ์ž‘์ ์œผ๋กœ๋ถ€ํ„ฐ์˜ ๋ชจ๋“  ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ๋ฐ›์•„, ๋ชจ๋“  ์ •์ ์—์„œ ๊ฑฐ๋ฆฌ๋ฅผ ์ฒดํฌํ•˜์—ฌ ๊ฐ€์žฅ ๋จผ ๊ณณ์˜ ๋…ธ๋“œ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.
  3. find_max_len_sub ํ•จ์ˆ˜๋Š” ์ธ์ž๋กœ ๋ฐ›์€ n์œผ๋กœ๋ถ€ํ„ฐ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‹คํ–‰ํ•˜์—ฌ ๊ฐ ๋…ธ๋“œ๋งˆ๋‹ค ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ์ €์žฅํ•œ๋‹ค.
  4. main ํ•จ์ˆ˜๋Š” find_max_len ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , ์ด๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ๋˜๊ฒ ๋‹ค.


๊ฒฐ๊ณผ

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

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