1.wormhole
1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<stack> 5 #include<cstring> 6 #include<algorithm> 7 8 using namespace std; 9 #define INF 99999999 10 #define N 501 11 int g[N][N]; 12 int d[N]; 13 int n,m,w; 14 15 bool BELLMAN_FORD() 16 { 17 for (int i=0;i<n;i++) 18 d[i]=INF; 19 d[0]=0; 20 for (int k=0;k<n-1;k++) 21 for (int i=0;i<n;i++) 22 for (int j=0;j<n;j++) 23 if ((g[i][j]!=INF)&&(d[j]>d[i]+g[i][j])) 24 d[j]=d[i]+g[i][j]; 25 for (int i=0;i<n;i++) 26 for (int j=0;j<n;j++) 27 if ((g[i][j]!=INF)&&(d[j]>d[i]+g[i][j])) return false; 28 return true; 29 } 30 int main() 31 { 32 int _count; 33 scanf("%d",&_count); 34 for (int q=0;q<_count;q++) 35 { 36 scanf("%d%d%d",&n,&m,&w); 37 for (int i=0;i<n;i++) 38 for (int j=0;j<n;j++) 39 g[i][j]=INF; 40 int u,v,t; 41 for (int i=0;i<m;i++) 42 { 43 scanf("%d%d%d",&u,&v,&t); 44 if (g[u-1][v-1]>t) g[u-1][v-1]=t; 45 if (g[v-1][u-1]>t) g[v-1][u-1]=t; 46 } 47 for (int i=0;i<w;i++) 48 { 49 scanf("%d%d%d",&u,&v,&t); 50 g[u-1][v-1]=-t; 51 } 52 if (BELLMAN_FORD()) printf("NO\n"); 53 else printf("YES\n"); 54 } 55 return 0; 56 }
时间: 2024-10-20 16:25:25