λ¬Έμ
λ°±μ€ μ¨λΌμΈ μ μ§ - 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 |
π¬ λκΈ