最大流模板

模板一:

Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch. 
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network. 
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle. 

InputThe input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch. 
OutputFor each case, output a single integer, the maximum rate at which water may emptied from the pond. 
Sample Input

5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10

Sample Output

50
以该题为例。输入m条边,n个点,然后是m条边,对应有权值,
思路 主要是有一个分层数组d,,然后用bfs对数组d进行更新,用dfs按照d的限制进行跑图,返回的是当前状态起点到终点的值,期间会对flow数组修改,即加每次递归前都会加一条反向边, 然后反复更新分层数组,直到d[n]==-1,退出。
#include<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
using namespace std;
typedef long long ll;
const int N=1E3+7;
const ll inf =9223372036854775800;
ll n,m;
ll flow[N][N],d[N];
void bfs(){
    memset(d,-1,sizeof(d));
    queue<ll>que;
    que.push(1);
    d[1]=0; 
    while(que.size()){
        ll x=que.front();que.pop(); 
        for(int i=1;i<=m;i++){
            if(d[i]<0 && flow[x][i]>0){
                d[i]=d[x]+1;
                que.push(i);
            }
        }
    }
}

ll dfs(int x,ll mx)
{
    if(x == m) return mx ;
    int a;
    for(int i = 1;i<= m;i++){
        if(flow[x][i] > 0 && d[i] == d[x] + 1  && (a =dfs(i,min(mx,flow[x][i] )))){
            flow[x][i] -= a;
            flow[i][x] += a;
            return a ;
        }
    }
    return 0 ;
}
int main(){
    while(cin>>n>>m){
        ll x,y,z;
        memset(flow,0,sizeof(flow));
        for(int i=1;i<=n;i++){    
            scanf("%lld%lld%lld",&x,&y,&z);
            flow[x][y]+=z;
        }
        ll ans=0;
        while(1){
            bfs();  
            if(d[m]==-1) break;
            ans+=dfs(1,inf); 
        }
        cout<<ans<<endl;
    }
    return 0;
}

模板二 链式前向星+最大流(可以进行狐优化)

#include<bits/stdc++.h>
using namespace std;
const int N=1E5+7;
const int inf=1e9+7;
int head[N],d[N];
struct stu{
    int to;
    int w;
    int nxt;
}arr[N];
int cnt=0;
int n,m;

void add(int x,int y,int z){
    arr[cnt].to=y;
    arr[cnt].w=z;
    arr[cnt].nxt=head[x];
    head[x]=cnt++;
}
void bfs(){
    memset(d,-1,sizeof(d));
    queue<int >que;
    que.push(1);
    d[1]=0;
    while(que.size()){
        int x=que.front();que.pop();
        for(int i=head[x];i!=-1;i=arr[i].nxt){
            int dx=arr[i].to;
            int dw=arr[i].w;
            if(d[dx]<0&&dw>0){
                d[dx]=d[x]+1;
                que.push(dx);
            } 
        }
    }
}

int dfs(int x,int y){
    if(x==m) return y;
    int a=0;
    for(int i=head[x];i!=-1;i=arr[i].nxt){
        int dx=arr[i].to;
        int dw=arr[i].w;
        if(dw>0&&d[dx]==d[x]+1){
            int x1=dfs(dx,min(y,dw));
            a+=x1;
            y-=x1;
            arr[i].w-=x1;
            arr[i^1].w+=x1;
            if(y==0) break;
        }
    }
    if(a==0) d[x]=-1;//如果说a==0,也就是当前状态不能发生分流,直接标记为-1,并且当前状态的返回值为0; 
    return a;
}
int main(){ 
     
    while(~scanf("%d%d",&n,&m)){
        memset(head,-1,sizeof(head));
        int a,b,c;
        for(int i=1;i<=n;i++){
            scanf("%d%d%d",&a,&b,&c);
            add(a,b,c);
            add(b,a,0);
        }
        int ans=0;
        while(1){
            bfs(); 
            if(d[m]==-1) break;
            else ans+=dfs(1,inf); 
        }
        cout<<ans<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/Accepting/p/11377994.html