网络流专题

最大流

学习链接

https://www.cnblogs.com/rmy020718/p/9546071.html

学习链接2

https://www.luogu.org/problemnew/solution/P3381

模板

https://www.luogu.org/problem/P3376

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
template<class T>inline void MAX(T &x,T y){if(y>x)x=y;}
template<class T>inline void MIN(T &x,T y){if(y<x)x=y;}
template<class T>inline void rd(T &x){
	x=0;char o,f=1;
	while(o=getchar(),o<48)if(o==45)f=-f;
	do x=(x<<3)+(x<<1)+(o^48);
	while(o=getchar(),o>47);
	x*=f;
}
const int M=1e5+5;
const int inf=1e9;
int n,m,s,t,tot,head[M],to[M<<1],nxt[M<<1],flow[M<<1];
inline void add_edge(int a,int b,int c){
	to[++tot]=b;
	flow[tot]=c;
	nxt[tot]=head[a];
	head[a]=tot;
}
int Q[M],deep[M],cur[M];
bool BFS(){
	for(int i=1;i<=n;i++)deep[i]=0;
	for(int i=1;i<=n;i++)cur[i]=head[i];
	int L=0,R=0;
	deep[s]=1;
	Q[++R]=s;
	while(L<R){
		int x=Q[++L];
		for(int i=head[x];i;i=nxt[i]){
			int y=to[i];
			if(!deep[y]&&flow[i]>0){
				deep[y]=deep[x]+1;
				Q[++R]=y;
			}
		}
	}
	return deep[t]>0;
}
int dfs(int x,int limit){
	if(x==t||!limit)return limit;
	int res=0;
	for(int &i=cur[x];i;i=nxt[i]){
		int y=to[i];
		if(deep[y]==deep[x]+1){
			int f=dfs(y,min(limit,flow[i]));
			if(!f)continue;
			res+=f;
			limit-=f;
			flow[i]-=f;
			flow[i^1]+=f;
			if(!limit)break;
		}
	}
	return res;
}
int main(){
#ifndef ONLINE_JUDGE
	freopen("jiedai.in","r",stdin);
//  freopen("jiedai.out","w",stdout);
#endif
	memset(head,0,sizeof(head));
	tot=1;
	rd(n),rd(m),rd(s),rd(t);
	for(int i=1;i<=m;i++){
		int a,b,c;
		rd(a),rd(b),rd(c);
		add_edge(a,b,c);
		add_edge(b,a,0);
	}
	int mxflow=0;
	while(BFS())mxflow+=dfs(s,inf);
	printf("%d\n",mxflow);
	return (0-0);
}

费用流(最小费用最大流)

学习链接

https://www.cnblogs.com/rmy020718/p/9548758.html

模板

