Code:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxm = 2500 * 2 + 200 + 10;
const int maxn = 500 + 10;
const int INF = 999999;
struct Edge
{
int s;
int e;
int weight;
};
Edge M[maxm];
int dist[maxn];
bool Bellman_Ford( int n, int m, int w )
{
for( int i = 0; i < maxn; i++ )
dist[i] = INF;
dist[0] = 0;
for( int i = 0; i < n; i++ )
{
bool flag = true;
for( int j = 0; j < 2 * m + w; j++ )
{
if( dist[M[j].e] > dist[M[j].s] + M[j].weight )
{
dist[M[j].e] = dist[M[j].s] + M[j].weight;
flag = false;
}
}
if( flag ) break;
}
for( int j = 0; j < 2 * m + w; j++ )
{
if( dist[M[j].e] > dist[M[j].s] + M[j].weight )
return true;
}
return false;
}
int main()
{
int f;
int n;
int m;
int w;
scanf( "%d", &f );
while( f-- )
{
scanf( "%d%d%d", &n, &m, &w );
for( int i = 0; i < m; i++ )
{
scanf( "%d%d%d", &M[i].s, &M[i].e, &M[i].weight );
M[i+m].s = M[i].e;
M[i+m].e = M[i].s;
M[i+m].weight = M[i].weight;
}
for( int i = m * 2; i < m * 2 + w; i++ )
{
scanf( "%d%d%d", &M[i].s, &M[i].e, &M[i].weight );
M[i].weight = -M[i].weight;
}
printf( "%s\n", Bellman_Ford( n, m, w ) ? "YES" : "NO" );
}
return 0;
}