上下界网络流(费用流)模板

· · 个人记录

上下界网络流:

#include<bits/stdc++.h>
using namespace std;
using type=int;
const type inf=(1<<30);
#define debug cerr<<x<<" "<<y<<" "<<flow<<endl;
const int MaxN=1e5+5,MaxM=1e6+5;
struct edge{
    int next,to;
    type flow;
}e[MaxM<<1];
int head[MaxN],cnt=1;
inline void ADD(int x,int y,type flow){
    e[++cnt]={head[x],y,flow};
    head[x]=cnt;
}
inline void add(int x,int y,type flow){
    ADD(x,y,flow);
    ADD(y,x,0);
}
int dep[MaxN],cur[MaxN];
inline bool bfs(int s,int t){
    queue<int>q;
    q.push(s);
    memset(dep,0,sizeof(dep));
    cur[s]=head[s];
    dep[s]=1;
    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(dep[y]||!e[i].flow){
                continue;
            }
            dep[y]=dep[x]+1;
            cur[y]=head[y];
            q.push(y);
            if(y==t){
                return 1;
            }
        }
    }
    return 0;
}
inline type dfs(int x,type flow,int t){
    if(x==t||!flow){
        return flow;
    }
    type limit,res=0;
    for(int i=cur[x];i&&flow;i=e[i].next){
        cur[x]=i;
        int y=e[i].to;
        if(dep[y]==dep[x]+1&&e[i].flow){
            limit=dfs(y,min(flow,e[i].flow),t);
            if(!limit){
                dep[y]=0;
            }else{
                e[i].flow-=limit;
                e[i^1].flow+=limit;
                res+=limit;
                flow-=limit;
            }
        }
    }
    return res;
}
inline type dinic(int s,int t){
    type res=0;
    while(bfs(s,t)){
        res+=dfs(s,inf,t);
    }
    return res;
}
inline void init(){
    memset(head,0,sizeof(head));
    cnt=1;
}
int s,t,S,T,n,m,N;
type deg[MaxN],Able;
inline void addedge(int x,int y,type l,type r){
    assert(l<=r);
    deg[x]-=l;
    deg[y]+=l;
    add(x,y,r-l);
}
inline bool able(){
    for(int i=1;i<=N;i++){
        if(deg[i]>0){
            Able+=deg[i];
            add(S,i,deg[i]);
        }else if(deg[i]<0){
            add(i,T,-deg[i]);
        }
    }
    add(t,s,inf);
    return Able==dinic(S,T);
}
inline type maxflow(){
    assert(able());
    type res=e[cnt].flow;
    e[cnt].flow=e[cnt^1].flow=0;
    return res+dinic(s,t);
}
inline type minflow(){
    assert(able());
    type res=e[cnt].flow;
    e[cnt].flow=e[cnt^1].flow=0;
    return res-dinic(t,s);
}
inline void build(){
    cin>>n>>m;
}
int main(){
    S=MaxN-1,T=MaxN-2;
    s=MaxN-3,t=MaxN-4;
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    build();
    return 0;
}

上下界费用流(可行流):

#include<bits/stdc++.h>
using namespace std;
using type1=int;
using type2=int;
const type1 inf=(1<<30);
const type2 INF=(1<<30);
#define debug cerr<<x<<" "<<y<<" "<<flow<<" "<<cost<<endl;
const int MaxN=5e3+5,MaxM=1e5+5;
struct edge{
    int next,to;
    type1 flow;
    type2 cost;
}e[MaxM<<1];
int head[MaxN],cnt=1;
inline void ADD(int x,int y,type1 flow,type2 cost){
    e[++cnt]={head[x],y,flow,cost};
    head[x]=cnt;
}
inline void add(int x,int y,type1 flow,type2 cost){
    ADD(x,y,flow,cost);
    ADD(y,x,0,-cost);
}
type2 dis[MaxN];
int cur[MaxN];
bitset<MaxN>vis;
inline bool spfa(int s,int t){
    queue<int>q;
    q.push(s);
    fill(dis,dis+MaxN,INF);
    cur[s]=head[s];
    vis[s]=1;
    dis[s]=0;
    while(!q.empty()){
        int x=q.front();
        q.pop();
        vis[x]=0;
        for(int i=head[x];i;i=e[i].next){
            int y=e[i].to;
            if(e[i].flow&&dis[y]>dis[x]+e[i].cost){
                dis[y]=dis[x]+e[i].cost;
                cur[y]=head[y];
                if(!vis[y]){
                    vis[y]=1;
                    q.push(y);
                }
            }
        }
    }
    return dis[t]!=INF;
}
inline pair<type1,type2> dfs(int x,type1 flow,int t){
    if(x==t||!flow){
        return {flow,0};
    }
    vis[x]=1;
    type1 res=0;
    type2 Cost=0;
    for(int i=cur[x];i&&flow;i=e[i].next){
        int y=e[i].to;
        cur[x]=i;
        if(!vis[y]&&e[i].flow&&dis[y]==dis[x]+e[i].cost){
            auto [limit1,limit2]=dfs(y,min(e[i].flow,flow),t);
            if(!limit1){
                dis[y]=0;
            }else{
                e[i].flow-=limit1;
                e[i^1].flow+=limit1;
                Cost+=limit1*e[i].cost+limit2;
                res+=limit1;
                flow-=limit1;
            }
        }
    }
    vis[x]=0;
    return {res,Cost};
}
inline pair<type1,type2> dinic(int s,int t){
    type1 maxflow=0;
    type2 mincost=0;
    while(spfa(s,t)){
        auto [x,y]=dfs(s,inf,t);
        maxflow+=x;
        mincost+=y;
    }
    return {maxflow,mincost};
}
int s,t,S,T,n,m,N;
type1 deg[MaxN],Able;
inline void init(){
    memset(head,0,sizeof(head));
    memset(deg,0,sizeof(deg));
    cnt=1;
    Able=0;
}
inline void addedge(int x,int y,type1 l,type1 r,type2 cost){
    assert(l<=r);
    deg[x]-=l;
    deg[y]+=l;
    add(x,y,r-l,cost);
}
inline pair<int,int> mincost(){
    for(int i=1;i<=N;i++){
        if(deg[i]>0){
            Able+=deg[i];
            add(S,i,deg[i],0);
        }else{
            add(i,T,-deg[i],0);
        }
    }
    add(t,s,inf,0);
    auto [able,cost]=dinic(S,T);
    assert(able==Able);
    return {able,cost};
}
inline void build(){
    cin>>n>>m;
}
int main(){
    S=MaxN-2,T=MaxN-1;
    t=MaxN-3,s=MaxN-4;
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    build();
    return 0;
}