POJ 1273 Drainage Ditches 最大流入门题

一道基础的网络流中的最大流问题

题目链接:http://poj.org/problem?id=1273

利用最大流定理:残留网络上找不到增广路径,则当前流为最大流;反之,如果当前流不为最大流,则一定
有增广路径,进行多次广搜累加流量即可。

Drainage Ditches
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 59879   Accepted: 22988

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


AC代码:

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <limits.h>
using namespace std;
#define MAXN 210
int INF = INT_MAX;
int n, m, st, ed;
int maze[MAXN][MAXN], flow[MAXN], path[MAXN];
queue<int> q;
int BFS() {
    while(!q.empty()) q.pop();
    memset(path, -1, sizeof(path));
    path[st] = 0, flow[st] = INF;
    q.push(st);
    while(!q.empty()) {
        int tmp = q.front(); q.pop();
        if(tmp == ed) break;
        for(int i = 1; i <= m; i++) {
            if(i != st && path[i] == -1 && maze[tmp][i]) {
                flow[i] = min(flow[tmp], maze[tmp][i]);
                q.push(i);
                path[i] = tmp;
            }
        }
    }
    if(path[ed] == -1) return -1;
    return flow[ed];
}
int Edmonds_Karp() {
    int max_flow = 0, step, now, pre;
    while(1) {
        step = BFS();
        if(step == -1) break;
        max_flow += step;
        now = ed;
        while(now != st) {
            pre = path[now];
            maze[pre][now] -= step; //减少正向边流量
            maze[now][pre] += step; //添加反向边流量
            now = pre;
        }
    }
    return max_flow;
}
int main() {
    while(~scanf("%d%d", &n, &m)) {
        memset(maze, 0, sizeof(maze));
        int u, v, x;
        while(n--) {
            scanf("%d%d%d", &u, &v, &x);
            maze[u][v] += x;
        }
        st = 1, ed = m;
        printf("%d\n", Edmonds_Karp());
    }
    return 0;
}


posted @ 2015-04-10 02:07  gaoxiang36999  阅读(148)  评论(0编辑  收藏  举报