1 int bfs() 2 { 3 queue<int>q; 4 memset(d,0,sizeof(d)); 5 d[1]=1; 6 q.push(1); 7 while (!q.empty()) 8 { 9 int u=q.front(); 10 q.pop(); 11 for (int i=head[u];i!=-1;i=eage[i].next) 12 { 13 int v=eage[i].v; 14 if (!d[v]&&eage[i].cap>0) 15 { 16 d[v]=d[u]+1; 17 q.push(v); 18 } 19 } 20 } 21 return d[n]; 22 } 23 24 int dfs(int u,int w) 25 { 26 if (u==n||w==0) return w; 27 int ans=0; 28 for (int i=head[u];i!=-1;i=eage[i].next) 29 { 30 int v=eage[i].v; 31 if (d[u]==d[v]-1&&eage[i].cap>0) 32 { 33 int temp=dfs(v,min(w,eage[i].cap)); 34 eage[i].cap-=temp; 35 eage[i^1].cap+=temp; 36 w-=temp; 37 ans+=temp; 38 } 39 } 40 return ans; 41 } 42 43 int dinic() 44 { 45 int ans=0; 46 while (bfs()) ans+=dfs(1,10000000); 47 return ans; 48 }
原文:http://www.cnblogs.com/pblr/p/5719633.html