POJ 3259
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#define MAXN 10005
#define MAXM 10005
#define MOD 10000
#define inf 1500000000
#define eps 1e-9
#define pi acos(-1.0)
typedef long long LL;
using namespace std;
int T, n, m, w, s, e, t, dis[505];
struct Edge {
int u, v, w, next;
} edge[MAXN];
int es;
void add(int u, int v, int w) {
edge[es].u = u, edge[es].v = v, edge[es].w = w;
}
bool Bellman_Ford() {
for (int i = 1; i <= n; i++) dis[i] = inf;
dis[1] = 0;
bool flag;
for (int i = 1; i < n; i++) {
flag = false;
for (int j = 0; j < es; j++) {
int u = edge[j].u, v = edge[j].v, w = edge[j].w;
if (dis[u] + w < dis[v])
dis[v] = dis[u] + w;
flag = true;
}
if (!flag) return false;
}
for (int i = 0; i < es; i++) {
int u = edge[i].u, v = edge[i].v, w = edge[i].w;
if (dis[u] + w < dis[v]) {
return true;
}
}
return false;
}
int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
scanf("%d", &T);
while (T--) {
scanf("%d %d %d", &n, &m, &w);
es = 0;
for (int i = 0; i < m; i++) {
scanf("%d %d %d", &s, &e, &t);
add(s, e, t); es++;
add(e, s, t); es++;
}
for (int i = 0; i < w; i++) {
scanf("%d %d %d", &s, &e, &t);
add(s, e, -t); es++;
}
if (Bellman_Ford()) puts("YES");
else puts("NO");
}
return 0;
}