最小费用最大流学习笔记

1给一个n个节点m条边的图,每条边存在容量和单位费用,有一个源点和一个汇点,求源点到汇点的最大流的同时保证每条边的流量乘以单位费用之和最小

2和最大流问题很相似,只不过把原先最大流寻找阻塞流的bfs替代成了以单位费用为边权跑最短路的spfa。同时用一个pre数组记录阻塞流每个节点的前一个节点,代替求最大流中dep的作用

3在更新流量的dfs中,只需要补上当前费用加上当前路径增加的流量乘以单位费用即可。

代码实现:

根据模板题:【模板】最小费用最大流 - 洛谷

1数据存储

const int maxn=5e3+10;
const int maxe=1e5+10;
const int inf=0x3f3f3f3f;
int n,m,s,t; 
struct edge{
	int to,w,c,next;
}e[maxe];
int head[maxn];//链式前向星 
int cur[maxn];//保存每个节点的head 
int tot=1;
int dis[maxn];//跑最短路 
bool vis[maxn];//spfa标记是否入队列 
int pre[maxn]; //记录阻塞流中当前节点前一个节点 
int ans=0,ret=0;//记录最大流,记录费用 
inline void add(int u,int to,int w,int c)
{
	tot++;
	e[tot].to=to;e[tot].w=w;e[tot].c=c;
	e[tot].next=head[u];head[u]=tot;
	tot++;
	e[tot].to=u;e[tot].w=0;e[tot].c=-c;
	e[tot].next=head[to];head[to]=tot;
}

2spfa求阻塞流

bool spfa(int s,int t)//寻找阻塞流 
{
	for(int i=1;i<=n;i++)
	{
	dis[i]=inf;
	vis[i]=0;	
	}
	queue<int>q;
	dis[s]=0;
	q.push(s);
	vis[s]=1;
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		vis[u]=0;
		for(int i=head[u];i;i=e[i].next)
		{
			int to=e[i].to;
			int c=e[i].c;
			int w=e[i].w;
			if(dis[to]>dis[u]+c&&w>0)
			{
				dis[to]=dis[u]+c;
				pre[to]=u;
				if(!vis[to])
				{
				q.push(to);
				vis[to]=1;	
				}
			}
		}
	}
	for(int i=1;i<=n;i++)
	cur[i]=head[i];
	return dis[t]!=inf;
}

3更新流量

int dinic(int u,int t,int flow)//更新流量 
{
	if(u==t)
	return flow;
	int sum=0;
	for(int &i=cur[u];i;i=e[i].next)
	{
		int to=e[i].to;
		int c=e[i].c;
		if(dis[to]==dis[u]+c&&e[i].w&&pre[to]==u)
		{
			int up=dinic(to,t,min(flow-sum,e[i].w));
			if(up)
			{
				e[i].w-=up;
				e[i^1].w+=up;
				ret+=up*c;//记录费用 
				sum+=up;
			}
		}
	}
	return sum;
}

4主函数

int main() {
	cin>>n>>m>>s>>t;
	for(int i=1;i<=m;i++)
	{
		int u,v,w,c;
		cin>>u>>v>>w>>c;
		add(u,v,w,c);
	}
	int q=0;
	while(spfa(s,t))
	{
		ans+=dinic(s,t,inf);
	}
	cout<<ans<<' '<<ret<<'\n';
	return 0;
}

5完整代码

#include <bits/stdc++.h>
using namespace std;
const int maxn=5e3+10;
const int maxe=1e5+10;
const int inf=0x3f3f3f3f;
int n,m,s,t; 
struct edge{
	int to,w,c,next;
}e[maxe];
int head[maxn];//链式前向星 
int cur[maxn];//保存每个节点的head 
int tot=1;
int dis[maxn];//跑最短路 
bool vis[maxn];//spfa标记是否入队列 
int pre[maxn]; //记录阻塞流中当前节点前一个节点 
int ans=0,ret=0;//记录最大流,记录费用 
inline void add(int u,int to,int w,int c)
{
	tot++;
	e[tot].to=to;e[tot].w=w;e[tot].c=c;
	e[tot].next=head[u];head[u]=tot;
	tot++;
	e[tot].to=u;e[tot].w=0;e[tot].c=-c;
	e[tot].next=head[to];head[to]=tot;
}
bool spfa(int s,int t)//寻找阻塞流 
{
	for(int i=1;i<=n;i++)
	{
	dis[i]=inf;
	vis[i]=0;	
	}
	queue<int>q;
	dis[s]=0;
	q.push(s);
	vis[s]=1;
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		vis[u]=0;
		for(int i=head[u];i;i=e[i].next)
		{
			int to=e[i].to;
			int c=e[i].c;
			int w=e[i].w;
			if(dis[to]>dis[u]+c&&w>0)
			{
				dis[to]=dis[u]+c;
				pre[to]=u;
				if(!vis[to])
				{
				q.push(to);
				vis[to]=1;	
				}
			}
		}
	}
	for(int i=1;i<=n;i++)
	cur[i]=head[i];
	return dis[t]!=inf;
}
int dinic(int u,int t,int flow)//更新流量 
{
	if(u==t)
	return flow;
	int sum=0;
	for(int &i=cur[u];i;i=e[i].next)
	{
		int to=e[i].to;
		int c=e[i].c;
		if(dis[to]==dis[u]+c&&e[i].w&&pre[to]==u)
		{
			int up=dinic(to,t,min(flow-sum,e[i].w));
			if(up)
			{
				e[i].w-=up;
				e[i^1].w+=up;
				ret+=up*c;//记录费用 
				sum+=up;
			}
		}
	}
	return sum;
}
int main() {
	cin>>n>>m>>s>>t;
	for(int i=1;i<=m;i++)
	{
		int u,v,w,c;
		cin>>u>>v>>w>>c;
		add(u,v,w,c);
	}
	int q=0;
	while(spfa(s,t))
	{
		ans+=dinic(s,t,inf);
	}
	cout<<ans<<' '<<ret<<'\n';
	return 0;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值