这题和poj 1860很像,不过那个是求上升回路,这题是求下降回路。
1 #include <stdio.h> 2 #include <string.h> 3 int u[5400],v[5400],w[5400]; 4 int d[505]; 5 int n,m1,m2; 6 bool Bellman() 7 { 8 int i,j; 9 memset(d,0x3f,sizeof(d)); 10 d[1] = 0; 11 for(j = 1; j < n; j++) 12 { 13 bool ok = 1; 14 for(i = 0; i < 2*m1+m2; i++) 15 if(d[u[i]] + w[i] < d[v[i]]) 16 { 17 d[v[i]] = d[u[i]] + w[i]; 18 ok = 0; 19 } 20 if(ok) break; 21 } 22 for(i = 0; i < 2*m1+m2; i++) 23 if(d[u[i]] + w[i] < d[v[i]]) 24 return 1; 25 return 0; 26 } 27 int main() 28 { 29 int F,i; 30 scanf("%d",&F); 31 while(F--) 32 { 33 scanf("%d%d%d",&n,&m1,&m2); 34 for(i = 0; i < 2*m1; i += 2) 35 { 36 scanf("%d%d%d",&u[i],&v[i],&w[i]); 37 u[i+1] = v[i]; 38 v[i+1] = u[i]; 39 w[i+1] = w[i]; 40 } 41 for(i = 2*m1; i < 2*m1+m2; i++) 42 { 43 scanf("%d%d%d",&u[i],&v[i],&w[i]); 44 w[i] = -w[i]; 45 } 46 if(Bellman()) 47 printf("YES\n"); 48 else printf("NO\n"); 49 } 50 return 0; 51 }