Currency Exchange POJ - 1860 spfa判断正环
作者:互联网
//spfa 判断正环 #include<iostream> #include<queue> #include<cstring> using namespace std; const int N=1e4; const int INF=2e9; int h[N],to[N],ne[N],idx; double r[N],c[N]; int n, m,X; double V; void add(int u,int v,double r1,double c1) { to[idx]=v; r[idx]=r1; c[idx]=c1; ne[idx]=h[u]; h[u]=idx++; } double dist[N]; int times[N]; bool vis[N]; bool spfa(int st) { memset(vis,0,sizeof vis); memset(times,0,sizeof times); for(int i=1;i<=n;i++) dist[i]=-INF; queue<int>q; q.push(st); vis[st]=1; dist[st]=V; while(q.size()) { int u=q.front(); q.pop(); vis[u]=false; for(int i=h[u];~i;i=ne[i]) { int v=to[i]; if(dist[v]<(dist[u]-c[i])*r[i]) { dist[v]=(dist[u]-c[i])*r[i]; if(!vis[v]) { q.push(v); vis[v]=1; if(++times[v]>n) return true; } } } } return false; } int main() { while(cin>>n>>m>>X>>V) { idx=0; memset(h,-1,sizeof h); for(int i=1;i<=m;i++) { int u,v; double r1,c1,r2,c2; cin>>u>>v>>r1>>c1>>r2>>c2; add(u,v,r1,c1); add(v,u,r2,c2); } if(spfa(X)) cout<<"YES"<<endl; else cout<<"NO"<<endl; } }
标签:正环,idx,vis,int,double,Exchange,st,1860,c1 来源: https://www.cnblogs.com/QingyuYYYYY/p/12235624.html