最大流Edmonds-Karp模板

在广搜的基础上设计的算法。

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
const int num_p=200;
const int INF=1e9;
int flow[num_p][num_p],cap[num_p][num_p];
int p[num_p],a[num_p],n,m;
int EK()
{
    queue<int>q;
    memset(flow,0,sizeof(flow));
    int  f=0;
    int s=0,t=n-1;
    for(;;)
    {
        memset(a,0,sizeof(a));
        a[s]=INF;
        q.push(s);
        while(!q.empty())
        {
            int u=q.front();
            //   printf("u=%d\n",u);
            q.pop();
            for(int v=1; v<=n; v++)
                if(!a[v]&&cap[u][v]>flow[u][v])
                {
                    //  printf("p[%d]=%d\n",v,u);
                    p[v]=u;
                    //   printf("push(%d)\n",v);
                    q.push(v);
                    //    printf("a[%d]=%d\tcap[%d][%d]-flow[%d][%d]=%d\n",u,a[u],u,v,u,v,cap[u][v]-flow[u][v]);
                    a[v]=min(a[u],cap[u][v]-flow[u][v]);
                    //   printf("a[%d]=%d\n",v,a[v]);
                }
        }
        //   printf("a[%d]=%d\n",t,a[t]);
        if(a[t]==0)
            break;
        for(int u=t; u!=s; u=p[u])
        {
            flow[p[u]][u]+=a[t];
            // printf("flow[%d][%d]=%d\n",p[u],u,flow[p[u]][u]);
            flow[u][p[u]]-=a[t];
        }
        f+=a[t];
    }
    return f;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=0; i<m; i++)
        {
            int ua,vb,cc;
            scanf("%d%d%d",&ua,&vb,&cc);
            cap[ua][vb]=cc;
        }
        int ans= EK();
        cout<<ans<<endl;
    }
    return 0;
}


/*
6 10
0 1 16
0 2 13
1 2 10
2 1 4
1 3 12
3 2 9
2 4 14
3 5 20
4 3 7
4 5 4
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值