POJ 1273-drainage Ditches (network flow _ Maximum Flow _isap () algorithm and EK () algorithm) __POJ

Source: Internet
Author: User

Drainage ditches

Time Limit: 1000MS Memory Limit: 10000K
Total submissions: 58538 accepted: 22485

Description Every time it rains on farmer John's fields, a pond forms over Bessie ' s favorite clover. This means the clover is covered by water for awhile and takes quite a long. Thus, farmer John has built a set of drainage ditches so this Bessie ' s clover Patch is never covered in water. Instead, the water is drained to a nearby stream. Being Ace engineer, farmer John has also installed regulators at the beginning of each ditch Rate water flows into that ditch.
Farmer John knows not a many gallons of water each ditch can transport per minute but also the exact layout of the Ditches, which feeds out of the pond and into each other and stream in a potentially complex network.
Given All this information, determine the maximum rate in which water can be transported out of the pond Am. For any given ditch, water flows in only one direction, but there to be might to be a way that water can flow in a circle.

Input the input includes several cases. For each case, the contains two space-separated integers, n (0 <= n <=) 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'll flow through this ditch from Si to Ei. CI (0 <= ci <= 10,000,000) is the maximum rate at which water'll flow through the ditch.

Output for each case, output a single integer, the maximum rate at which water could emptied from the pond.

Sample Input

5 4
1 2/
1 4
2 4 2 3 3 4
10

Sample Output

50
There are N roads, m points, let you find 1 as the source point, M is the maximum flow of the meeting point;
Train of thought: A typical algorithm for maximum flow graced path. When and only when the S-T path (augmented path) does not exist in the residual network, the current flow is the maximum flow from S to t;

ISAP ()

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> #include <
iostream> #include <queue> #include <set> using namespace std;
const int inf=0x3f3f3f3f;
Head store forward star with the same starting arc, NUM store the number of remaining nodes in each layer, D store the number of nodes in the layer, cur store the current arc, the pre storage path, that is, the previous node of the current node, used for backtracking.
int head[1010],num[1010],d[1010],cur[1010],n,m,cnt,s,t,q[100010],nv,pre[1010];
int maxint=inf;
    struct node {int v,cap;
int next;
}EDGE[10010];
    void Add (int u,int v,int cap) {//Gazheng to Arc edge[cnt].v=v;
    Edge[cnt].cap=cap;
    Edge[cnt].next=head[u];
    head[u]=cnt++;
    Add reverse arc edge[cnt].v=u;
    Edge[cnt].cap=0;
    EDGE[CNT].NEXT=HEAD[V];
head[v]=cnt++;
    } void BFs ()///BFS each node with a layer {////algorithm needs to initialize the D array with BFS, from T to s reverse to int i,j;
    memset (num,0,sizeof (num));
    Memset (d,-1,sizeof (d));
    int f1=0,f2=0;
    q[f1++]=t;//from D[t]=0;
    Num[0]=1;
        while (F2&LT;=F1) {int u=q[f2++];
            for (I=head[u];i!=-1;i=edge[i].next) {int v=edge[i].v;
            if (d[v]!=-1) continue;
        d[v]=d[u]+1;//the number of layers of this node is the next layer of the previous node num[d[v]]++;//the number of nodes in the layer +1 q[f1++]=v;
    }} void Isap () {memcpy (cur,head,sizeof (cur));
    BFS ();
    int flow=0,u=pre[s]=s,i;
            while (D[S]&LT;NV) {//if d[s] is greater than NV, indicates a fault if (u==t) {int f=maxint,pos; for (I=S;I!=T;I=EDGE[CUR[I]].V) {//Find the minimum residual flow on the road if (F&GT;EDGE[CUR[I]].CAP) {F=edge[cur[i]]
                    . Caps;
                Pos=i;
                } for (I=S;I!=T;I=EDGE[CUR[I]].V) {//update, will all traffic on the road-least participate in traffic edge[cur[i]].cap-=f;
            Edge[cur[i]^1].cap+=f;
        flow+=f;//adding total flow u=pos; for (I=cur[u];i!=-1;i=edge[i].next) {if (d[edge[i].v]+1==d[u]&&edge[i].cap) {b
            Reak; } if (i!=-1) {//If a viable augmented path is found, update cur[u]=i;//update the current arc pre[edge[i].v]=u;//update path u=edge[i].v;//backtracking} else{if (--num[d[u]]==0) break;//gap Optimizing int
            MIND=NV;
                    for (I=head[u];i!=-1;i=edge[i].next) {//Find minimum level if (EDGE[I].CAP&AMP;&AMP;MIND&GT;D[EDGE[I].V]) {
                    Cur[u]=i;
                MIND=D[EDGE[I].V];
            }} d[u]=mind+1;
            num[d[u]]++;
        U=pre[u];
} printf ("%d\n", flow);
    int main () {int i,a,b,c;
        while (~SCANF ("%d%d", &n,&m)) {memset (head,-1,sizeof);
        cnt=0;
        while (n--) {scanf ("%d%d%d", &a,&b,&c);//Add edge Add (a,b,c);
        } S=1;
        T=m;
        nv=t+1;
    ISAP ();
return 0;
 }

EK ()

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> #include <
Queue> using namespace std;
#define INF 9999999999 int flow[210][210];
int maxflow[210],father[210],vis[210];
int max_flow;
int m,i;
    void EK (int s,int e) {queue<int >q;
    int u,v;
        while (1) {memset (maxflow,0,sizeof (Maxflow)), the inflow capacity of each point is set to 0 for each search of the augmented path; memset (vis,0,sizeof (VIS)); The inflow of the maxflow[s]=inf;//source point is set to positive infinity; Q.push (s);//press the source point into the queue; while (!q.empty ())//When the queue is not empty {u=q
            . Front ();
            Q.pop ();
                    for (v=s;v<=e;v++) {if (!vis[v]&&flow[u][v]>0) {
                    Vis[v]=1;
                    Father[v]=u;//records His father conveniently reverse update q.push (v); Maxflow[v]=min (Maxflow[u],flow[u][v])//The capacity of the current point is the lesser of the father point capacity and the side traffic} if (maxflow[e]> 0)//If the meeting point is found and the meeting point capacity is not 0, empty the queue {
                while (!q.empty ()) Q.pop ();
            Break
        } if (maxflow[e]==0)//has not been found to the meeting point of the Grace Road, the exit of the entire cycle break; for (I=e;i!=s;i=father[i]) {flow[father[i]][i]-=maxflow[e];//forward update flow[i][father[i]]+=maxf
    low[e];//Reverse Update} max_flow+=maxflow[e];//update max stream} int main () {int n;
    int si,ei,ci;
        while (~SCANF ("%d%d", &n,&m)) {max_flow=0;//maximum stream initialization; memset (flow,0,sizeof (flow));
            for (i=0;i<n;i++) {scanf ("%d%d%d", &si,&ei,&ci);
        Flow[si][ei]+=ci;
        EK (1,M);
    printf ("%d\n", Max_flow);
 }
}


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.