POJ 1273 Drainage Ditches



Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

const int maxn = 200 + 10;
const int INF = 99999999;

queue<int> apath;

int cap[maxn][maxn];
int flow[maxn][maxn];
int augPath[maxn];
int resNet[maxn];

void init()
{
    while( !apath.empty() )
        apath.pop();
    memset( cap, 0, sizeof( cap ) );
    memset( flow, 0, sizeof( flow ) );
}

int EdmondsKarp( int s, int t )
{
    int maxFlow = 0;
    while( true )
    {
        memset( resNet, 0, sizeof( resNet ) );
        resNet[s] = INF;
        augPath[s] = -1;
        apath.push( s );

        while( !apath.empty() )
        {
            int cur = apath.front();
            apath.pop();

            for( int i = 1; i <= t; i++ )
            {
                if( !resNet[i] && flow[cur][i] < cap[cur][i] )
                {
                    apath.push( i );
                    augPath[i] = cur;
                    resNet[i] = min( cap[cur][i] - flow[cur][i], resNet[cur] );
                }
            }
        }

        if( resNet[t] == 0 )
            break;
        for( int k = t; augPath[k] != -1; k = augPath[k] )
        {
            flow[augPath[k]][k] += resNet[t];
            flow[k][augPath[k]] -= resNet[t];
        }

        maxFlow += resNet[t];
    }
    return maxFlow;
}

int main()
{
    int n;
    int m;
    int si, ei, ci;

    while( ~scanf( "%d%d", &n, &m ) )
    {
        init();

        while( n-- )
        {
            scanf( "%d%d%d", &si, &ei, &ci );
            cap[si][ei] += ci;
        }

        printf( "%d\n", EdmondsKarp( 1, m ) );
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值