-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1865.py
38 lines (30 loc) · 883 Bytes
/
1865.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 웜홀
import sys
input = sys.stdin.readline
def belmanford(start):
min_time[start] = 0
for i in range(1, N + 1):
for s in range(1, N + 1):
for next, T in graph[s]:
if min_time[next] > min_time[s] + T:
min_time[next] = min_time[s] + T
if i == N:
return True
return False
TC = int(input())
for _ in range(TC):
N, M, W = map(int, input().split())
graph = [[] for _ in range(N + 1)]
min_time = [10001 for _ in range(N + 1)]
for __ in range(M):
S, E, T = map(int, input().split())
graph[S].append([E, T])
graph[E].append([S, T])
for __ in range(W):
S, E, T = map(int, input().split())
graph[S].append([E, -T])
cycle = belmanford(1)
if not cycle:
print("NO")
else:
print("YES")