费用流模板

· · 个人记录

最大费用:

#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 maxcost=0;
    while(spfa(s,t)){
        auto [x,y]=dfs(s,inf,t);
        maxflow+=x;
        maxcost+=y;
    }
    return {maxflow,maxcost};
}
inline void init(){
    memset(head,0,sizeof(head));
    cnt=1;
}
int n,m,s,t;
inline void build(){
    cin>>n>>m;
    auto [flow,cost]=dinic(s,t);
    cout<<flow<<" "<<cost<<"\n";
}
int main(){
    t=MaxN-1;
    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};
}
inline void init(){
    memset(head,0,sizeof(head));
    cnt=1;
}
int n,m,s,t;
inline void build(){
    cin>>n>>m;
    auto [flow,cost]=dinic(s,t);
    cout<<flow<<" "<<cost<<"\n";
}
int main(){
    t=MaxN-1;
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    build();
    return 0;
}