https://www.luogu.org/problem/P3381

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
template<class T>inline void MAX(T &x,T y){if(y>x)x=y;}
template<class T>inline void MIN(T &x,T y){if(y<x)x=y;}
template<class T>inline void rd(T &x){
	x=0;char o,f=1;
	while(o=getchar(),o<48)if(o==45)f=-f;
	do x=(x<<3)+(x<<1)+(o^48);
	while(o=getchar(),o>47);
	x*=f;
}
const int M=4e5+5;
const ll INF=1e18;
const int inf=1e9;
int n,m,s,t,tot,head[M],to[M<<1],nxt[M<<1],flow[M<<1],cost[M<<1];
void add_edge(int a,int b,int c,int d){
	to[++tot]=b;
	flow[tot]=c;
	cost[tot]=d;
	nxt[tot]=head[a];
	head[a]=tot;
}
int Q[M],limit[M],mark[M],pre[M],last[M];
ll dis[M],mxflow,micost;
bool SPFA(){
	for(int i=1;i<=n;i++)dis[i]=INF,limit[i]=inf,mark[i]=0;
	int L=0,R=0;
	Q[++R]=s;
	dis[s]=0;
	pre[t]=0;
	while(L!=R){
		int x=Q[L=L%n+1];
		mark[x]=0;
		for(int i=head[x];i;i=nxt[i]){
			int y=to[i];
			if(flow[i]&&dis[x]+cost[i]<dis[y]){
				dis[y]=dis[x]+cost[i];
				pre[y]=x;
				last[y]=i;
				limit[y]=min(limit[x],flow[i]);
				if(!mark[y]){
					Q[R=R%n+1]=y;
					mark[y]=1;
				}
			}
		}
	}
	return pre[t];
}
void MCMF(){
	while(SPFA()){
		mxflow+=limit[t];
		micost+=limit[t]*dis[t];
		for(int i=t;i!=s;i=pre[i]){
			flow[last[i]]-=limit[t];
			flow[last[i]^1]+=limit[t];
		}
	}
}
int main(){
#ifndef ONLINE_JUDGE
	freopen("jiedai.in","r",stdin);
//  freopen("jiedai.out","w",stdout);
#endif
	memset(head,0,sizeof(head));
	tot=1;
	rd(n),rd(m),rd(s),rd(t);
	for(int i=1;i<=m;i++){
		int a,b,c,d;
		rd(a),rd(b),rd(c),rd(d);
		add_edge(a,b,c,d);
		add_edge(b,a,0,-d);
	}
	MCMF();
	printf("%lld %lld\n",mxflow,micost);
	return (0-0);
}

一堆题目的链接

https://www.luogu.org/problem/list?tag=332

题解链接

https://www.cnblogs.com/xseventh/p/7912202.html

例一:最大流最小割定理

模板题链接

https://www.luogu.org/problem/P2762

分析

从源点向每个实验连一条边,容量为实验的收益;
从每个仪器向汇点连一条边,容量为仪器的费用;
从每个实验向每个仪器连一条边,容量为正无穷。
如果要进行一项实验,就割掉它所需要的仪器与汇点之间的边,
这样源点就无法通过这条实验边到达汇点。
如果不进行一项实验,就割掉源点与这个实验之间的边,
同样地,源点也无法通过这条实验边到达汇点。
可以发现,只要源点与汇点是不连通的,就对应存在一组合法的选择方案。
在这个图中,一个割的容量就等于不进行的实验的边权和加上需要的仪器的边权和。
割 的 容 量 = s u m { 不 要 的 实 验 } + s u m { 要 的 仪 器 } 割的容量=sum\{不要的实验\}+sum\{要的仪器\} =sum{}+sum{}
割 的 容 量 = s u m { 所 有 实 验 } − s u m { 要 的 实 验 } + s u m { 要 的 仪 器 } 割的容量=sum\{所有实验\}-sum\{要的实验\}+sum\{要的仪器\} =sum{}sum{}+sum{}
割 的 容 量 = s u m { 所 有 实 验 } − 净 收 益 割的容量=sum\{所有实验\}-净收益 =sum{}
净 收 益 = s u m { 所 有 实 验 } − 割 的 容 量 净收益=sum\{所有实验\}-割的容量 =sum{}
那么 最 大 净 收 益 = s u m { 所 有 实 验 } − 最 小 割 最大净收益=sum\{所有实验\}-最小割 =sum{}
根据最大流最小割定理,可得:
最 大 净 收 益 = s u m { 所 有 实 验 } − 最 大 流 最大净收益=sum\{所有实验\}-最大流 =sum{}

代码

