HLPP感性瞎扯

· · 个人记录

HLPP=侯磊胖胖

大家好,今天我们来扯HLPP算法了。

这是一个大毒瘤网络流算法,能够在O(n^2\sqrt{m})的时间复杂度内跑出一张图的最大流出来。但是,一般的网络流题目考的是建图,而不是丧心病狂的卡常。

闲话少说,正片开始。

关于求最大流,一个直观的想法就是从源点灌流量,死命灌,灌到再也没有一滴流量可以活着到达汇点的时候,你就成功了。

依据增广路定理,这是正确的,因为这时找不到任何一条增广路(不然就有流量能够达到汇点,意味着水没灌够)。我们只需要在灌流量的时候在反向边上同时增加负流量就行。

显然,我们可以就直接暴力灌,对于每个点i维护extra_i,表示这个点当前剩余这么多流量。初始extra_S赋为inf,其它点都赋成0。之后,维护一个队列,初始为S。之后就暴力推进,如果在推进的过程中有点的extra被更新,就将它入队。最后答案即为extra_T。

(其实还有个细节,就是S和T都不能入队。S不能入队是因为一开始S能流出的流量全流走了,接下来S只能接受走投无路想要回到革命根据地的流量,但是不能再流出来。T不能入队则更为直观,长征都结束了,你还想跳入敌人的包围圈?因此一开始不应直接将S赋值并入队,而是将与S有边(S,y,z)相连的点y的extra_y赋成z,并不需要初始化extra_S。)

但是,如果你真就照着上述思路把代码敲出来了,你会发现,它陷入了死循环。

为什么呢?

考虑点x。设有一条边(x,y),则extra_y就可以被x更新。

但是!!!当你更新完y后,反向边(y,x)也获得了流量。这意味着流量可以回流!

怎么办呢?

这时我们就想到了中国人民的老朋友Dinic。

回想一下,在Dinic中,相较于EK,我们引入了高度height概念。只有height_x=height_y+1的点对(x,y),x才有可能被y更新。

那侯磊胖胖算法能怎么应用它呢?

这个时候,我们可以从汇点开始,建立反过来的height数组。这时,只有height_x=height_y+1的点对(x,y),x才有可能将y更新。

注意一字之差,但实现却大大不同。

在反向bfs出height数组后,程序就可以运行了。

在一个点x推过流量后,如果它还有剩余流量(即extra_x不为0),这时我们就可以抬高它的height_x,抬到它所有能推到的点y(即所有使得边(x,y)的剩余流量不为0的点y)的min(height_y)+1。即:height_x=min(height_y)+1。这样就可以保证每次推完之后还能再推(经济发展可持续)。

那为什么就不会产生反复横跳反复推流的情形呢?

因为当两个点反复推流,一直推到height比源点还高的时候,它们就会把流量流回原点!!!

所以我们要初始把height_S赋成n,避免在非反复推流的情况下,别的点就急着吐出流量给源点。

这时候你就认为可以了吗?

\color{Blue}{TLE}

暂且估算一下这个暴力推流的程序的复杂度,就可以发现它肯定不止O(n^2\sqrt{m}),甚至连Dinic都比不上。(至于准确复杂度是多少呢?我也不知道)

怎么办呢?这时候,侯磊胖胖的发明者Tarjan(对,就是那个发明强连通分量tarjan算法、LCAtarjan算法、splay等等的大毒瘤)发话了:

我们可以把暴力中的队列换成以高度为键值的优先队列,每次从队列中取出height最高的点,进行推流。剩下的一模一样。

正确性的瞎扯证明:

显然,某一时刻队列里的所有元素全都是等价的,就跟拓扑排序一样,可以瞎交换位置而不影响正确性。但是,如果我们取height最高的点,它能推出的流量或许比较多(因为比其它点都要高),入队的点也或许比较多(同上),同时自身height的变化也或许比较小(同上)。

因此我们就学完了侯磊胖胖。

