HLPP模板

· · 个人记录

#include<bits/stdc++.h>
using namespace std;
#define debug cerr<<x<<" "<<y<<" "<<flow<<endl;
const int MaxN=1.2e3+5,MaxM=1.2e5+5,inf=INT_MAX;
const long long INF=LLONG_MAX;
#define huihe int
int n,m,s,t,LIMIT;
struct edge{
    int next,to;
    huihe flow;
}e[MaxM<<1];
int head[MaxN],cnt=1;
inline void ADD(int x,int y,huihe flow){
    e[++cnt]={head[x],y,flow};
    head[x]=cnt;
}
inline void add(int x,int y,huihe flow){
    ADD(x,y,flow);
    ADD(y,x,0);
}
vector<huihe>flow;
vector<int>h,gap;
int maxh,nowh;
vector<int>height[MaxN];
list<int>num[MaxN];
list<int>::iterator it[MaxN];
inline void relabel(){
    h.assign(LIMIT,n);
    gap.assign(n+1,0);
    queue<int>q;
    h[t]=0;
    q.push(t);
    while(!q.empty()){
        int x=q.front();
        q.pop();
        for(int i=head[x];i;i=e[i].next){
            int y=e[i].to;
            if(h[y]==n&&e[i^1].flow){
                h[y]=h[x]+1;
                gap[h[y]]++;
                q.push(y);
            }
        }
        maxh=nowh=h[x];
    }
    for(int i=0;i<=n;i++){
        height[i].clear();
        num[i].clear();
    }
    for(int i=1;i<=n;i++){
        if(h[i]<n){
            it[i]=num[h[i]].insert(num[h[i]].begin(),i);
            if(flow[i]){
                height[h[i]].push_back(i);
            }
        }
    }
}
inline void update(int x,int y,int i){
    huihe res=min(flow[x],e[i].flow);
    e[i].flow-=res;
    e[i^1].flow+=res;
    flow[x]-=res;
    flow[y]+=res;
    if(flow[y]&&flow[y]<=res){
        height[h[y]].push_back(y);
    }
}
inline void push(int x){
    int hgt=n;
    for(int i=head[x];i;i=e[i].next){
        int y=e[i].to;
        if(e[i].flow){
            if(h[x]==h[y]+1){
                update(x,y,i);
                if(flow[x]<=0){
                    return ;
                }
            }else{
                hgt=min(hgt,h[y]+1);
            }
        }
    }
    if(gap[h[x]]==1){
        for(int i=h[x];i<=maxh;i++){
            for(auto y:num[i]){
                gap[h[y]]--;
                h[y]=n;
            }
            num[i].clear();
        }
        maxh=h[x]-1;
    }else{
        gap[h[x]]--;
        it[x]=num[h[x]].erase(it[x]);
        h[x]=hgt;
        if(hgt==n){
            return ;
        }
        gap[hgt]++;
        it[x]=num[hgt].insert(num[hgt].begin(),x);
        nowh=hgt;
        maxh=max(maxh,nowh);
        height[hgt].push_back(x);
    }
}
inline huihe HLPP(){
    maxh=nowh=0;
    h.assign(LIMIT,0);
    h[s]=n;
    for(int i=1;i<=n;i++){
        if(i!=s){
            it[i]=num[h[i]].insert(num[h[i]].begin(),i);
        }
    }
    gap.assign(n+1,0);
    gap[0]=n-1;
    flow.assign(LIMIT,0);
    flow[s]=inf;
    flow[t]=-inf;
    for(int i=head[s];i;i=e[i].next){
        update(s,e[i].to,i);
    }
    relabel();
    while(nowh){
        while(!height[nowh].empty()){
            int x=height[nowh].back();
            height[nowh].pop_back();
            push(x);
        }
        nowh--;
    }
    return flow[t]+inf;
}
inline void init(){
    memset(head,0,sizeof(head));
    cnt=1;
}
inline void build(){
    cin>>n>>m;
    LIMIT=max(n+1,t);
    cout<<HLPP()<<'\n';
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    t=MaxN-1;
    build();
    return 0;
}