1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 6 const int INF = 0x3f3f3f3f; 7 const int maxn = 50005; 8 int n, w, m, k, dis[maxn]; 9 10 struct node 11 { 12 int u, v, w; 13 node() {} 14 node(int uu, int vv, int ww): u(uu), v(vv), w(ww) {} 15 }edge[maxn]; 16 17 bool Bellman_Ford(int s) 18 { 19 for(int i = 1; i <= n; i++) dis[i] = INF; 20 dis[s] = 0; 21 for(int i = 0; i < n; i++) 22 { 23 int flag = 1; 24 for(int j = 0; j < k; j++) 25 { 26 int u = edge[j].u; 27 int v = edge[j].v; 28 int w = edge[j].w; 29 if(dis[u] + w < dis[v]) 30 { 31 flag = 0; 32 dis[v] = dis[u]+w; 33 if(i == n-1) 34 return true; 35 //如果循环n-1次还能缩,就存在负环 36 } 37 } 38 if(flag) return false; 39 } 40 return false; 41 } 42 43 int main() 44 { 45 ios::sync_with_stdio(false); 46 cin.tie(0); 47 48 int t; 49 cin >> t; 50 while(t--) 51 { 52 k = 0; 53 scanf("%d%d%d", &n, &m, &w); 54 for(int i = 1; i <= m+w; i++) 55 { 56 int u, v, w; 57 scanf("%d%d%d", &u, &v, &w); 58 if(i <= m) 59 { 60 edge[k++] = node(u, v, w); 61 edge[k++] = node(v, u, w); 62 } 63 else edge[k++] = node(u, v, -w); 64 } 65 if(Bellman_Ford(1)){ 66 cout << "YES" << endl; 67 } 68 else 69 cout << "NO" << endl; 70 } 71 return 0; 72 }