POJ - 1273 Drainage Ditches(最大流)

裸的最大流,贴上刘汝佳紫书上的模板就能过。


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
const int maxn=420;
const int INF=0x3f3f3f3f;
struct Edge
{
    int from, to, cap, flow;
    Edge(int u, int v, int c, int f):from(u), to(v), cap(c), flow(f){}
};
struct EdmondsKarp
{
    int n, m;
    vector<Edge> edge;
    vector<int> G[maxn];
    int a[maxn];
    int p[maxn];

    void init(int n)
    {
        for(int i=1; i<=n; i++)
            G[i].clear();
        edge.clear();
    }
    void AddEdge(int from, int to, int cap)
    {
        edge.push_back(Edge(from, to, cap, 0));
        edge.push_back(Edge(to, from, 0, 0));
        m=edge.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }
    int Maxflow(int s, int t)
    {
        int flow=0;
        while(1)
        {
            memset(a, 0, sizeof(a));
            queue<int> q;
            q.push(s);
            a[s]=INF;
            while(!q.empty())
            {
                int x=q.front();
                q.pop();
                for(int i=0; i<G[x].size(); i++)
                {
                    Edge& e=edge[G[x][i]];
                    if(!a[e.to] && e.cap>e.flow)
                    {
                        p[e.to]=G[x][i];
                        a[e.to]=min(a[x], e.cap-e.flow);
                        q.push(e.to);
                    }
                }
                if(a[t])break;
            }
            if(!a[t])break;
            for(int u=t; u!=s; u=edge[p[u]].from)
            {
                edge[p[u]].flow+=a[t];
                edge[p[u]^1].flow-=a[t];
            }
            flow+=a[t];
        }
        return flow;
    }
}solver;

int main()
{
    int n, m;
    while(~scanf("%d%d", &m, &n))
    {
        solver.init(n);
        while(m--)
        {
            int u, v, c;
            scanf("%d%d%d", &u, &v, &c);
            solver.AddEdge(u, v, c);
        }
        printf("%d\n", solver.Maxflow(1, n));
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值