首页 > 代码库 > POJ1273 Drainage Ditches 【最大流Dinic】

POJ1273 Drainage Ditches 【最大流Dinic】

Drainage Ditches
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 56870 Accepted: 21863

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

Source

USACO 93

题意:给定n和m,其中n表示水沟的条数,m表示汇点。然后给出n条水沟的详细情况s、t、c表示从节点s到t的容量为c。其中1为源点,求源点到汇点的最大流。

题解:Dinic算法流程,先对原图用BFS进行分层处理,即每一个点相对于源点的最近位置,一直处理到汇点为止。然后从源点开始进行深搜(用栈维护)确保每次都是按照标号增1的顺序搜索下去,如果不能搜索下去则回溯,直到找到一条增广路,然后路径上的每一段都减少此路上的瓶颈值,然后继续深搜,若搜不出增广路则重新分层,接着深搜下去,直到最终无法分层才得到最大流值。

#include <stdio.h>
#include <string.h>
#include <queue>
#define maxn 202
#define inf 0x7fffffff
using std::queue;

int G[maxn][maxn], Layer[maxn];
int Sta[maxn];
bool vis[maxn];

bool countLayer(int m) {
    queue<int> Q; Q.push(1);
    memset(Layer, 0, sizeof(int) * (m + 1));
    int i, now;
    Layer[1] = 1;
    while(!Q.empty()) {
        now = Q.front(); Q.pop();
        for(i = 1; i <= m; ++i) {
            if(G[now][i] && !Layer[i]) {
                Layer[i] = Layer[now] + 1;
                if(m == i) return true;
                else Q.push(i);
            }
        }
    }
    return false;
}

int Dinic(int m) {
    int minCut, minCutPos, maxFlow = 0;
    int s, t, i, id = 0, now;
    while(countLayer(m)) {
        memset(vis, 0, sizeof(bool) * (m + 1));
        vis[1] = 1; Sta[id++] = 1;
        while(id) {
            //栈中需要存放路径,所以不出栈
            now = Sta[id - 1];
            if(now == m) {
                minCut = inf;
                for(i = 1; i < id; ++i) {
                    s = Sta[i - 1];
                    t = Sta[i];
                    if(G[s][t] && G[s][t] < minCut) {
                        minCut = G[s][t];
                        minCutPos = s;
                    }
                }
                //增广,添加取消流
                maxFlow += minCut;
                for(i = 1; i < id; ++i) {
                    s = Sta[i - 1]; t = Sta[i];
                    G[s][t] -= minCut;
                    G[t][s] += minCut;
                }
                //出栈到最小割位置
                while(id && Sta[id - 1] != minCutPos) {
                    vis[Sta[--id]] = 0;
                }
            } else {
                for(i = 1; i <= m; ++i) {
                    if(G[now][i] && !vis[i] && Layer[now] + 1 == Layer[i]) {
                        Sta[id++] = i; vis[i] = 1;
                        break;
                    }
                }
                if(i > m) --id;
            }
        }
    }
    return maxFlow;
}

int main() {
    //freopen("stdin.txt", "r", stdin);
    int n, m, i, s, t, c;
    while(scanf("%d%d", &n, &m) == 2) {
        memset(G, 0, sizeof(G));
        for(i = 0; i < n; ++i) {
            scanf("%d%d%d", &s, &t, &c);
            G[s][t] += c;
        }
        printf("%d\n", Dinic(m));
    }
    return 0;
}



POJ1273 Drainage Ditches 【最大流Dinic】