这题的输入是真的恶心

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
template<class T>inline void MAX(T &x,T y){if(y>x)x=y;}
template<class T>inline void MIN(T &x,T y){if(y<x)x=y;}
template<class T>inline void rd(T &x,string &str,int &now,int &len){
	x=0;char o,f=1;
	while(now<len&&(o=str[now++],o<48))if(o==45)f=-f;
	do x=(x<<3)+(x<<1)+(o^48);
	while(now<len&&(o=str[now++],o>47));
	x*=f;
}
const int M=55;
const int inf=1e9;
int n,m,s,t,ans,A[M],B[M];
int tot,head[M<<1],to[M*M<<1],nxt[M*M<<1],flow[M*M<<1];
void add_edge(int a,int b,int c){
	to[++tot]=b;
	flow[tot]=c;
	nxt[tot]=head[a];
	head[a]=tot;
}
int Q[M<<1],dis[M<<1],cur[M<<1];
bool BFS(){
	memset(dis,0,sizeof(dis));
	for(int i=1;i<=n+m+2;i++)cur[i]=head[i];
	int L=0,R=0;
	dis[s]=1;
	Q[++R]=s;
	while(L<R){
		int x=Q[++L];
		for(int i=head[x];i;i=nxt[i]){
			int y=to[i];
			if(!dis[y]&&flow[i]){
				dis[y]=dis[x]+1;
				Q[++R]=y;
			}
		}
	}
	return dis[t]>0;
}
int dfs(int x,int limit){
	if(!limit||x==t)return limit;
	int res=0;
	for(int &i=cur[x];i;i=nxt[i]){
		int y=to[i];
		if(dis[y]==dis[x]+1){
			int f=dfs(y,min(limit,flow[i]));
			if(!f)continue;
			res+=f;
			limit-=f;
			flow[i]-=f;
			flow[i^1]+=f;
			if(!limit)break;
		}
	}
	return res;
}
int main(){
#ifndef ONLINE_JUDGE
	freopen("jiedai.in","r",stdin);
//	freopen("jiedai.out","w",stdout);
#endif
	scanf("%d%d",&m,&n);
	s=n+m+1,t=n+m+2,tot=1;
	string str;
	getline(cin,str);
	for(int i=1,j;i<=m;i++){
		getline(cin,str);
		int now=0,len=str.length();
		rd(A[i],str,now,len);
		add_edge(s,i,A[i]);
		add_edge(i,s,0);
		ans+=A[i];
		while(1){
			rd(j,str,now,len);
			add_edge(i,j+m,inf);
			add_edge(j+m,i,0);
			if(now>=len)break;
		}
	}
	for(int i=1;i<=n;i++){
		scanf("%d",&B[i]);
		add_edge(m+i,t,B[i]);
		add_edge(t,m+i,0);
	}
	while(BFS())ans-=dfs(s,inf);
	for(int i=1;i<=m;i++)if(dis[i])printf("%d ",i);
	puts("");
	for(int i=1;i<=n;i++)if(dis[i+m])printf("%d ",i);
	puts("");
	printf("%d\n",ans);
	return (0-0);
}

例二:二分图最大匹配

学习链接

https://blog.csdn.net/qq_38956769/article/details/80238896

模板

https://loj.ac/problem/6000

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
template<class T>inline void MAX(T &x,T y){if(y>x)x=y;}
template<class T>inline void MIN(T &x,T y){if(y<x)x=y;}
template<class T>inline void rd(T &x){
	x=0;char o,f=1;
	while(o=getchar(),o<48)if(o==45)f=-f;
	do x=(x<<3)+(x<<1)+(o^48);
	while(o=getchar(),o>47);
	x*=f;
}
const int M=105;
int n,m,a,b,match[M],mark[M];
int tot,head[M],to[M*M],nxt[M*M];
void add_edge(int a,int b){
	to[++tot]=b;
	nxt[tot]=head[a];
	head[a]=tot;
}
bool dfs(int x){
	for(int i=head[x];i;i=nxt[i]){
		int y=to[i];
		if(mark[y])continue;
		mark[y]=1;
		if(!match[y]||dfs(match[y])){
			match[x]=y;
			match[y]=x;
			return true;
		}
	}
	return false;
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("jiedai.in","r",stdin);
//  freopen("jiedai.out","w",stdout);
#endif
	rd(n),rd(m);
	while(scanf("%d%d",&a,&b)!=EOF)add_edge(a,b);
	int ans=0;
	for(int i=1;i<=m;i++){
		memset(mark,0,sizeof(mark));
		if(!match[i]&&dfs(i))ans++;
	}
	printf("%d\n",ans);
	return (0-0);
}

