https://www.acmicpc.net/problem/1865
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.StringTokenizer;
public class Main {
static StringBuilder sb = new StringBuilder();
static Reader scanner = new Reader();
static final int INF = 30000000;
static int N, M, W;
static Edge[] edges;
static int[] weight;
static void input() {
N = scanner.nextInt();
M = scanner.nextInt();
W = scanner.nextInt();
edges = new Edge[2 * M + W];
for(int road = 0; road < M; road++) {
int l1 = scanner.nextInt(), l2 = scanner.nextInt(), weight = scanner.nextInt();
edges[2 * road] = new Edge(l1, l2, weight);
edges[2 * road + 1] = new Edge(l2, l1, weight);
}
for(int worm = 2 * M; worm < 2 * M + W; worm++) {
int start = scanner.nextInt(), end = scanner.nextInt(), weight = scanner.nextInt();
edges[worm] = new Edge(start, end, weight * (-1));
}
}
static void solution() {
boolean isCycle = false;
for(int loc = 1; loc <= N; loc++) {
if(bellmanFord(loc)) {
isCycle = true;
sb.append("YES").append('\n');
break;
}
}
if(!isCycle) sb.append("NO").append('\n');
}
static boolean bellmanFord(int start) {
weight = new int[N + 1];
Arrays.fill(weight, INF);
weight[start] = 0;
boolean isChanged = false;
loop:
for(int loc = 1; loc <= N; loc++) {
isChanged = false;
for(Edge e : edges) {
if(weight[e.start] == INF) continue;
if(weight[e.end] > weight[e.start] + e.weight) {
weight[e.end] = weight[e.start] + e.weight;
isChanged = true;
if(loc == N) {
isChanged = true;
break loop;
}
}
}
if(!isChanged) break;
}
return isChanged;
}
static class Edge {
int start, end, weight;
public Edge(int start, int end, int weight) {
this.start = start;
this.end = end;
this.weight = weight;
}
}
public static void main(String[] args) {
int TC = scanner.nextInt();
while(TC-- > 0) {
input();
solution();
}
System.out.println(sb.toString());
}
static class Reader {
BufferedReader br;
StringTokenizer st;
public Reader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch(IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
}
}