Network flow isap

For network flow, high-end office is very useful.
isap is in addition to promoting best reserved for network streaming (feel), after joining gap optimization, ran out of efficiency is not bad.
As for the basic operation is the use of isap d [node] = d [pre ] +1 This property is optimal. Also be augmented by algorithm method.
The gap optimization, the hierarchical graph is updated when the record of the number of nodes. If the node number is zero, then there will be no augmenting path, and that is, you can quit.

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#define For(aa,bb,cc) for(int aa=bb;aa<=cc;++aa)
using namespace std;
const int maxn=1010,inf=1e10;
int n,m,s,t;
int be[maxn],ne[maxn],w[maxn],to[maxn],e;
int d[maxn],gap[maxn];

void add(int x,int y,int z){
    to[++e]=y,ne[e]=be[x],be[x]=e,w[e]=z;
}

int isap(int node,int flow){
    if(node==t) return flow;
    int res=flow;
    for(int i=be[node];i;i=ne[i]){
        int u=to[i];
        if(w[i]>0 && d[node]==d[u]+1){
            int minflow=isap(u,min(w[i],res));
            w[i]-=minflow;
            w[i^1]+=minflow;
            res-=minflow;
            if(!res) return flow;
        }
    }
    if(!(--gap[d[node]])) d[s]=n;
    ++gap[++d[node]];
    return flow-res;
}

void work(){
    e=1;
    scanf("%d%d",&m,&n);
    s=1,t=n;
    For(i,1,m){
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        add(x,y,z),add(y,x,0);
    }
    int maxflow=0;
    for(gap[0]=n;d[s]<n;){
        maxflow+=isap(s,inf);
    }
    printf("%d\n",maxflow);
}

int main(){
    work();
    return 0;
}
Published 51 original articles · won praise 6 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_35776579/article/details/54914305