POJ1237 Drainage Ditches (网络流)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/pengwill97/article/details/79950159

题意分析

农夫John有N个池塘一次编号为1到N,在这些池塘之间有M条单向的水渠,每一条有一个最大的通水速度。每一个池子里有一个调节器,可以设定池子里的水分别通过哪些水渠流多少到别的池子。现在要求从编号为1的水池向编号为N的水池进行输水,最大的通水速度是多少。

最大流 FF居然没有卡。

代码总览

#include<cstdio>
#include<algorithm>
#include<queue>
#include<bitset>
#include<cstring>
using namespace std;
const int nmax = 205;
int tt,kase = 1,n,m,s = 1,t,ans;
int mp[nmax][nmax], pre[nmax];
bitset<nmax> visit;
bool findpath() {
    visit.reset();
    memset (pre,0,sizeof pre);
    queue<int> q; q.push(s); visit.set(s); pre[s] = s;
    while(!q.empty()) {
        int cur = q.front(); q.pop();
        if(cur == t) return true;
        for(int i = 1;i<=n;++i) {
            if(!visit.test(i) && mp[cur][i]) {
                visit.set(i);
                pre[i] = cur;
                q.push(i);
            }
        }
    }
    return false;
}
void max_flow(){
    while(true){
        if(!findpath()) return;
        int minx = 0x3f3f3f3f;
        for(int i = t;i != s; i = pre[i]) minx = min(minx,mp[pre[i]][i]);
        for(int i = t;i != s; i = pre[i]){
            mp[pre[i]][i] -= minx;
            mp[i][pre[i]] += minx;
        }
        ans += minx;
    }
}
int main(){
    while(scanf("%d %d",&m,&n) !=EOF) {
        memset(mp,0,sizeof mp);
        t = n; ans = 0;
        int a,b,c;
        for(int i = 0;i<m;++i){
            scanf("%d %d %d",&a,&b,&c);
            mp[a][b] += c;
        }
        max_flow();
        printf("%d\n",ans);
    }
    return 0;
}
阅读更多

Drainage Ditches

05-26

Problem DescriptionnEvery 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. nFarmer 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. nGiven 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. n nnInputnThe 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.n nnOutputnFor each case, output a single integer, the maximum rate at which water may emptied from the pond. n nnSample Inputn5 4n1 2 40n1 4 20n2 4 20n2 3 30n3 4 10n nnSample Outputn50

没有更多推荐了,返回首页