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;
}
时间: 2024-12-24 18:20:19

POJ1273 Drainage Ditches 【最大流Dinic】的相关文章

POJ1273:Drainage Ditches(最大流入门 EK,dinic算法)

http://poj.org/problem?id=1273 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 Jo

poj-1273 Drainage Ditches(最大流基础题)

题目链接: Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 67475   Accepted: 26075 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 cover

[POJ 1273] Drainage Ditches &amp; 最大流Dinic模板

Drainage Ditches 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

POJ1273 Drainage Ditches 最大流模板题(dinic)

最大流的模板题 给出边数M,顶点数N 以及每条边的容量 求1到N的最大流 注意可以有重边 邻接矩阵模板: #include<iostream> #include<cstdio> #include<cstring> #define maxx 0x3f3f3f #define M 205 using namespace std; int arc[M][M]; //弧的剩余流量 int level[M]; int n; int min(int a,int b) { retur

poj1273 Drainage Ditches(裸最大流)

Drainage Ditches Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Drainage Ditches Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description Every time it rains on Farmer Joh

Poj 1273 Drainage Ditches(最大流 Edmonds-Karp )

题目链接:poj1273 Drainage Ditches 呜呜,今天自学网络流,看了EK算法,学的晕晕的,留个简单模板题来作纪念... 1 #include<cstdio> 2 #include<vector> 3 #include<queue> 4 #include<cstring> 5 #include<set> 6 #include<algorithm> 7 #define CLR(a,b) memset((a),(b),si

POJ 1273 Drainage Ditches 最大流

很裸的最大流问题,不过注意会有重边,o(╯□╰)o,被阴了WA了一发 还有就是要用long long #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include

POJ-1273 Drainage Ditches 【最大流】

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

poj 1273 Drainage Ditches 最大流入门题

题目链接:http://poj.org/problem?id=1273 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