hdu1532 网络流

裸题,直接上模版。


#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <cstdio>
using namespace std;

const int inf = 500000000;
const int maxn = 2200;
struct node
{
    int u, v;//左右端点
    int f; //flow 流量
};
node e[maxn];

int first[maxn], cc;  //cc是结点编号
int next[maxn], p[maxn], a[maxn];


//加边
inline void add_edge(int u, int v, int f) //
{

    //加入正向边
    e[cc].u = u;
    e[cc].v = v;
    e[cc].f = f;
    next[cc] = first[u];
    first[u] = cc;
    cc++;

    //加入反向边
    e[cc].u = v;
    e[cc].v = u;
    e[cc].f = 0;  //是0
    next[cc] = first[v];
    first[v] = cc;
    cc++;

}

//最大流的EK算法

int EK(int s, int t)
{
    queue<int> q;
    int f = 0; //结果
    while(1)
    {
        memset(a, 0, sizeof(a));
        memset(p, -1, sizeof(p));

        q.push(s);
        a[s] = inf;


        while( !q.empty() )
        {
            int u = q.front();
            q.pop();
            int i;

            for(i = first[u]; i != -1; i = next[i] )
            {
                if( !a[e[i].v] && e[i].f )
                {
                    p[ e[i].v ] = i;
                    a[ e[i].v ] = min(a[u], e[i].f );
                    q.push(e[i].v);
                }
            }
        }

        if(a[t] == 0) break;

        int u;
        for(u = t; u != s; u = e[p[u]].u)
        {
            e[p[u]].f -= a[t];
            e[p[u] ^ 1].f += a[t];
        }
        f += a[t];
    }
    return f;
}

int main()
{
    int N, M;
    while( scanf("%d%d", &N, &M) != EOF)
    {
        cc = 0;
        memset(first, -1, sizeof first);
        memset(next, -1, sizeof next);
        int u, v, f;
        while(N--)
        {
            scanf("%d%d%d",&u,&v,&f);
            add_edge(u, v, f);
        }

        int ans = EK(1, M);
        printf("%d\n", ans);

    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值