[Network stream] [update TEMPLATE] dinic multi-channel augmented + current arc

Source: Internet
Author: User

Add Current Arc Optimization

#define INF (1<<30)#define N 101000#define M 200010#pragma comment(linker, "/STACK:32000000")int n,m,s,t;int head[N],cur[N],cnt;struct edge{    int v,w,next;}e[M];int q[N],dis[N],start,end;void addedge(int u,int v,int w){e[cnt]=(edge){v,w,head[u]}; head[u]=cnt++;e[cnt]=(edge){u,0,head[v]}; head[v]=cnt++;}int bfs(){    memset(dis,-1,sizeof(dis));    start=end=0; q[0]=t; dis[t]=0;    while (start<=end)    {        int u=q[start++];        for(int i=head[u];i!=-1;i=e[i].next)        if(dis[e[i].v]==-1 && e[i^1].w>0){  dis[e[i].v]=dis[u]+1; q[++end]=e[i].v;  }    }    return dis[s]!=-1;}int dfs(int u,int t,int flow){    if(u==t) return flow;    int delta=flow;    for(int &i=cur[u];i!=-1;i=e[i].next)        if(dis[u]==dis[e[i].v]+1 && e[i].w>0)        {            int d=dfs(e[i].v,t,min(delta,e[i].w));            e[i].w-=d;    e[i^1].w+=d;   delta-=d;            if(delta==0) break;        }    return flow-delta;}int ans0=0;while (bfs()){for(int i=1;i<=n;i++) cur[i]=head[i];ans0+=dfs(s,t,INF);}

The speed is strange. POJ3469 is slower than the previous pre-stream push, but the HDU4280 can speed up to 4000 +. Because it is recursive, pay attention to stack explosion.

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.