判断是否有负环。。。Bellman_ford
#include <algorithm> #include <cstring> #include <cstdio> #define N 2200 #define INF 9999999 int v[N],w[N],u[N]; int d[N]; int n,m; bool Bellman_ford() { for(int i=0; i<n; ++i) d[i]=INF; d[0]=0; for(int k=0; k<n-1; ++k) { for(int i=0; i<m; ++i) { int x=u[i],y=v[i]; if(d[x]<INF) { d[y]=d[y]<(d[x]+w[i])?d[y]:(d[x]+w[i]);printf("%d=%d,%d=%d\n",x,d[x],y,d[y]); } } } for(int i=0; i<m; ++i) { int x=u[i],y=v[i]; if(d[y]>d[x]+w[i]) { printf("%d %d %d %d %d %d\n",x,y,d[y],d[x]+w[i],d[x],w[i]); return true; } } return false; } int main() { int t; scanf("%d",&t); while(t--) { scanf("%d %d",&n,&m); for(int i=0; i<m; ++i) { scanf("%d %d %d",&u[i],&v[i],&w[i]); } if(Bellman_ford()) puts("possible"); else puts("not possible"); } return 0; }