λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
πŸƒ algorithm/boj

BOJ 13905 - μ„ΈλΆ€

by HandHand 2021. 3. 8.

문제

λ°±μ€€ 온라인 저지 - 13905번

풀이 κ³Όμ •

μ‹œμž‘ μ§€μ μ—μ„œ λͺ©ν‘œ μ§€μ κΉŒμ§€ 이동할 λ•Œ ν•„μš”ν•œ κ°„μ„ μ˜ μ΅œμ†Œ 무게λ₯Ό κ΅¬ν•˜λŠ” λ¬Έμ œμž…λ‹ˆλ‹€.

μ€‘λŸ‰μ œν•œ λ¬Έμ œμ™€ 같은 μœ ν˜•μ΄λ©° BFS + 이뢄 탐색 으둜 ν•΄κ²°ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
이뢄 탐색 을 μ΄μš©ν•΄ μ œν•œ 무게λ₯Ό μ •ν•˜κ³  ν•΄λ‹Ή 무게둜 λͺ©ν‘œ 지점에 도달할 수 μžˆλŠ”μ§€ BFS 둜 νŒλ‹¨ν•˜λ©΄ λ©λ‹ˆλ‹€.

μ½”λ“œ


import sys
from collections import deque

N, M = list(map(int, sys.stdin.readline().split()))
S, E = list(map(int, sys.stdin.readline().split()))
bridges = [list(map(int, sys.stdin.readline().split())) for _ in range(M)]


def make_graph():
    graph = [[] for _ in range(N + 1)]

    for frm, to, cost in bridges:
        graph[frm].append([to, cost])
        graph[to].append([frm, cost])

    return graph


def bfs(min_weight, graph):
    q = deque()
    visit = [0] * (N + 1)

    visit[S] = 1
    q.append(S)

    while q:
        here = q.popleft()

        if here == E:
            return True

        for there, cost in graph[here]:
            if min_weight <= cost and not visit[there]:
                visit[there] = 1
                q.append(there)

    return False


def solution():
    graph = make_graph()

    answer = 0
    lo, hi = 1, 1000000

    while lo <= hi:
        mid = (lo + hi) // 2
        is_possible = bfs(mid, graph)

        if is_possible:
            answer = mid
            lo = mid + 1
        else:
            hi = mid - 1

    return answer


print(solution())
λ°˜μ‘ν˜•

'πŸƒ algorithm > boj' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

BOJ 1802 - 쒅이 μ ‘κΈ°  (0) 2021.03.08
BOJ 17829 - 222-풀링  (0) 2021.03.08
BOJ 2458 - ν‚€ μˆœμ„œ  (0) 2021.03.08
BOJ 18111 - λ§ˆμΈν¬λž˜ν”„νŠΈ  (0) 2021.03.08
BOJ 1806 - λΆ€λΆ„ν•©  (0) 2021.03.08

πŸ’¬ λŒ“κΈ€