最大流模板

本文深入解析了最大流算法,包括其在解决复杂网络流量问题中的应用。通过具体案例,介绍了如何使用最大流算法来确定从水源到河流的最大排水速率,详细讲解了BFS和DFS在算法中的作用,以及如何通过链式前向星优化算法性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

模板一:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值