[网络流-最大流]POJ-1273Drainage Ditches

题目链接

http://poj.org/problem?id=1273


题目

基本最大流输入


题解

Edmonds-Karp算法


代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 500;
const int INF = 0x3f3f3f3f;
typedef struct node{
    int from,to,cap,flow;
    node(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){};
}Edge;
typedef struct Edmondskarp
{
    vector<Edge> edges;
    vector<int> G[maxn];
    int path[maxn];
    int alpha[maxn];

    void init() {
        for(int i=0;i<maxn;i++) G[i].clear();
        edges.clear();
    }

    void AddEdge(int u,int v,int c)
    {
        edges.push_back(Edge(u,v,c,0));
        edges.push_back(Edge(v,u,0,0));
        int len = edges.size();
        G[u].push_back(len-2);
        G[v].push_back(len-1);
    }

    int MaxFlow(int s,int t)
    {
        int maxflow = 0;
        while(true) {
            queue<int> qu;
            qu.push(s);
            memset(alpha,0,sizeof(alpha));
            alpha[s] = INF;
            while(!qu.empty()&&!alpha[t])
            {
                int x = qu.front();qu.pop();
                for(int i=0;i<G[x].size();i++)
                {
                    Edge &e = edges[G[x][i]];
                    if(!alpha[e.to] && e.cap > e.flow) {
                        alpha[e.to] = min(alpha[e.from],e.cap-e.flow);
                        path[e.to] = G[x][i];
                        qu.push(e.to);
                    }
                }
            }
            if(!alpha[t]) break;
            for(int u=t;u!=s;u=edges[path[u]].from)
            {
                edges[path[u]].flow+=alpha[t];
                edges[path[u]^1].flow -= alpha[t];
            }
            maxflow += alpha[t];
        }
        return maxflow;
    }

}EK;
int main()
{
    int n,m;
    while(~scanf("%d%d",&m,&n))
    {
        EK solve;
        solve.init();
        for(int i=0;i<m;i++)
        {
            int u,v,c;
            scanf("%d%d%d",&u,&v,&c);
            solve.AddEdge(u,v,c);
        }
        printf("%d\n",solve.MaxFlow(1,n));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值