标签:style blog io color sp for on div log
注意:有时加边不一定要加反向弧。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<queue> 5 using namespace std; 6 #define INF 2147483647 7 #define MAXN 20011 8 #define MAXM 600301 9 int v[MAXM],cap[MAXM],en,first[MAXN],next[MAXM]; 10 int d[MAXN],cur[MAXN],S,T; 11 queue<int>q; 12 int n,m; 13 void Init_Dinic(){memset(first,-1,sizeof(first)); en=0;} 14 void AddEdge(const int &U,const int &V,const int &W) 15 { 16 v[en]=V; cap[en]=W; 17 next[en]=first[U]; 18 first[U]=en++; 19 v[en]=V; 20 next[en]=first[U]; 21 first[U]=en++; 22 } 23 bool bfs() 24 { 25 memset(d,-1,sizeof(d)); q.push(S); d[S]=0; 26 while(!q.empty()) 27 { 28 int U=q.front(); q.pop(); 29 for(int i=first[U];i!=-1;i=next[i]) 30 if(d[v[i]]==-1 && cap[i]) 31 { 32 d[v[i]]=d[U]+1; 33 q.push(v[i]); 34 } 35 } 36 return d[T]!=-1; 37 } 38 int dfs(int U,int a) 39 { 40 if(U==T || !a) return a; 41 int Flow=0,f; 42 for(int &i=cur[U];i!=-1;i=next[i]) 43 if(d[U]+1==d[v[i]] && (f=dfs(v[i],min(a,cap[i])))) 44 { 45 cap[i]-=f; cap[i^1]+=f; 46 Flow+=f; a-=f; if(!a) break; 47 } 48 if(!Flow) d[U]=-1; 49 return Flow; 50 } 51 int max_flow() 52 { 53 int Flow=0,tmp=0; 54 while(bfs()) 55 { 56 memcpy(cur,first,(n+5)*sizeof(int)); 57 while(tmp=dfs(S,INF)) Flow+=tmp; 58 } 59 return Flow; 60 }
标签:style blog io color sp for on div log
原文地址:http://www.cnblogs.com/autsky-jadek/p/4148722.html