POJ 1273 || HDU 1532 Drainage Ditches (最大流模型)

Drainage DitchesHal Burch
Time Limit 1000 ms
Memory Limit 65536 kb
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. Note however,
that there can be more than one ditch between two intersections.
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
Input file contains multiple test cases.
In a test case:
Line 1: 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.
Line 2..N+1: Each of 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,One line with 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 4.2


题意:就是给出各个边的最大流量,和起点终点,求最大流。


   Edmonds-Karp 增广路算法

Code:

 

//Edmondes-Karp

#include <cstdio>

#include <cstring>

#include <queue>

#define INF 0x7fffffff

using namespace std;

queue<int> q;

const int maxn = 200;

int n, m, ans;

int next[maxn+10], p[maxn+10], f[maxn+10][maxn+10], cap[maxn+10][maxn+10];

int Edmondes_Karp(int s, int t) {

    int ans = 0, v, u;

    queue<int> q;

    memset(f,0,sizeof(f));

    while(true) {

        memset(p,0,sizeof(p));

        p[s] = INF;

        q.push(s);

        while(!q.empty()) { //BFS找增广路

            int u = q.front();

            q.pop();

            for(v=1; v<=m; v++)

                if(!p[v]&&cap[u][v]>f[u][v]) { //找到新节点v

                    next[v] = u;                //记录v的父亲,并加入FIFO队列

                    q.push(v);

                    p[v] = p[u] < cap[u][v]-f[u][v]?p[u] : cap[u][v] - f[u][v];

                    //s-v路径上的最小残量

                }

        }

        if(!p[t]) break;    //找不到增广路,则当前流已经是最大流

        for(u=t; u!=s; u= next[u]) { //从汇点往回走

            f[next[u]][u] +=p[t];//更新正向流量

            f[u][next[u]] -=p[t];//更新反向流量

        }

        ans += p[t];    //更新从s流出的总流量

    }

    return ans;

}

int main() {

    int i, k, k1, k2, k3;

    while(~scanf("%d%d",&n,&m)) {

        memset(cap,0,sizeof(cap));

        for(i=1; i<=n; i++) {

            scanf("%d%d%d",&k1,&k2,&k3);

            cap[k1][k2] +=k3;

        }

        printf("%d\n",Edmondes_Karp(1,m) );

    }

    return 0;

}




 

 

你可能感兴趣的:(poj)