哦,另外,同Dinic+gap=ISAP一样,侯磊胖胖也是基于height数组的,也可以进行gap优化。如果对于某个height,其节点数为0的话,显然就不可能再到达汇点了(因为流量只能在height差为1的点对间传递)。因此就可以将该height的最后一个节点的所有关联点的height都赋成n+1。(如果你没有听懂,建议先把ISAP算法学掉。)

如果你现在头脑晕晕乎乎昏昏沉沉像喝了电脑配件一样,不要放弃,我们马上把代码分段解析一下。

I.bfs求出height。

queue<int>Q;
bool bfs(){
    memset(height,0x3f3f3f3f,sizeof(height)),height[T]=0,Q.push(T);
    while(!Q.empty()){
        int x=Q.front();Q.pop();
        for(int i=head[x];i!=-1;i=edge[i].next)if(height[edge[i].to]==0x3f3f3f3f)height[edge[i].to]=height[x]+1,Q.push(edge[i].to);
    }
    return height[S]!=0x3f3f3f3f;
}

如果你学过Dinic,对它应该没有任何陌生感,只是把S改成T而已。

II.推流

priority_queue<pair<int,int> >q;
bool in[N];
void Push_Flow(int x){
    for(int i=head[x],y,pushaway;i!=-1;i=edge[i].next){
        y=edge[i].to;
        if(!edge[i].val||height[edge[i].to]+1!=height[x])continue;//if there is a gap or the edge has been filled, then we shouldn't push flow.
        pushaway=min(edge[i].val,extra[x]);//we can push away such much flow
        edge[i].val-=pushaway,edge[i^1].val+=pushaway,extra[x]-=pushaway,extra[y]+=pushaway;//modifying the remaining flow of each edges and nodes
        if(y!=S&&y!=T&&!in[y])q.push(make_pair(height[y],y)),in[y]=true;//if y isn't S or T, and it is not in the priority_queue, push it in
        if(!extra[x])return;//the flow of node x has been run out of.
    }
}

也比较好理解。能推就推,推不了(有断层)跳过,推完了退出。

III.重标号(就是提升一个节点的height)。


void Relabel(int x){
    height[x]=0x3f3f3f3f;
    for(int i=head[x];i!=-1;i=edge[i].next)if(edge[i].val)height[x]=min(height[x],height[edge[i].to]+1);
}

如果你看懂了如何提升height的部分,理解它应该不困难。

IV.主函数


int HLPP(){
    if(!bfs())return 0;//if S and T are initially unconnected, the max flow is clearly 0.
    height[S]=n;
    for(int i=1;i<=n;i++)if(height[i]!=0x3f3f3f3f)gap[height[i]]++;//counting the amounts of each height
    for(int i=head[S],y,pushaway;i!=-1;i=edge[i].next){//push from S, a little difference between the Push_Flow fuction.
        y=edge[i].to;
        if(!edge[i].val)continue;
        pushaway=edge[i].val;//JUST PUSH!!! S initially has infinite flow.
        edge[i].val-=pushaway,edge[i^1].val+=pushaway,extra[S]-=pushaway,extra[y]+=pushaway;
        if(y!=S&&y!=T&&!in[y])q.push(make_pair(height[y],y)),in[y]=true;
    }
    while(!q.empty()){//when it can still be updated:
        int x=q.top().second;q.pop();
        in[x]=false,Push_Flow(x);
        if(!extra[x])continue;//if x's flow had all been pushed away, break.
        if((--gap[height[x]])==0){//if such gap has no nodes:
            for(int i=1;i<=n;i++)if(i!=S&&i!=T&&height[i]>height[x]&&height[i]<n+1)height[i]=n+1;//assign the related nodes to UNREACHABLE(n+1) 
        }
        Relabel(x),gap[height[x]]++,q.push(make_pair(height[x],x)),in[x]=true;//push x back to the priority_queue
    }
    return extra[T];
}

应该比较清晰易懂吧……当一个点的height>n时(不管是n+1还是0x3f3f3f3f),都不会存在一条增广路(因为它最多只有n个节点,则最大height只能到n)。