一些结论:
最小覆盖点数=最大匹配数
最小边覆盖=最大独立集=总节点数-最大匹配数

例三:最小路径覆盖 [二分图最大匹配]

学习链接

https://blog.csdn.net/qq_39627843/article/details/82012572

模板

https://www.luogu.org/problem/P2764

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
template<class T>inline void MAX(T &x,T y){if(y>x)x=y;}
template<class T>inline void MIN(T &x,T y){if(y<x)x=y;}
template<class T>inline void rd(T &x){
	x=0;char o,f=1;
	while(o=getchar(),o<48)if(o==45)f=-f;
	do x=(x<<3)+(x<<1)+(o^48);
	while(o=getchar(),o>47);
	x*=f;
}
const int N=155,M=6005;
int n,m,tot,head[N<<1],to[M],nxt[M];
inline void add_edge(int a,int b){
	to[++tot]=b;
	nxt[tot]=head[a];
	head[a]=tot;
}
int match[N<<1],mark[N<<1],vis[N];
bool dfs(int x){
	for(int i=head[x];i;i=nxt[i]){
		int y=to[i];
		if(mark[y])continue;
		mark[y]=1;
		if(!match[y]||dfs(match[y])){
			match[x]=y;
			match[y]=x;
			return true;
		}
	}
	return false;
}
void visit(int x){
	vis[x]=1;
	printf("%d ",x);
	if(match[x])visit(match[x]-n);
}
int main(){
#ifndef ONLINE_JUDGE
	freopen("jiedai.in","r",stdin);
//	freopen("jiedai.out","w",stdout);
#endif
	rd(n),rd(m);
	for(int i=1;i<=m;i++){
		int a,b;
		rd(a),rd(b);
		add_edge(a,b+n);
	}
	int ans=n;
	for(int i=1;i<=n;i++)if(!match[i]){
		memset(mark,0,sizeof(mark));
		ans-=dfs(i);
	}
	for(int i=1;i<=n;i++)if(!vis[i]){
		visit(i);
		puts("");
	}
	printf("%d\n",ans);
	return (0-0);
}

例四:魔术球问题 [最小路径覆盖]

链接

https://www.luogu.org/problem/P2765

分析

相加之后为完全平方的两个数之间建一条边,那么一根柱子上的若干个球形成了一条链。如果当前的图是1-m形成的图时,那么最少需要的柱子就是最小路径覆盖。
所以每次向图中加入一个数的边,然后用匈牙利算法在残余网络寻找新的匹配,直到需要的柱子数刚好超过题目的输入时停止,输出加入的球的个数即可。

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
template<class T>inline void MAX(T &x,T y){if(y>x)x=y;}
template<class T>inline void MIN(T &x,T y){if(y<x)x=y;}
template<class T>inline void rd(T &x){
	x=0;char o,f=1;
	while(o=getchar(),o<48)if(o==45)f=-f;
	do x=(x<<3)+(x<<1)+(o^48);
	while(o=getchar(),o>47);
	x*=f;
}
const int M=5e5+5;
int n,tot,head[M],to[M],nxt[M];
void add_edge(int a,int b){
	to[++tot]=b;
	nxt[tot]=head[a];
	head[a]=tot;
}
int mark[M],match[M],vis[M];
bool dfs(int x){
	for(int i=head[x];i;i=nxt[i]){
		int y=to[i];
		if(mark[y])continue;
		mark[y]=1;
		if(!match[y]||dfs(y)){
			match[x]=y;
			match[y]=x;
			return true;
		}
	}
	return false;
}
void visit(int x){
	vis[x]=1;
	printf("%d ",x>>1);
	if(match[x])visit(match[x]-1);
}
int main(){
#ifndef ONLINE_JUDGE
	freopen("jiedai.in","r",stdin);
//	freopen("jiedai.out","w",stdout);
#endif
	rd(n);
	for(int now=1,path=1;;now++,path++){
		for(int i=1;i*i<2*now;i++){
			int pre=i*i-now;
			if(pre>0&&pre<now)add_edge(pre<<1,now<<1|1);
		}
		for(int i=1;i<=now;i++)mark[i<<1|1]=0;
		for(int i=1;i<=now;i++)if(!match[i<<1]&&dfs(i<<1)){path--;break;}
		if(path>n){
			printf("%d\n",now-1);
			for(int i=1;i<now;i++)if(!vis[i<<1]){
				visit(i<<1);
				puts("");
			}
			break;
		}
	}
	return (0-0);
}

