标签:
#include <bits/stdc++.h> using namespace std; const int N (1e5+5), M(1e5+5); int head[N]; struct Edge{ /* r: residual capacity */ int v, r, nt; }E[M]; int tail; void add_edge(int u, int v, int c){ E[tail]={v, c, head[u]}, head[u]=tail++; E[tail]={u, 0, head[v]}, head[v]=tail++; } int level[N]; void bfs(int s){ memset(level, -1, sizeof(level)); queue<int> que; level[s]=0; que.push(s); for(int u; !que.empty(); ){ u=que.front(), que.pop(); for(int i=head[u]; ~i; i=E[i].nt){ int &v=E[i].v; if(E[i].r>0 && level[v]<0){ level[v]=level[u]+1; que.push(v); } } } } int iter[N]; int dfs(int u, int t, int f){ /* t: terminal (sink) */ if(u==t) return f; for(int &i=iter[u]; i!=-1; i++){ int &v=E[i].v, &r=E[i].r; if(r>0 && level[u]<level[v]){ int d=dfs(v, t, min(f, r)); if(d>0){ r-=d; E[i^1].r+=d; return d; } } } return 0; } int dinic(int s, int t){ const int INF=1<<30; for(int flow=0;;){ bfs(s); if(level[t]<0) return flow; memcpy(iter, head, sizeof(iter)); for(int f; f=dfs(s, t, INF); flow+=f); } } void init(){ tail=0; memset(head, -1, sizeof(head)); }
标签:
原文地址:http://www.cnblogs.com/Patt/p/5014463.html