USACO Section 4.2 Drainage Ditches(最大流)

最大流问题。ISAP算法。注意可能会有重边,不过我用的数据结构支持重边。距离d我直接初始化为0,也可以用BFS逆向找一次。

-----------------------------------------------------------------------

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#define rep(i,l,r) for(int i=l;i<r;i++)
#define dow(i,l,r) for(int i=l;i>r;i--)
#define clr(x,c) memset(x,c,sizeof(x))
using namespace std;
const int inf=0x3f3f3f3f,maxn=200+5;
struct edge {
    int from,to,cap,flow;
};
struct ISAP {
    int n,m,s,t;
    vector<edge> edges;
    vector<int> g[maxn];
    int d[maxn];
    int cur[maxn];
    int p[maxn];
    int num[maxn];
    
    void init(int n) {
        this->n=n;
        rep(i,0,n) g[i].clear();
        edges.clear();
    }
    
    void addEdge(int from,int to,int cap) {
        edges.push_back((edge){from,to,cap,0});
        edges.push_back((edge){to,from,0,0});
        m=edges.size();
        g[from].push_back(m-2);
        g[to].push_back(m-1);
    }
    
    int augment() {
        int x=t,a=inf;
        while(x!=s) {
            edge& e=edges[p[x]];
            a=min(a,e.cap-e.flow);
            x=edges[p[x]].from;
        }
        x=t;
        while(x!=s) {
            edges[p[x]].flow+=a;
            edges[p[x]^1].flow-=a;
            x=edges[p[x]].from;
        }
        return a;
    }
    
    int maxFlow(int s,int t) {
        this->s=s; this->t=t;
        int flow=0;
        clr(d,0);
        clr(num,0);
        rep(i,0,n) num[d[i]]++;
        int x=s;
        clr(cur,0);
        while(d[s]<n) {
            if(x==t) {
                flow+=augment();
                x=s;
            }
            int ok=0;
            rep(i,cur[x],g[x].size()) {
                edge& e=edges[g[x][i]];
                if(e.cap>e.flow && d[x]==d[e.to]+1) {
                    ok=1;
                    p[e.to]=g[x][i];
                    cur[x]=i;
                    x=e.to;
                    break;
                }
            }
            if(!ok) {
                int m=n-1;
                rep(i,0,g[x].size()) {
                    edge& e=edges[g[x][i]];
                    if(e.cap>e.flow) m=min(m,d[e.to]);
                }
                if(--num[d[x]]==0) break;
                num[d[x]=m+1]++;
                cur[x]=0;
                if(x!=s) x=edges[p[x]].from;
            }
        }
        return flow;
    }
} isap;
int s() {
    int n,m;
    cin>>m>>n;
    isap.init(n);
    rep(i,0,m) {
        int from,to,cap,pd=1;
        scanf("%d%d%d",&from,&to,&cap);
        isap.addEdge(from-1,to-1,cap);
    }
    return isap.maxFlow(0,n-1);
}
int main() {
    freopen("ditch.in","r",stdin);
    freopen("ditch.out","w",stdout);
    
    cout<<s()<<endl;
    
    return 0;
}

----------------------------------------------------------------------- 

Drainage Ditches
Hal Burch

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.

PROGRAM NAME: ditch

INPUT FORMAT

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.

SAMPLE INPUT (file ditch.in)

5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10

OUTPUT FORMAT

One line with a single integer, the maximum rate at which water may emptied from the pond.

SAMPLE OUTPUT (file ditch.out)

50
posted @ 2015-02-17 21:31  JSZX11556  阅读(283)  评论(0编辑  收藏  举报