HDU 1532 Drainage Ditches(最大流)

最大流模版题,弄了好多天也没有明白..
今天好不容易根据记忆写了个..

/*  xzppp  */
#include <iostream>
#include <vector>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <string>
#include <cmath>
#include <bitset>
#include <iomanip>
using namespace std;
#define FFF freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define MP make_pair
#define PB push_back
typedef long long  LL;
typedef unsigned long long ULL;
typedef pair<int,int > pii;
typedef pair<LL,LL> pll;
typedef pair<double,double > pdd;
typedef pair<double,int > pdi;
const int MAXN = 1e5+17;
const int MAXM = 1e6+17;
const int MAXV = 2*1e3+17;
const int BIT = 15+3;
const int INF = 0x7fffffff;
const LL INFF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 998244353;
int vis[MAXN];
struct edge
{
    int to,cap,rev;
    edge(int a,int b,int c):to(a),cap(b),rev(c){};
};
vector<edge > G[MAXN];
void adeg(int from,int to,int cap)
{
    G[from].push_back(edge(to,cap,G[to].size()));
    G[to].push_back(edge(from,0,G[from].size()-1));
}
int dfs(int p,int t,int f)
{
    if(p==t) return f;
    vis[p] = 1;
    for (int i = 0; i < G[p].size(); ++i)
    {
        int next = G[p][i].to;
        if(!vis[next]&&G[p][i].cap>0)
        {
            int d = dfs(next,t,min(G[p][i].cap,f));
            if(d>0)
            {
                G[p][i].cap -= d;
                G[next][G[p][i].rev].cap += d;
                return d;
            }
        }
    }
    return 0;
}
int maxflow(int s,int t)
{
    int res = 0;
    while(1)
    {
        memset(vis, 0, sizeof(vis));
        int f = dfs(s,t,INF);
        if(f==0) return res;
        res += f;
    }
}
int main()
{
    #ifndef ONLINE_JUDGE 
    FFF
    #endif
    int m,n;
    while(cin>>m>>n)
    {
        for (int i = 0; i < n; ++i)
        {
            G[i].clear();
        }
        for (int i = 0; i < m; ++i)
        {
            int u,v,c;
            scanf("%d%d%d",&u,&v,&c);
            u--;v--;
            adeg(u, v, c);
        }
        int ans = maxflow(0, n-1);
        cout<<ans<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值