上下界网络流总结

前言

最近发现网络流很菜,所以就做点题目练练手.

无源汇上下界可行流

题面

我们不妨先把所有的边的容量设为\([0,upp-low]\),那么这个时候显然一条边的实际流量应该是\(flow_i+low_i\)对吧.

我们对于新建的图跑流,肯定是能够保证\(flow_i\)平衡,但是我们无法保证\(flow_i+low_i\)平衡,这时我们引入一个\(delta\)数组.

对于\(delta_i\),它的值是所有出边的下界-入边的下界.

那么这个时候我们对于\(delta_i>0\)的就只需要增加它的入边,\(delta<0\)的增加它的出边.

这时我们引入超级源点和超级汇点,然后直接按照上述方法连边即可.

/*
  mail: mleautomaton@foxmail.com
  author: MLEAutoMaton
  This Code is made by MLEAutoMaton
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<iostream>
using namespace std;
#define ll long long
#define re register
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
inline int gi(){
	int f=1,sum=0;char ch=getchar();
	while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
	return f*sum;
}
const int N=50010,Inf=1e9+10;
int front[N],cnt,n,m,s,t,dep[N],in[N],out[N];
queue<int>Q;
struct node{int to,nxt,w;}e[N<<1];
void Add(int u,int v,int w){
	e[cnt]=(node){v,front[u],w};front[u]=cnt++;
	e[cnt]=(node){u,front[v],0};front[v]=cnt++;
}
bool bfs(){
	Q.push(s);memset(dep,0,sizeof(dep));dep[s]=1;
	while(!Q.empty()){
		int u=Q.front();Q.pop();
		for(int i=front[u];~i;i=e[i].nxt){
			int v=e[i].to;
			if(!dep[v] && e[i].w){
				dep[v]=dep[u]+1;Q.push(v);
			}
		}
	}
	return dep[t];
}
int dfs(int u,int flow){
	if(u==t || !flow)return flow;
	for(int i=front[u];~i;i=e[i].nxt){
		int v=e[i].to;
		if(dep[v]==dep[u]+1 && e[i].w){
			int di=dfs(v,min(flow,e[i].w));
			if(di){
				e[i].w-=di;e[i^1].w+=di;return di;
			}
			else dep[v]=0;
		}
	}
	return 0;
}
int Dinic(){
	int flow=0;
	while(bfs())while(int d=dfs(s,Inf))flow+=d;
	return flow;
}
int sum,Up[N];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
#endif
	n=gi();m=gi();memset(front,-1,sizeof(front));
	s=0;t=n+1;
	for(int i=1;i<=m;i++){
		int u=gi(),v=gi(),low=gi(),upp=gi();
		in[v]+=low;out[u]+=low;Up[i]=upp;
		Add(u,v,upp-low);
	}
	for(int i=1;i<=n;i++)
		if(in[i]>out[i]){Add(s,i,in[i]-out[i]);sum+=in[i]-out[i];}
		else Add(i,t,out[i]-in[i]);
	int flow=Dinic();
	if(flow<sum)return puts("NO"),0;
	puts("YES");
	for(int i=1;i<=m;i++)
		printf("%d\n",Up[i]-e[(i-1)*2].w);
	return 0;
}

有源汇上下界最大流

题面

还是先按照上述的方法,将\(t->s\)连边\(\infty\)保证流量平衡,然后跑出来一个可行流.

接着在残量网络上面跑\(s->t\)的最大流就是答案.

/*
  mail: mleautomaton@foxmail.com
  author: MLEAutoMaton
  This Code is made by MLEAutoMaton
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<iostream>
using namespace std;
#define ll long long
#define re register
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
inline int gi(){
	int f=1,sum=0;char ch=getchar();
	while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
	return f*sum;
}
const int N=100010,Inf=1e9+10;
int n,m,s,t,front[N],cnt,dep[N],ss,tt,delta[N],sum;queue<int>Q;
struct node{int to,nxt,w;}e[N<<1];
void Add(int u,int v,int w){
	e[cnt]=(node){v,front[u],w};front[u]=cnt++;
	e[cnt]=(node){u,front[v],0};front[v]=cnt++;
}
bool bfs(){
	Q.push(ss);memset(dep,0,sizeof(dep));dep[ss]=1;
	while(!Q.empty()){
		int u=Q.front();Q.pop();
		for(int i=front[u];~i;i=e[i].nxt){
			int v=e[i].to;
			if(!dep[v] && e[i].w){
				dep[v]=dep[u]+1;Q.push(v);
			}
		}
	}
	return dep[tt];
}
int dfs(int u,int flow){
	if(u==tt || !flow)return flow;
	for(int i=front[u];~i;i=e[i].nxt){
		int v=e[i].to;
		if(dep[v]==dep[u]+1 && e[i].w){
			int di=dfs(v,min(e[i].w,flow));
			if(di){
				e[i].w-=di;e[i^1].w+=di;return di;
			}
			else dep[v]=0;
		}
	}
	return 0;
}
int Dinic(){
	int flow=0;
	while(bfs())while(int d=dfs(ss,Inf))flow+=d;
	return flow;
}
int main(){
	n=gi();m=gi();s=gi();t=gi();memset(front,-1,sizeof(front));
	ss=n+1;tt=ss+1;
	for(int i=1;i<=m;i++){
		int u=gi(),v=gi(),low=gi(),upp=gi();
		Add(u,v,upp-low);
		delta[u]-=low;delta[v]+=low;
	}
	for(int i=1;i<=n;i++)
		if(delta[i]>0)Add(ss,i,delta[i]),sum+=delta[i];
		else Add(i,tt,-delta[i]);
	Add(t,s,Inf);
	int flow=Dinic();
	if(flow<sum)puts("please go home to sleep");
	else{
		ss=s;tt=t;
		printf("%d\n",Dinic());
	}
	return 0;
}

有源汇上下界最小流

题面

还是先按照上述的方法,将\(t->s\)连边\(\infty\)保证流量平衡,然后跑出来一个可行流.

接着在残量网络上面跑出\(t->s\)的最大流.最后那可行流减去最大流就是答案.

/*
  mail: mleautomaton@foxmail.com
  author: MLEAutoMaton
  This Code is made by MLEAutoMaton
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<iostream>
using namespace std;
#define ll long long
#define re register
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
inline int gi(){
	int f=1,sum=0;char ch=getchar();
	while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
	return f*sum;
}
const int N=1000010,Inf=1e9+10;
int n,m,s,t,cur[N],front[N],cnt,dep[N],ss,tt,delta[N],sum;queue<int>Q;
struct node{int to,nxt,w;}e[N<<1];
void Add(int u,int v,int w){
	e[cnt]=(node){v,front[u],w};front[u]=cnt++;
	e[cnt]=(node){u,front[v],0};front[v]=cnt++;
}
bool bfs(){
	Q.push(ss);memset(dep,0,sizeof(dep));dep[ss]=1;
	while(!Q.empty()){
		int u=Q.front();Q.pop();
		for(int i=front[u];~i;i=e[i].nxt){
			int v=e[i].to;
			if(!dep[v] && e[i].w){
				dep[v]=dep[u]+1;Q.push(v);
			}
		}
	}
	return dep[tt];
}
int dfs(int u,int flow){
	if(u==tt || !flow)return flow;
	for(int &i=cur[u];~i;i=e[i].nxt){
		int v=e[i].to;
		if(dep[v]==dep[u]+1 && e[i].w){
			int di=dfs(v,min(e[i].w,flow));
			if(di){
				e[i].w-=di;e[i^1].w+=di;return di;
			}
			else dep[v]=0;
		}
	}
	return 0;
}
int Dinic(){
	int flow=0;
	while(bfs()){
		for(int i=1;i<=max(tt,n);i++)cur[i]=front[i];
		while(int d=dfs(ss,Inf))flow+=d;
	}
	return flow;
}
void del(int u){
	for(int i=front[u];~i;i=e[i].nxt)
		e[i].w=e[i^1].w=0;
}
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
#endif
	n=gi();m=gi();s=gi();t=gi();memset(front,-1,sizeof(front));
	ss=n+1;tt=ss+1;
	for(int i=1;i<=m;i++){
		int u=gi(),v=gi(),low=gi(),upp=gi();
		Add(u,v,upp-low);
		delta[u]-=low;delta[v]+=low;
	}
	for(int i=1;i<=n;i++)
		if(delta[i]>0)Add(ss,i,delta[i]),sum+=delta[i];
		else Add(i,tt,-delta[i]);
	Add(t,s,Inf);
	int flow=Dinic();
	if(flow<sum)puts("please go home to sleep");
	else{
		flow=e[cnt-1].w;
		del(ss);del(tt);ss=t;tt=s;e[cnt-1].w=e[cnt-2].w=0;
		printf("%d\n",flow-Dinic());
	}
	return 0;
}
posted @ 2019-07-24 21:18  QwQGJH  阅读(209)  评论(2编辑  收藏  举报