网络流模板
Posted windystreet
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了网络流模板相关的知识,希望对你有一定的参考价值。
const int maxn = 1e5+2; const int INF = 1<<30; const int mod = 1e9+7; struct Edge{ int from,to,cap,flow; }; struct Dinic { int n,m,s,t; vector<Edge>edge; vector<int>G[maxn]; bool vis[maxn]; int d[maxn]; int cur[maxn]; void init(int n){ this->n = n; for(int i=0;i<=n;i++)G[i].clear(),edge.clear(); } void addedge(int from,int to,int cap){ edge.pb((Edge){from,to,cap,0}); edge.pb((Edge){to,from,0,0}); m = edge.size(); G[from].pb(m-2); G[to].pb(m-1); } bool bfs(){ mem(vis,0); queue<int>Q; Q.push(s); d[s] = 0; vis[s] = 1; while(!Q.empty()){ int x = Q.front(); Q.pop(); int sz = G[x].size(); for(int i=0;i<sz;++i){ Edge &e = edge[G[x][i]]; if(!vis[e.to] && e.cap>e.flow){ vis[e.to] = 1 ; d[e.to] = d[x] + 1; Q.push(e.to); } } } return vis[t]; } int dfs(int x,int a){ if(x == t || a == 0)return a; int flow = 0,f; int sz = G[x].size(); for(int &i = cur[x];i<sz;i++){ Edge &e = edge[G[x][i]]; if(d[x] + 1 == d[e.to] && (f = dfs(e.to,min(a,e.cap - e.flow)))>0){ e.flow += f; edge[G[x][i]^1].flow -=f; flow += f; a -= f; if(a==0)break; } } // if(!flow) d[x] = -2; //炸点优化 return flow; } int maxflow(int s,int t){ this->s = s; this -> t = t; int flow = 0; while(bfs()){ mem(cur,0); flow += dfs(s,INF); } return flow; } };
以上是关于网络流模板的主要内容,如果未能解决你的问题,请参考以下文章