POJ 1273 最大流(Dinic算法)

                                                  Drainage Ditches

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 90777   Accepted: 35220

Description

Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch. 
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network. 
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle. 

Input

The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.

Output

For each case, output a single integer, the maximum rate at which water may emptied from the pond.

Sample Input

5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10

Sample Output

50

Dinic模板:

#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <string>
#include <stack>
#include <algorithm>
#include <cstring>
#include <set>
#include <queue>
#include <map>
#include <stack>
#include <cmath>
#include <string>
#define inf 0x3f3f3f3f
#define L(rt) rt << 1
#define R(rt) rt << 1 | 1
#define rep(a, b) for(int i = a; i <= b; ++i)
#define eps 1e-2
using namespace std;
static const int N = 105;
static const int maxn = 205;
static const int mod = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
int n, m;
struct Dinic{
    struct Edge{
        int v, w, next;
    }edge[maxn << 1];
    int head[maxn], tot, dep[maxn], cur[maxn];
    void init(){
        tot = 0;
        memset(head, -1, sizeof(head));
    }
    void addEdge(int u, int v, int w){
        edge[tot].v = v;
        edge[tot].w = w;
        edge[tot].next = head[u];
        head[u] = tot++;
    }
    bool bfs(int s, int t){
        memset(dep, -1, sizeof(dep));
        dep[s] = 0;
        queue<int>q;
        q.push(s);
        while(!q.empty()){
            int u = q.front();
            q.pop();
            for(int i = head[u]; ~i; i = edge[i].next){
                int v = edge[i].v, w = edge[i].w;
                if(w > 0 && dep[v] == -1){
                    dep[v] = dep[u] + 1;
                    q.push(v);
                } 
            }
        }
        return dep[t] > 0;
    }
    int dfs(int u, int t, int f){
        if(u == t) return f;
        for(int& i = cur[u]; ~i; i = edge[i].next){
            int v = edge[i].v, w = edge[i].w;
            if(w > 0 && dep[v] == dep[u] + 1){
                int d = dfs(v, t, min(f, w));
                if(d > 0){
                    edge[i].w -= d;
                    edge[i ^ 1].w += d;
                    return d;
                }
            }
        }
        return 0;
    }
    int dinic(int s, int t){
        int ans = 0;
        while(bfs(s, t)){
            for(int i = 1; i <= n; ++i) cur[i] = head[i];
            while(int d = dfs(s, t, inf)){
                ans += d;
            }
        }
        return ans;
    }
}mf;
void mainwork(){
    while(scanf("%d%d", &m, &n) != EOF){
        mf.init();
        int u, v, w;
        for(int cas = 1; cas <= m; ++cas){
            scanf("%d%d%d", &u, &v, &w);
            mf.addEdge(u, v, w);
            mf.addEdge(v, u, 0);
        }
        printf("%d\n", mf.dinic(1, n));
    }
}
void input(){

}
int main(){
    //prework();
    mainwork();
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章