例五:圆桌问题 [最大流]

链接:https://www.luogu.org/problem/P3254

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
template<class T>inline void MAX(T &x,T y){if(y>x)x=y;}
template<class T>inline void MIN(T &x,T y){if(y<x)x=y;}
template<class T>inline void rd(T &x){
	x=0;char o,f=1;
	while(o=getchar(),o<48)if(o==45)f=-f;
	do x=(x<<3)+(x<<1)+(o^48);
	while(o=getchar(),o>47);
	x*=f;
}
const int M=155,N=275,inf=1e9;
int tot,head[N+M],to[M*N<<1],nxt[M*N<<1],flow[M*N<<1];
inline void add_edge(int a,int b,int c){
	to[++tot]=b;
	nxt[tot]=head[a];
	flow[tot]=c;
	head[a]=tot;
}
int n,m,s,t,sum,A[M],B[N],dis[N+M],Q[N+M],cur[N+M];
bool BFS(){
	memset(dis,0,sizeof(dis));
	for(int i=1;i<=n+m+2;i++)cur[i]=head[i];
	dis[s]=1;
	int L=0,R=0;
	Q[++R]=s;
	while(L<R){
		int x=Q[++L];
		for(int i=head[x];i;i=nxt[i]){
			int y=to[i];
			if(dis[y]||!flow[i])continue;
			dis[y]=dis[x]+1;
			Q[++R]=y;
		}
	}
	return dis[t];
}
int dfs(int x,int limit){
	if(x==t||!limit)return limit;
	int res=0;
	for(int &i=cur[x];i;i=nxt[i]){
		int y=to[i];
		if(dis[y]==dis[x]+1){
			int f=dfs(y,min(limit,flow[i]));
			if(!f)continue;
			res+=f;
			limit-=f;
			flow[i]-=f;
			flow[i^1]+=f;
			if(!limit)break;
		}
	}
	return res;
}
int main(){
#ifndef ONLINE_JUDGE
	freopen("jiedai.in","r",stdin);
//  freopen("jiedai.out","w",stdout);
#endif
	rd(m),rd(n);
	tot=1,s=n+m+1,t=n+m+2;
	for(int i=1;i<=m;i++){
		rd(A[i]);
		add_edge(s,i,A[i]);
		add_edge(i,s,0);
		sum+=A[i];
	}
	for(int i=1;i<=n;i++){
		rd(B[i]);
		add_edge(m+i,t,B[i]);
		add_edge(t,m+i,0);
	}
	for(int i=1;i<=m;i++)for(int j=1;j<=n;j++){
		add_edge(i,m+j,1);
		add_edge(m+j,i,0);
	}
	int mxflow=0;
	while(BFS())mxflow+=dfs(s,inf);
	if(mxflow==sum){
		puts("1");
		for(int i=1;i<=m;i++){
			for(int j=head[i];j;j=nxt[j])
				if(!flow[j])printf("%d ",to[j]-m);
			puts("");
		}
	}
	else puts("0");
	return (0-0);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值