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;
}



你可能感兴趣的:(POJ-1273 Drainage Ditches(最大流))