POJ1273 drainage ditches "max Stream" "SAP"

Source: Internet
Author: User

Topic Links:

http://poj.org/problem?id=1273


Main topic:

Farmer John's field has M ponds and n ditches for drainage, pond number 1~m,1 Pond is the source of all ditches,

Pond M is a ditch . meeting Point. give you the pond connected by n ditches and the amount of water that can flow, and ask the whole ditch from the source to the meeting point .

How much water can flow at most.


Ideas:

It is obvious that the maximum flow problem of network flow is obtained. Use a chain forward star (adjacency table) to store the network so you don't have to consider the problem of re-edge. It

In fact, the heavy edges are parallel edges. Using SAP Algorithm +GAP optimization to find the maximum flow. Sap+gap template refer to my other

A blog post: http://blog.csdn.net/lianai911/article/details/44962653


AC Code:

#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <    queue>using namespace Std;const int maxn = 220;const int MAXM = maxn*maxn;const int INF = 0xffffff0;struct edgenode{    int to;    int W; int next;}    Edges[maxm];int head[maxn],id;void addedges (int u,int v,int w) {edges[id].to = v;    EDGES[ID].W = W;    Edges[id].next = Head[u];    Head[u] = id++;    edges[id].to = u;    EDGES[ID].W = 0;    Edges[id].next = Head[v]; HEAD[V] = id++;}    int numh[maxn],h[maxn],curedges[maxn],pre[maxn];void BFS (int end,int N) {memset (numh,0,sizeof (NUMH));    for (int i = 1; I <= N; ++i) numh[h[i]=n]++;    H[end] = 0;    numh[n]--;    numh[0]++;    Queue<int> Q;    Q.push (end); while (!        Q.empty ()) {int v = q.front ();        Q.pop ();        int i = head[v];            while (i! =-1) {int u = edges[i].to;                if (H[u] < N) {i = Edges[i].next; Continue           } H[u] = H[v] + 1;            numh[n]--;            numh[h[u]]++;            Q.push (U);        i = Edges[i].next;    }}}int SAP (int start,int end,int N) {int curflow,flowans = 0,temp,neck;    memset (h,0,sizeof (h));    memset (pre,-1,sizeof (pre));    for (int i = 1; I <= N; ++i) curedges[i] = Head[i];    BFS (End,n);    int u = start;            while (H[start] < N) {if (U = = end) {Curflow = INF;                for (int i = start; I! = end; i = edges[curedges[i]].to) {if (Curflow > EDGES[CUREDGES[I]].W)                    {neck = i;                Curflow = EDGES[CUREDGES[I]].W; }} for (int i = start; I! = end; i = edges[curedges[i]].to) {temp = cured                Ges[i];                EDGES[TEMP].W-= Curflow;            EDGES[TEMP^1].W + = Curflow;            } Flowans + = Curflow;        u = neck;  }      int i;                for (i = curedges[u]; i =-1; i = edges[i].next) if (EDGES[I].W && h[u]==h[edges[i].to]+1)        Break            if (i! =-1) {curedges[u] = i;            Pre[edges[i].to] = u;        u = edges[i].to;            } else {if (0 = =--numh[h[u]]) break;            Curedges[u] = Head[u]; for (temp = N,i = Head[u]; I! =-1; i = edges[i].next) if (EDGES[I].W) temp = min (temp,h[e            Dges[i].to]);            H[u] = temp + 1;            ++numh[h[u]];        if (u! = start) U = pre[u]; }} return Flowans;}    int main () {int n,m,u,v,w;        while (~SCANF ("%d%d", &n,&m)) {memset (head,-1,sizeof (Head));        id = 0;            for (int i = 0; i < N; ++i) {scanf ("%d%d%d", &u,&v,&w);        Addedges (U,V,W);    } printf ("%d\n", SAP (1,m,m)); } return 0;}


POJ1273 drainage ditches "max Stream" "SAP"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.