【图论】网络流模板

最大流,最小割

dinic

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define ll long long
using namespace std;
const int Maxn=2e2+5,Maxm=(5e3+50)*2,INF=1e8;
struct edge{
	int to,nxt;
	ll f;
}e[Maxm];
int n,m,S,T,tot=1;
int head[Maxn],dep[Maxn],cur[Maxn];
inline void add(int u,int v,ll w)
{
	e[++tot].to=v; e[tot].nxt=head[u]; head[u]=tot; e[tot].f=w;
	e[++tot].to=u; e[tot].nxt=head[v]; head[v]=tot; e[tot].f=0;
}

inline bool bfs()
{
	queue<int> q;
	memset(dep,0,sizeof dep);
	q.push(S);
	dep[S]=1; cur[S]=head[S];
	while(!q.empty())
	{
		int now=q.front();q.pop();
		for(int i=head[now];i>1;i=e[i].nxt)
		{
			int nx=e[i].to;
			if(dep[nx]||e[i].f==0) continue;
			dep[nx]=dep[now]+1;
			cur[nx]=head[nx];
			if(nx==T) return 1;
			q.push(nx);
		}
	}
	return 0;
}

inline ll dfs(int x,ll limit)
{
	if(x==T) return limit;
	ll flow=0;
	for(int i=cur[x];i>1&&flow<limit;i=e[i].nxt)
	{
		int nx=e[i].to;
		if(dep[nx]==dep[x]+1&&e[i].f)
		{
			ll t=dfs(nx,min(limit-flow,e[i].f));
			e[i].f-=t; e[i^1].f+=t;
			flow+=t;
		}
	}
	if(flow==0) dep[x]=0;
	return flow;
}

inline ll dinic()
{
	ll ret=0;
	while(bfs())
	{
		ret+=dfs(S,INF);
	}
	return ret;
}

int main()
{
	scanf("%d%d%d%d",&n,&m,&S,&T);
	for(int i=1,a,b;i<=m;i++)
	{
		ll c;
		scanf("%d%d%lld",&a,&b,&c);
		add(a,b,c);
	}
	printf("%lld",dinic());
	
	return 0;
}

费用流

EK

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){ if(ch=='-') f=-1;ch=getchar(); }
	while(ch>='0'&&ch<='9'){ x=(x<<1)+(x<<3)+(ch^48);ch=getchar(); }
	return x*f;
}
const int Maxn=5e3+5,Maxm=5e4*2+500,INF=1e8;
struct edge{
	int to,nxt,f,cost;
}e[Maxm];
queue<int> q;
int n,m,S,T,tot=1;
int head[Maxn],incf[Maxn],pre[Maxn],dis[Maxn];
bool vis[Maxn];
inline void add(int u,int v,int c,int d)
{
	e[++tot].to=v; e[tot].nxt=head[u]; head[u]=tot; e[tot].f=c; e[tot].cost=d;
	e[++tot].to=u; e[tot].nxt=head[v]; head[v]=tot; e[tot].f=0; e[tot].cost=-d;
}

inline bool spfa()
{
	memset(dis,0x3f,sizeof dis);
	memset(incf,0,sizeof incf);
	q.push(S);
	incf[S]=INF; dis[S]=0;
	while(!q.empty())
	{
		int now=q.front();q.pop();
		vis[now]=0;
		for(int i=head[now];i>1;i=e[i].nxt)
		{
			int nx=e[i].to;
			if(dis[nx]>dis[now]+e[i].cost&&e[i].f)
			{
				dis[nx]=dis[now]+e[i].cost;
				incf[nx]=min(incf[now],e[i].f);
				pre[nx]=i;
				if(!vis[nx])
				{
					vis[nx]=1;
					q.push(nx);
				}
			}
		}
	}
	return incf[T]>0;
}

inline void EK(int& flow,int& cost)
{
	flow=0; cost=0;
	while(spfa())
	{
		int t=incf[T];
		flow+=t; cost+=t*dis[T];
		for(int i=T;i!=S;i=e[pre[i]^1].to)
		{
			e[pre[i]].f-=t;
			e[pre[i]^1].f+=t;
		}
	}
}

int main()
{
	n=read(); m=read(); S=read(); T=read();
	for(int i=1,a,b,c,d;i<=m;i++)
	{
		a=read(); b=read(); c=read(); d=read();
		add(a,b,c,d);
	}
	int flow,cost;
	EK(flow,cost);
	printf("%d %d",flow,cost);
	return 0;
}

TIPS:

    1. 拆点之后的无向边加的时候,注意起点和终点的不同,不能直接加正向和反向流量一样的边
  • 2.注意最小割点和最小割边的不同之处
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值