74 lines
1.5 KiB
C
74 lines
1.5 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdbool.h>
|
|
#include <string.h>
|
|
|
|
#define MAX_SIZE 5200
|
|
#define MAX_NODE 501
|
|
|
|
typedef struct {
|
|
int start;
|
|
int end;
|
|
int time;
|
|
} Edge;
|
|
|
|
int main() {
|
|
int T;
|
|
scanf("%d", &T);
|
|
|
|
Edge* edge = (Edge*)malloc(sizeof(Edge)*MAX_SIZE);
|
|
int* distance = (int*)malloc(sizeof(int)*MAX_NODE);
|
|
|
|
while(T--) {
|
|
int N, M, W;
|
|
scanf("%d %d %d",&N, &M, &W);
|
|
|
|
// ROAD
|
|
for(int i=0; i<2*M; i+=2) {
|
|
int S, E, T;
|
|
scanf("%d %d %d",&S, &E, &T);
|
|
|
|
Edge temp1 = {S, E, T};
|
|
Edge temp2 = {E, S, T};
|
|
|
|
edge[i] = temp1;
|
|
edge[i+1] = temp2;
|
|
}
|
|
|
|
// WORM HOLE
|
|
for(int i=0; i<W; i++) {
|
|
int S, E, T;
|
|
scanf("%d %d %d",&S, &E, &T);
|
|
|
|
Edge temp = {S, E, T*(-1)};
|
|
|
|
edge[2*M+i] = temp;
|
|
}
|
|
|
|
memset(distance, 0, sizeof(int)*MAX_NODE);
|
|
bool negative_cycle = false;
|
|
|
|
for(int n=0; n<N; n++) {
|
|
for(int i=0; i<2*M+W; i++) {
|
|
int s = edge[i].start;
|
|
int e = edge[i].end;
|
|
int t = edge[i].time;
|
|
|
|
if(distance[e] > distance[s] + t) {
|
|
distance[e] = distance[s] + t;
|
|
|
|
if(n==N-1) {
|
|
negative_cycle = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
printf("%s\n", negative_cycle ? "YES" : "NO");
|
|
}
|
|
|
|
free(edge);
|
|
free(distance);
|
|
return 0;
|
|
} |