POJ1273 network flow []

                                                         Drainage Ditches
 
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 91824   Accepted: 35588

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

 
Thinking: capacity between the given point, find the maximum flow of the entire network, the board maximum flow problem, here is the ISAP template.
 
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>

using namespace std;
const int maxnE = 1e6 + 7;
const int maxn  = 1e5 + 7;
const int maxnQ = 1e6 + 7;
const int inf   = 0x3f3f3f3f;

struct Edge {
     int V; /// arc tail 
    int CAP; /// capacity 
    int NXT; /// directed at an arc starting from a same arc head 
} e [maxnE];

int head [MAXN], CNT;
 int D [MAXN], CUR [MAXN], pre [MAXN], NUM [MAXN];
 int Source, sink; /// super source, sink super 
int NV; /// renumbered the upper limit of the 
int n-, m;

queue <int> q;

void add(int u, int v, int capacity) {
    e [cnt] .v = v;
    e[cnt].cap = capacity;
    to [cnt] .nxt = Head [e];
    head [U] = CNT ++ ;
     // forward edge 

    E [CNT] .v = U;
    and [cnt] .cap = 0 ;
    e[cnt].nxt = head[v];
    head [V] = CNT ++ ;
     // reverse side 
}

void rev_bfs () { /// reverse BFS 
    Memset (NUM, 0 , the sizeof (NUM));
    memset(d, -1, sizeof(d));
    D [sink] = 0 ; /// super sink directly labeled 
    NUM [ 0 ] = . 1 ;
    q.push(sink);
    while(!q.empty()) {
        int u = q.front();
        q.pop();
        for(int i = head[u]; ~i; i = e[i].nxt) {
            int v = e[i].v;
            if(~d[v]) continue;///已经标过号
            d[v] = d[u] + 1;
            q.push(v);
            Surely [d [v]] ++ ;
        }
    }
}

int ISAP () {
    the memcpy (CUR, head, the sizeof (CUR)); /// current arc optimization 
    rev_bfs ();
     int Flow = 0 , U = pre [Source] = Source;
     int I;
     the while (D [sink] <NV) { / // the longest chain, is the largest index nv-1, if the fault is greater than or equal DESCRIPTION nv 
        // the printf ( "Flow:% D \ n-", Flow); 
        IF (U == sink) { / // If it finds an augmenting path, is modified along the entry path Flow 
            int F = INF, Neck;
             for (I = Source;! I = sink; I = E [CUR [I]] V.) { // / modifying the flow rate 
                IF (F> E [CUR [I]]. CAP) {
                    F . = E [CUR [I]] CAP; /// decreasing flow rate required 
                    Neck = I; /// recording rollback point, without going back to the starting point to find 
                }
            }
            for (!. I = Source; I = sink; I = E [CUR [I]] V) { /// modifying the flow rate 
                E [CUR [I]] CAP - =. F;
                and [cur [i] ^ 1 ] .cap + = f;
            }
            flow += f;
            u = neck;///回退
        }
        for(i = cur[u]; ~i; i = e[i].nxt) {
            if(d[e[i].v] + 1 == d[u] && e[i].cap) break;
        }
        IF (~ I) {
             // if there is viable augmenting path 
            CUR [U] = I;
            pre [e [i] .v] = u;
            u = e[i].v;
        } The else { /// Otherwise backoff, re-find augmenting path 
            IF ( 0 == (--num [D [U]])) BREAK ;
             int Mind = NV;
             for (I = head [U]; ~ I ; I = E [I] .nxt) {
                 IF (E [I] && .cap Mind> D [E [I] .v]) { /// find the smallest can be augmented index 
                    CUR [U] = I ;
                    mind = d[e[i].v];
                }
            }
            d[u] = mind + 1;
            num[d[u]]++;
            u = pre [u]; /// 回退
        }
    }
    return flow;
}

void the init () { /// initialization 
    Memset (head, - . 1 , the sizeof (head));
    cnt = 0;
}

void solve()
{
    you and, v, c;
    init();
    for(int i = 0; i < m; ++i) {
        scanf("%d %d %d",&u, &v, &c);
        add(u,v,c);
    }
    source = 1, sink = n, nv = sink + 1;
    printf("%d\n",ISAP());
}

int main ()
{
    while(scanf("%d %d", &m, &n) != EOF) {
        solve();
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/orangeko/p/11918969.html