最大流 && 最小费用最大流模板
模板从 这里 搬运,链接博客还有很多网络流题集题解参考。
最大流模板 ( 可处理重边 )
const int maxn = 1e6 + 10; const int INF = 0x3f3f3f3f; struct Edge { int from,to,cap,flow; Edge(){} Edge(int from,int to,int cap,int flow):from(from),to(to),cap(cap),flow(flow){} }; struct Dinic { int n,m,s,t; //结点数,边数(包括反向弧),源点与汇点编号 vector<Edge> edges; //边表 edges[e]和edges[e^1]互为反向弧 vector<int> G[maxn]; //邻接表,G[i][j]表示结点i的第j条边在e数组中的序号 bool vis[maxn]; //BFS使用,标记一个节点是否被遍历过 int d[maxn]; //d[i]表从起点s到i点的距离(层次) int cur[maxn]; //cur[i]表当前正访问i节点的第cur[i]条弧 void init(int n,int s,int t) { this->n=n,this->s=s,this->t=t; for(int i=0;i<=n;i++) G[i].clear(); edges.clear(); } void AddEdge(int from,int to,int cap) { edges.push_back( Edge(from,to,cap,0) ); edges.push_back( Edge(to,from,0,0) ); m = edges.size(); G[from].push_back(m-2); G[to].push_back(m-1); } bool BFS() { memset(vis,0,sizeof(vis)); queue<int> Q;//用来保存节点编号的 Q.push(s); d[s]=0; vis[s]=true; while(!Q.empty()) { int x=Q.front(); Q.pop(); for(int i=0; i<G[x].size(); i++) { Edge& e=edges[G[x][i]]; if(!vis[e.to] && e.cap>e.flow) { vis[e.to]=true; d[e.to] = d[x]+1; Q.push(e.to); } } } return vis[t]; } //a表示从s到x目前为止所有弧的最小残量 //flow表示从x到t的最小残量 int DFS(int x,int a) { if(x==t || a==0)return a; int flow=0,f;//flow用来记录从x到t的最小残量 for(int& i=cur[x]; i<G[x].size(); i++) { Edge& e=edges[G[x][i]]; if(d[x]+1==d[e.to] && (f=DFS( e.to,min(a,e.cap-e.flow) ) )>0 ) { e.flow +=f; edges[G[x][i]^1].flow -=f; flow += f; a -= f; if(a==0) break; } } if(!flow) d[x] = -1;///炸点优化 return flow; } int Maxflow() { int flow=0; while(BFS()) { memset(cur,0,sizeof(cur)); flow += DFS(s,INF); } return flow; } }DC;
#include<bits/stdc++.h> using namespace std; const int maxn = 1210; const int maxm = 240005;///边要是题目规定的两倍 const int INF = 0x3f3f3f3f; struct edge{ int to,cap,tot,rev; }; struct DINIC{ int n,m; edge w[maxm]; int fr[maxm]; int num[maxn],cur[maxn],first[maxn]; edge e[maxm]; void init(int n){ memset(cur,0,sizeof(cur)); this->n=n; m=0; } void AddEdge(int from,int to,int cap){ w[++m]=(edge){to,cap}; num[from]++,fr[m]=from; w[++m]=(edge){from,0}; num[to]++,fr[m]=to; } void prepare(){ first[1]=1; for(int i=2;i<=n;i++) first[i]=first[i-1]+num[i-1]; for(int i=1;i<n;i++) num[i]=first[i+1]-1; num[n]=m; for(int i=1;i<=m;i++){ e[first[fr[i]]+(cur[fr[i]]++)]=w[i]; if (!(i%2)){ e[first[fr[i]]+cur[fr[i]]-1].rev=first[w[i].to]+cur[w[i].to]-1; e[first[w[i].to]+cur[w[i].to]-1].rev=first[fr[i]]+cur[fr[i]]-1; } } } int q[maxn]; int dist[maxn]; int t; bool bfs(int s){ int l=1,r=1; q[1]=s; memset(dist,-1,(n+1)*4); dist[s]=0; while(l<=r){ int u=q[l++]; for(int i=first[u];i<=num[u];i++){ int v=e[i].to; if ((dist[v]!=-1) || (!e[i].cap)) continue; dist[v]=dist[u]+1; if (v==t) return true; q[++r]=v; } } return dist[t]!=-1; } int dfs(int u,int flow){ if (u==t) return flow; int ans=0; for(int& i=cur[u];i<=num[u];i++){ int v=e[i].to; if (!e[i].cap || dist[v]!=dist[u]+1) continue; int t=dfs(v,min(flow,e[i].cap)); if (t){ e[i].cap-=t; e[e[i].rev].tot+=t; flow-=t; ans+=t; if (!flow) return ans; } } return ans; } int MaxFlow(int s,int t){ int ans=0; this->t=t; while(bfs(s)){ do{ memcpy(cur,first,(n+1)*4); int flow; while(flow=dfs(s,INF)) ans+=flow; }while(bfs(s)); for(int i=1;i<=m;i++) e[i].cap+=e[i].tot,e[i].tot=0; } return ans; } }DC; int main(void) { int N, M, S, T; while(~scanf("%d %d %d %d", &N, &M, &S, &T)){ DC.init(N); while(M--){ int u, v, w; scanf("%d %d %d", &u, &v, &w); DC.AddEdge(u, v, w); } DC.prepare(); printf("%d", DC.MaxFlow(S, T)); } return 0; }