ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [๋ฐฑ์ค€] 1967 ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„ with Python
    PS 2021. 10. 11. 23:34
    728x90
    ๋ฐ˜์‘ํ˜•

    ๐Ÿ“Œ BOJ 1967 ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„

    ๐Ÿ’ก ์กฐ๊ฑด ๋ฐ ํ’€์ด

    1. ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜ (1 โ‰ค n โ‰ค 10,000)
    2. ์ฒซ ๋ฒˆ์งธ ์ •์ˆ˜๋Š” ๊ฐ„์„ ์ด ์—ฐ๊ฒฐํ•˜๋Š” ๋‘ ๋…ธ๋“œ ์ค‘ ๋ถ€๋ชจ ๋…ธ๋“œ์˜ ๋ฒˆํ˜ธ
      ๋‘ ๋ฒˆ์งธ ์ •์ˆ˜๋Š” ์ž์‹ ๋…ธ๋“œ
      ์„ธ ๋ฒˆ์งธ ์ •์ˆ˜๋Š” ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜
    3. ๋ถ€๋ชจ ๋…ธ๋“œ์˜ ๋ฒˆํ˜ธ๊ฐ€ ์ž‘์€ ๊ฒƒ์ด ๋จผ์ € ์ž…๋ ฅ๋˜๊ณ ,
      ๋ถ€๋ชจ ๋…ธ๋“œ์˜ ๋ฒˆํ˜ธ๊ฐ€ ๊ฐ™์œผ๋ฉด ์ž์‹ ๋…ธ๋“œ์˜ ๋ฒˆํ˜ธ๊ฐ€ ์ž‘์€ ๊ฒƒ์ด ๋จผ์ € ์ž…๋ ฅ๋œ๋‹ค.
    4. BFS ์œ ํ˜•์˜ ๋ฌธ์ œ
    5. ๋ฃจํŠธ ๋…ธ๋“œ์˜ ๋ฒˆํ˜ธ๋Š” ํ•ญ์ƒ 1
      ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๋Š” 100๋ณด๋‹ค ํฌ์ง€ ์•Š์€ ์–‘์˜ ์ •์ˆ˜
    6. ํŠธ๋ฆฌ์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ๊ฒฝ๋กœ๋“ค ์ค‘, ๊ฐ€์žฅ ๊ธด ๊ฒฝ๋กœ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

    ๐Ÿ–ฅ ์†Œ์Šค ์ฝ”๋“œ

    from sys import stdin
    from collections import deque
    
    n = int(stdin.readline())
    tree = [[] for _ in range(n + 1)]
    for _ in range(n - 1):
        a, b, c = map(int, stdin.readline().split())
        tree[a].append((b, c))
        tree[b].append((a, c))
    
    
    def bfs(i):
        visited = set()
        q = deque()
        q.append((i, 0))
        visited.add(i)
        res = (0, 0)
    
        while q:
            now, cost = q.popleft()
            for n, c in tree[now]:
                if n not in visited:
                    visited.add(n)
                    t = c + cost
                    q.append((n, t))
    
                    if res[1] < t:
                        res = (n, t)
    
        return res
    
    
    a = bfs(1)
    b = bfs(a[0])
    print(b[1])

    ๐Ÿ”– ์˜ˆ์ œ ๋ฐ ์‹คํ–‰๊ฒฐ๊ณผ

    ์˜ˆ์ œ

    12
    1 2 3
    1 3 2
    2 4 5
    3 5 11
    3 6 9
    4 7 1
    4 8 7
    5 9 15
    5 10 4
    6 11 6
    6 12 10

    ์‹คํ–‰๊ฒฐ๊ณผ

    45

    โŒจ๏ธ ๋ฌธ์ œ ํ’€์ด

    1. BFS ๋ฌธ์ œ ์˜€์ง€๋งŒ, ํŠธ๋ฆฌ์˜ ๊ธธ์ด๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ๋ผ๊ณ  ํ•˜๊ธฐ์— ๋‹นํ™ฉ์„ ํ–ˆ๋‹ค.
    2. BFS ๋ฅผ ํ†ตํ•ด ๋ฃจํŠธ ๋…ธ๋“œ์ธ 1 ๋ถ€ํ„ฐ ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•ด์„œ ๊ฐ€์ค‘์น˜๊ฐ€ ๊ฐ€์žฅ ๋†’์€ ๊ฒƒ์„
      ๋ฐ˜ํ™˜๋ฐ›์•„ ๋ณ€์ˆ˜์— ๋‹ด๋Š”๋‹ค.
    3. ๋ณ€์ˆ˜์—๋Š” (node, cost) ํ˜•์‹์œผ๋กœ ์žˆ๋Š”๋ฐ, ์ด node ๋ฒˆํ˜ธ๋ฅผ ๋‹ค์‹œ BFS์— ๋„ฃ์–ด ์ฒ˜๋ฆฌํ•œ๋‹ค.
    4. 3๋ฒˆ์„ ํ†ตํ•ด์„œ ๋ฐ˜ํ™˜ ๋œ (node, cost) ์—์„œ cost๊ฐ€ ๋‹ต์ด ๋œ๋‹ค.
    5. ๋‹ค์‹œ ์„ค๋ช…ํ•˜์ž๋ฉด,
      2๋ฒˆ์—์„œ BFS ๋กœ ๊ฐ€์žฅ ๊ฐ€์ค‘์น˜๊ฐ€ ๋งŽ์ด ์Œ“์ด๋Š” ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค. ์ด๋ฅผ A-node ๋ผ๊ณ  ํ•˜๊ณ ,
      3๋ฒˆ์—์„œ A-node์—์„œ๋ถ€ํ„ฐ ๊ฐ€์žฅ ๊ฐ€์ค‘์น˜๊ฐ€ ๋งŽ์ด ์Œ“์ด๋Š” ๊ณณ์œผ๋กœ ์ˆœํšŒ๋ฅผ ํ•˜์—ฌ B-node๋ผ๊ณ  ํ•œ๋‹ค.
      ๋ฌธ์ œ์—์„œ ์„ค๋ช…ํ•˜๋“ฏ, ์ด ๋‘ ๋…ธ๋“œ๊ฐ€ ๊ฐ€์žฅ ๋†’์€ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฒฝ๋กœ ์ฆ‰, ์ง€๋ฆ„์ด ๋œ๋‹ค.

    ๐Ÿ’พ ๋Š๋‚€์ 

    • BFS๋ฅผ ์‘์šฉํ•˜์—ฌ ํ‘ธ๋Š” ๋ฌธ์ œ์˜€๋‹ค. ๋‘๋ฒˆ์ด๋‚˜ ๋Œ๋ฆด ์ƒ๊ฐ์„ ๋ชปํ•ด์„œ ํ—ค๋งธ๋‹ค.
    • ๋ฌธ์ œ๋ฅผ ํ•ด์„ํ•˜๊ณ  ์••์ถ•ํ•˜๋Š” ๋Šฅ๋ ฅ์„ ๋” ํ‚ค์šฐ๊ณ , ์ƒ๊ฐ์˜ ์ „ํ™˜์„ ํ•˜๋Š” ์Šต๊ด€์„ ๊ธธ๋Ÿฌ์•ผ๊ฒ ๋‹ค.
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.