【模板】有负圈的费用流
题目跳转
-
对于所有负权边,建下界为 \(0\)、上界为 \(f\)、费用为 \(−v\) 的边 \((y,x)\);同时建一条虚拟边:上界为 \(f\)、下界为\(f\),按照上下界网络流套路统计每个点入流量和出流量的差(设为 \(w\))。
-
对于所有正权边,建下界为 \(0\)、上界为 \(f\)、费用为 \(v\) 的边 \((x,y)\)。
-
建立超级源点 \(S\) 和 超级汇点 \(T\)。若 \(w_i\gt 0\):加入容量为 \(w_i\) 的边 \((S,i)\);\(w_i\lt 0\):加入容量为 \(−w_i\) 的边 \((i,T)\)。
-
加入容量为 \(inf\) 的边 \((t,s)\),在图上跑以 \(S\) 为源点,\(T\) 为汇点的最小费用最大流(求解有源汇最小费用可行流)。将可行流流量(最后加入的边 (t,s) 的流量)计入答案流量,将此次最小费用最大流的费用计入答案费用。
-
撤去最后加入的边 \((t,s)\)。跑一次以 \(s\) 为源点,\(t\) 为汇点的最小费用最大流。将此次最小费用最大流的流量和费用计入答案流量和答案费用。
答案流量为可行流流量与最小费用最大流流量之和,答案费用为强制满流的费用、可行流的费用与最小费用最大流的费用之和。
代码
#include<bits/stdc++.h>
using namespace std;
typedef int ll;
typedef pair<ll,ll> pll;
uint8_t buf[1<<20],*p1,*p2;
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
ll rd(){
ll x=0,f=1,ch=gc();
while(ch!=EOF&&(ch<'0'||ch>'9')){
if(ch=='-'){
f=-1;
}
ch=gc();
}
if(ch==EOF){
return x;
}
while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+(ch-'0');
ch=gc();
}
return x*f;
}
uint8_t obuf[1<<20],*op=obuf;
#define fls() (fwrite(obuf,1,op-obuf,stdout),op=obuf,0)
#define pc(ch) (op==obuf+(1<<20)?fls():0,*op++=(ch))
void write(ll x){
if(x<0){
x=-x;
pc('-');
}
if(x>9){
write(x/10);
}
pc(x%10+'0');
}
const ll maxn=2e2+5,maxm=1.04e4+5,inf=1e9;
ll n,m,ss,tt,s,t,ecnt=1,head[maxn],now[maxn],dis[maxn],h[maxn],d[maxn],res1,res2,ans1,ans2;
bool vis[maxn];
queue<ll> q;
priority_queue<pll,vector<pll>,greater<pll>> pq;
struct node{
ll nxt,to,lim,val;
}e[maxm<<1];
void adde(ll u,ll v,ll w,ll c){
e[++ecnt].nxt=head[u];
e[ecnt].to=v;
e[ecnt].lim=w;
e[ecnt].val=c;
head[u]=ecnt;
e[++ecnt].nxt=head[v];
e[ecnt].to=u;
e[ecnt].lim=0;
e[ecnt].val=-c;
head[v]=ecnt;
}
void spfa(){
for(ll i=0;i<=n+1;i++){
h[i]=inf;
}
q.push(s);
vis[s]=true;
h[s]=0;
while(!q.empty()){
ll x=q.front();
q.pop();
vis[x]=false;
for(ll i=head[x];i;i=e[i].nxt){
ll y=e[i].to;
if(e[i].lim>0&&h[y]>h[x]+e[i].val){
h[y]=h[x]+e[i].val;
if(!vis[y]){
vis[y]=true;
q.push(y);
}
}
}
}
}
bool dijkstra(){
for(ll i=0;i<=n+1;i++){
vis[i]=false;
dis[i]=inf;
}
pq=priority_queue<pll,vector<pll>,greater<pll>>();
pq.push({0,s});
dis[s]=0;
while(!pq.empty()){
ll x=pq.top().second;
pq.pop();
if(!vis[x]){
vis[x]=true;
for(ll i=head[x];i;i=e[i].nxt){
ll y=e[i].to;
if(e[i].lim>0&&dis[y]>dis[x]+e[i].val+h[x]-h[y]){
dis[y]=dis[x]+e[i].val+h[x]-h[y];
pq.push({dis[y],y});
}
}
}
}
return dis[t]!=inf;
}
ll dfs(ll x,ll sum){
if(x==t){
return sum;
}
ll res=0,k;
vis[x]=true;
for(ll i=now[x];i&∑i=e[i].nxt){
now[x]=i;
ll y=e[i].to;
if(e[i].lim>0&&!vis[y]&&h[y]==h[x]+e[i].val){
k=dfs(y,min(sum,e[i].lim));
e[i].lim-=k;
e[i^1].lim+=k;
sum-=k;
res+=k;
res2+=k*e[i].val;
}
}
vis[x]=false;
return res;
}
void dinic(){
res1=res2=0;
spfa();
while(dijkstra()){
for(ll i=0;i<=n+1;i++){
vis[i]=false;
now[i]=head[i];
if(dis[i]!=inf){
h[i]+=dis[i];
}
}
res1+=dfs(s,inf);
}
}
int main(){
n=rd();
m=rd();
ss=rd();
tt=rd();
t=n+1;
for(ll i=1;i<=m;i++){
ll u=rd(),v=rd(),f=rd(),c=rd();
if(f){
if(c<0){
d[u]-=f;
d[v]+=f;
ans2+=f*c;
adde(v,u,f,-c);
}else{
adde(u,v,f,c);
}
}
}
for(ll i=1;i<=n;i++){
if(d[i]>0){
adde(s,i,d[i],0);
}else if(d[i]<0){
adde(i,t,-d[i],0);
}
}
adde(tt,ss,inf,0);
dinic();
ans1=e[ecnt].lim;
ans2+=res2;
e[ecnt].lim=e[ecnt^1].lim=0;
s=ss;
t=tt;
dinic();
ans1+=res1;
ans2+=res2;
write(ans1);
pc(' ');
write(ans2);
fls();
return 0;
}
Goodbye!

浙公网安备 33010602011771号