【模板】网络流Dicnic

模板题

 

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
#define N 1000005
typedef long long LL;
#define INF 1000000000000000000ll
struct mzls
{
	int to,nt;
	LL w;
}a[N];
int n,m,s,t,cnt,head[N],lev[N],iter[N];
inline void add(int x,int y,LL z)
{
	a[cnt].to=y,a[cnt].w=z,a[cnt].nt=head[x],head[x]=cnt++;
	a[cnt].to=x,a[cnt].w=0,a[cnt].nt=head[y],head[y]=cnt++;
}
inline void bfs(int s)
{
	memset(lev,-1,sizeof(lev));
	lev[s]=0;
	queue<int>que;
	que.push(s);
	while(!que.empty())
	{
		int x=que.front();
		que.pop();
		for(int i=head[x];i!=-1;i=a[i].nt)
			if(a[i].w>0&&lev[a[i].to]<0)
			{
				lev[a[i].to]=lev[x]+1;
				que.push(a[i].to);
			}
	}
}
inline LL dfs(int x,int t,LL ml)
{
	if(x==t)
		return ml;
	for(int &i=iter[x];i!=-1;i=a[i].nt)
	{
		int v=a[i].to;
		LL w=a[i].w;
		if(w<=0||lev[v]<=lev[x])
			continue;
		LL x1=dfs(v,t,min(ml,w));
		if(x1>0)
		{
			a[i].w-=x1;
			a[i^1].w+=x1;
			return x1;
		}
	}
	return 0;
}
inline LL max_flow(int s,int t)
{
	LL ret=0;
	while(1)
	{
		bfs(s);
		if(lev[t]<0)
			return ret;
		memcpy(iter,head,sizeof(head));
		LL x1;
		while((x1=dfs(s,t,INF))>0)
			ret+=x1;
	}
}
int main()
{
	memset(head,-1,sizeof(head));
	scanf("%d%d%d%d",&n,&m,&s,&t);
	for(int i=1;i<=m;i++)
	{
		int x,y,z;
		scanf("%d%d%d",&x,&y,&z);
		add(x,y,z);
	}
	printf("%lld",max_flow(s,t));
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值