728x90
λ¬Έμ
νμ΄
λ²¨λ§ ν¬λ μκ³ λ¦¬μ¦μ μ¬μ©ν΄μ νλ©΄ λλ€.
λ¨, μΌλ°μ μΈ λ²¨λ§ ν¬λ μκ³ λ¦¬μ¦μ΄ μλ "μμ κ°μ μ¬μ΄ν΄"μ΄ μ‘΄μ¬νλμ§ μ¬λΆλ§ ꡬνλ©΄ λλ€.
λν "λλ‘"λ μλ°©ν₯μ΄κ³ "μν"μ λ¨λ°©ν₯μμ μ£Όμν΄μ μ λ ₯λ°μμΌ νλ€.
https://www.youtube.com/watch?v=Ppimbaxm8d8&list=PLRx0vPvlEmdAghTr5mXQxGpHjWqSz0dgC&index=13
μ½λ
from sys import stdin
input = stdin.readline
def bf():
for i in range(n):
for j in range(len(edges)):
cur, next, cost = edges[j]
if dist[next] > dist[cur] + cost:
dist[next] = dist[cur] + cost
if i == n - 1:
return True
return False
TC = int(input())
for _ in range(TC):
n, m, w = map(int, input().split())
edges = []
dist = [1e9] * (n + 1)
for i in range(m + w):
s, e, t = map(int, input().split())
if i >= m:
t = -t
else:
edges.append((e, s, t))
edges.append((s, e, t))
if bf():
print("YES")
else:
print("NO")
λ§λ¬΄λ¦¬
λ€μ΅μ€νΈλΌ μκ³ λ¦¬μ¦κ³Ό μ μ¬ν μ μ΄ λ§μ μ½κ² νμλ€.
728x90