POJ-1273 Drainage Ditches(最大流)

裸的最大流问题,照着模板打的.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <queue>

using namespace std;

#define INF 1e7
#define maxn 205 
struct Edge{
    int from, to, cap, flow;
    Edge(int a, int b, int c, int d){
        from = a;
        to = b;
        cap = c;
        flow = d;
    }
}; 
int n, m, a[maxn], path[maxn];
vector<Edge> v;
vector<int> Graph[maxn];
void Init(){

    for(int i = 1; i <= n; i++)
       Graph[i].clear();
    v.clear();
}
void AddEdge(int from, int to, int cap){

    v.push_back(Edge(from, to, cap, 0));
    v.push_back(Edge(to, from, 0, 0));

    int m = v.size();
    Graph[from].push_back(m-2);
    Graph[to].push_back(m-1);
}
int MaxFlow(int s, int t){

    int flow = 0;

    while(1){

        memset(a, 0, sizeof(a));
        a[s] = INF;
        queue<int> q;
        q.push(s);
        while(!q.empty()){

            int k = q.front();

            q.pop();
            for(int i = 0; i < Graph[k].size(); i++){

                Edge &e = v[Graph[k][i]];
                if(!a[e.to] && e.cap > e.flow){

                    a[e.to] = min(a[k], e.cap - e.flow);
                    path[e.to] = Graph[k][i];
                    q.push(e.to);
                }
            } 
            if(a[t])
              break;
        }
        if(!a[t])
          break;
        for(int i = t; i != s; i = v[path[i]].from){

            v[path[i]].flow += a[t];
            v[path[i]^1].flow -= a[t];
        }
        flow += a[t];        
    }
    return flow;
}

int main(){

    //freopen("in.txt", "r", stdin);
    while(cin >> m >> n){

        Init();
        int a, b, c;
        for(int i = 0; i < m; i++){

            cin >> a >> b >> c;
            AddEdge(a, b, c);
        }
        cout << MaxFlow(1, n) << endl;
    }   

    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值