V.大礼包:

#include<bits/stdc++.h>
using namespace std;
const int N=1210;
int n,m,head[N],cnt,extra[N],S,T,height[N],gap[N<<1];
struct node{
    int to,next,val;
}edge[240010];
void ae(int u,int v,int w){
    edge[cnt].next=head[u],edge[cnt].to=v,edge[cnt].val=w,head[u]=cnt++;
}
priority_queue<pair<int,int> >q;
queue<int>Q;
bool in[N];
bool bfs(){
    memset(height,0x3f3f3f3f,sizeof(height)),height[T]=0,Q.push(T);
    while(!Q.empty()){
        int x=Q.front();Q.pop();
        for(int i=head[x];i!=-1;i=edge[i].next)if(height[edge[i].to]==0x3f3f3f3f)height[edge[i].to]=height[x]+1,Q.push(edge[i].to);
    }
    return height[S]!=0x3f3f3f3f;
}
void Push_Flow(int x){
    for(int i=head[x],y,pushaway;i!=-1;i=edge[i].next){
        y=edge[i].to;
        if(!edge[i].val||height[edge[i].to]+1!=height[x])continue;//if there is a gap or the edge has been filled, then we shouldn't push flow.
        pushaway=min(edge[i].val,extra[x]);//we can push away such much flow
        edge[i].val-=pushaway,edge[i^1].val+=pushaway,extra[x]-=pushaway,extra[y]+=pushaway;//modifying the remaining flow of each edges and nodes
        if(y!=S&&y!=T&&!in[y])q.push(make_pair(height[y],y)),in[y]=true;//if y isn't S or T, and it is not in the priority_queue, push it in
        if(!extra[x])return;//the flow of node x has been run out of.
    }
}
void Relabel(int x){
    height[x]=0x3f3f3f3f;
    for(int i=head[x];i!=-1;i=edge[i].next)if(edge[i].val)height[x]=min(height[x],height[edge[i].to]+1);
}
int HLPP(){
    if(!bfs())return 0;//if S and T are initially unconnected, the max flow is clearly 0.
    height[S]=n;
    for(int i=1;i<=n;i++)if(height[i]!=0x3f3f3f3f)gap[height[i]]++;//counting the amounts of each height
    for(int i=head[S],y,pushaway;i!=-1;i=edge[i].next){//push from S, a little difference between the Push_Flow fuction.
        y=edge[i].to;
        if(!edge[i].val)continue;
        pushaway=edge[i].val;//JUST PUSH!!! S initially has infinite flow.
        edge[i].val-=pushaway,edge[i^1].val+=pushaway,extra[S]-=pushaway,extra[y]+=pushaway;
        if(y!=S&&y!=T&&!in[y])q.push(make_pair(height[y],y)),in[y]=true;
    }
    while(!q.empty()){//when it can still be updated:
        int x=q.top().second;q.pop();
        in[x]=false,Push_Flow(x);
        if(!extra[x])continue;//if x's flow had all been pushed away, break.
        if((--gap[height[x]])==0){//if such gap has no nodes:
            for(int i=1;i<=n;i++)if(i!=S&&i!=T&&height[i]>height[x]&&height[i]<n+1)height[i]=n+1;//assign the related nodes to UNREACHABLE(n+1) 
        }
        Relabel(x),gap[height[x]]++,q.push(make_pair(height[x],x)),in[x]=true;//push x back to the priority_queue
    }
    return extra[T];
}
int main(){
    scanf("%d%d%d%d",&n,&m,&S,&T),memset(head,-1,sizeof(head));
    for(int i=1,x,y,z;i<=m;i++)scanf("%d%d%d",&x,&y,&z),ae(x,y,z),ae(y,x,0);
    printf("%d\n",HLPP());
    return 0;
}

由于我的侯磊胖胖是看着Mr_Spade巨佬的博客学习的,码风可能比较相似,但本博客的内容属于原创。

总结:

侯磊胖胖果然是毒瘤算法。但是考试用Dinic就行了。

完结撒Tarjan~~~