Network maximum flow algorithm-highest label reserved propulsion HLPP

Source: Internet
Author: User
Tags stdin

Spit Groove

This algorithm:

How to say ...

You learn to pretend to be 13 ....

is more ugly than EK.

Run slower than EK

Written more difficult than Ek

Thought

Let 's start by guessing the idea of this algorithm : joy:

Look at someone's name--the highest mark reserved advance

What high-end atmospheric grade 2333333 cough

From its name we can see that its core idea is-to advance , not to find the augmented road

So how does it go forward?

Very simple, we start from the source point, and constantly to the other points of the flow, for each point to do so. So push to the end, we can get the maximum flow to the meeting point.

However, there may be a situation, that is, $a$ send traffic to $b$, $B $ feel embarrassed do not want, and then pushed to $a$, $A $ very enthusiastic and then pushed to $b$ ... Until it is pushed to the tle. So how do we solve this situation?

We introduce a high $h$ to each point, and specify that a point $u$ can $v$ traffic to another point, when and only if $h[u]=h[s]+1$

So we can make sure it doesn't happen.

In addition, there is a situation, that is, the point is still flow, but due to the height of the limit flow does not go out, how to do?

Very simply, we increase the height of this point so that the flow of this point can be streamed out.

Optimization

Reserved propulsion, that's what it is.

But what does the highest mark in its name mean?

Thanks to our acquaintance, Tarjan, he and his little friends found that the time complexity would be better if the point of each selection was the highest point.

can be optimized to $o (N^2\sqrt{m}) $

There is also a more obvious optimization, if a height $i$ is not present, that is, the figure does not have a height of $i$ points, then from the point higher than $i$ will not go to the meeting point $t$, because according to our restrictions, must go through the height of the $i$ point, so these points are useless

Code

Here's the question.

Not that this algorithm really is dead slow,,,,

#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<queue>using namespacestd;Const intmaxn=2*1e3+Ten;Const intinf=1e8+Ten; inlineCharNC () {Static CharBuf[maxn],*p1=buf,*p2=buf; returnp1==p2&& (p2= (p1=buf) +fread (buf,1, Maxn,stdin), P1==P2)? eof:*p1++;} InlineintRead () {CharC=NC ();intx=0, f=1;  while(c<'0'|| C>'9'){if(c=='-') f=-1; c=NC ();}  while(c>='0'&&c<='9') {x=x*Ten+c-'0'; c=NC ();} returnx*F;}intn,m,s,t;intH[MAXN];//the height of each nodeintF[MAXN];//traffic that can flow from each nodeintGAP[MAXN];//the number of each heightstructnode{intU,V,FLOW,NXT;} EDGE[MAXN];intHEAD[MAXN];intnum=0;//Note here num must start from 0InlinevoidAdd_edge (intXintYintz) {edge[num].u=x; EDGE[NUM].V=y; Edge[num].flow=Z; EDGE[NUM].NXT=Head[x]; HEAD[X]=num++;} InlinevoidAddedge (intXintYintz)    {Add_edge (x, y, z); Add_edge (Y,x,0);//look, don't forget to add the reverse side here.}structcomp{intpos,h; Comp (intpos=0,intH=0):p OS (POS), H (h) {} inlineBOOL operator< (ConstComp &a)Const{returnh<A.h;} };p riority_queue<comp>Q;BOOLWork (intUintVintID) {    intVal=min (f[u],edge[id].flow); Edge[id].flow-=val;edge[id^1].flow+=Val; F[u]-=val; f[v]+=Val; returnVal;} Inlineinthlpp () {H[s]=n; f[s]=Inf;q.push (Comp (s,h[s));  while(Q.size ()! =0)    {        intp=q.top (). Pos;q.pop (); if(! F[P])Continue;  for(inti=head[p];i!=-1; i=edge[i].nxt)if((p==s| | h[edge[i].v]+1==H[P]) && work (p,edge[i].v,i) && edge[i].v!=s && edge[i].v!=T) Q.push (Comp (edge[i].v,h[edge[i].v)); if(P!=s && p!=t &&F[p]) {            if((--gap[h[p]) = =0)//the height does not exist            {                 for(intI=1; i<=n;i++)                    if(h[p]T) H[i]=n+1;//set to not accessible            }            ++gap[++h[p]];//height +1Q.push (Comp (p,h[p)); }    }    returnf[t];}intMain () {#ifdef WIN32 freopen ("a.in","R", stdin); #else    #endif memset (Head,-1,sizeof(head)); N=read (), M=read (), S=read (), t=read ();  for(intI=1; i<=m;i++)    {        intX=read (), Y=read (), z=read ();     Addedge (x, y, z); } printf ("%d", HLPP ()); return 0;}

Network maximum flow algorithm-highest label reserved propulsion HLPP

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.