http://poj.org/problem?id=3259
1 #include<stdio.h>
2 #include<string.h>
3 #define maxn 5505
4 #define INF 9999999
5 typedef struct
6 {
7 int x,y,w;
8 }node;
9 node arr[maxn];
10 int dist[maxn];
11 int t,n,m,w,cnt;
12 int bellman_ford()
13 {
14 for(int i=1;i<=cnt;i++)
15 dist[i]=INF;
16 for(int i=1;i<n;i++)
17 {
18 int ok=0;
19 for(int j=1;j<=cnt;j++)
20 {
21 int u=arr[j].x;
22 int v=arr[j].y;
23 int z=arr[j].w;
24 if(dist[v]>dist[u]+z)
25 {
26 dist[v]=dist[u]+z;
27 ok=1;
28 }
29 }
30 if(!ok) break;
31 }
32 for(int j=1;j<=cnt;j++)
33 {
34 int u=arr[j].x;
35 int v=arr[j].y;
36 int z=arr[j].w;
37 if(dist[v]>dist[u]+z)
38 return 1;
39 }
40 return 0;
41 }
42 int main()
43 {
44 //freopen("in.txt","r",stdin);
45 scanf("%d",&t);
46 while(t--)
47 {
48 scanf("%d%d%d",&n,&m,&w);
49 int a,b,c;
50 cnt=0;
51 for(int i=1;i<=m;i++)
52 {
53 scanf("%d%d%d",&a,&b,&c);
54 arr[++cnt].x=a;
55 arr[cnt].y=b;
56 arr[cnt].w=c;
57 arr[++cnt].x=b;
58 arr[cnt].y=a;
59 arr[cnt].w=c;
60 }
61 for(int i=1;i<=w;i++)
62 {
63 scanf("%d%d%d",&a,&b,&c);
64 arr[++cnt].x=a;
65 arr[cnt].y=b;
66 arr[cnt].w=-c;
67 }
68 //printf("%d
",cnt);
69 if(bellman_ford())
70 printf("YES
");
71 else
72 printf("NO
");
73 }
74 return 0;
75 }