Wormholes POJ - 3259 spfa判断负环
作者:互联网
//判断负环 dist初始化为正无穷 //正环 负无穷 #include<iostream> #include<cstring> #include<queue> #include<algorithm> using namespace std; const int N=1e5,INF=0x3f3f3f3f; int dist[N]; int h[N],e[N],ne[N],w[N],idx; int n,m,z; void add(int a,int b,int c) { e[idx]=b; w[idx]=c; ne[idx]=h[a]; h[a]=idx++; } bool st[N]; int times[N]; bool spfa() { memset(st,0,sizeof st); memset(times,0,sizeof times); queue<int>q; st[1]=1; dist[1]=0; q.push(1); times[1]++; while(q.size()) { int t=q.front(); q.pop(); st[t]=0; for(int i=h[t];~i;i=ne[i]) { int j=e[i]; if(dist[j]>dist[t]+w[i]) { dist[j]=dist[t]+w[i]; if(!st[j]) { st[j]=1; q.push(j); times[j]++; if(times[j]>n) return true; } } } } return false; } int main() { int t,a,b,c; cin>>t; while(t--) { cin>>n>>m>>z; idx=0; memset(h,-1,sizeof h); memset(dist,0x3f,sizeof dist); for(int i=1;i<=m;i++) { int a,b,c; cin>>a>>b>>c; add(a,b,c); add(b,a,c); } for(int i=1;i<=z;i++) { int a,b,c; cin>>a>>b>>c; add(a,b,-c); } if(spfa()) cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0; }
标签:3259,dist,idx,int,memset,st,负环,spfa,times 来源: https://www.cnblogs.com/QingyuYYYYY/p/12235753.html