-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1865.py
35 lines (29 loc) · 821 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
import sys
input = sys.stdin.readline
tc = int(input())
INF = int(1e9)
def bellmanford(s):
graph[s] = 0
for re in range(1, n + 1):
for i in range(1, n + 1):
for next, t in edge[i]:
if graph[next] > graph[i] + t:
graph[next] = graph[i] + t
if re == n:
return True
return False
for _ in range(tc):
n, m, w = map(int, input().split())
graph = [INF] * (n + 1)
edge = [[] for _ in range(n + 1)]
for _ in range(m):
S, E, T = map(int, input().split())
edge[S].append([E, T])
edge[E].append([S, T])
for _ in range(w):
S, E, T = map(int, input().split())
edge[S].append([E, -T])
if bellmanford(1):
print("YES")
else:
print("NO")