标签:fir cstring names cst stream ace ios pac pre
#include<iostream> #include<algorithm> #include<cstring> #include<queue> using namespace std; struct node { int x; int num; }; const int inf = 99999999; int u[415],v[415],w[415],r[415]; int first[415],Next[415]; int num[415]; bool vis[415]; int n,m; bool bfs(int s,int t) { queue<node>q; memset(vis,0,sizeof(vis)); q.push(node{s,1}); node cur;vis[s]=true; while(!q.empty()){ cur=q.front();q.pop(); int k=first[cur.x]; num[cur.x]=cur.num;//记录每一点的层数 while(k!=-1){ if(!vis[v[k]]&&w[k]>0){ vis[v[k]]=true; q.push(node{v[k],cur.num+1}); } k=Next[k]; } } if(num[t]){return true;} else return false; } int dfs(int s,int t,int f) { if(s==t){return f;} int k=first[s]; vis[s]=true; int d; while(k!=-1){ if(!vis[v[k]]&&w[k]>0&&num[u[k]]==num[v[k]]-1){ int d=dfs(v[k],t,min(f,w[k])); w[k]-=d; w[r[k]]+=d; if(d!=0){return d;} } k=Next[k]; } return 0; } int dinic(int s,int t) { memset(num,0,sizeof(num)); int ans=0; while(bfs(s,t)){ int d; memset(vis,0,sizeof(vis)); while(d=dfs(s,t,inf)){ ans+=d; memset(vis,0,sizeof(vis)); } memset(num,0,sizeof(num)); } return ans; } int main() { while(cin>>m>>n){ fill(first,first+410,-1); for(int i=0;i<2*m;i++){ cin>>u[i]>>v[i]>>w[i]; r[i]=i+1;//记录反向边的编号 Next[i]=first[u[i]]; first[u[i]]=i; i++; u[i]=v[i-1]; v[i]=u[i-1]; w[i]=0; r[i]=i-1; Next[i]=first[u[i]]; first[u[i]]=i; } cout<<dinic(1,n)<<endl; } }
标签:fir cstring names cst stream ace ios pac pre
原文地址:https://www.cnblogs.com/ZGQblogs/p/9403535.html