首页 > 其他分享 >网络流

网络流

时间:2022-10-19 13:00:41浏览次数:35  
标签:head return LL flow 网络 tot 0ll

暂时不会。。。

dicnic

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL INF=0x3f3f3f3f;
const LL N=220;
const LL M=1e5+10;


LL n,m,s,t;

LL tot=1;
LL head[N],ver[M],ne[M];
LL w[M];
LL cur[N];

void add(LL x,LL y,LL z)
{
	ver[++tot]=y;
	ne[tot]=head[x];
	head[x]=tot;
	w[tot]=z;
}

LL d[N];

bool bfs()
{
	memcpy(cur,head,sizeof(head));
	memset(d,-1,sizeof(d));
	d[s]=0ll;
	queue<LL>q;
	q.push(s);
	while(q.size())
	{
		LL x=q.front();q.pop();
		if(x==t) break;
		for(LL i=head[x];i;i=ne[i])
		{
			LL y=ver[i];
			LL z=w[i];
			if(z>0ll&&d[y]==-1)
			{
				d[y]=d[x]+1;
				q.push(y);
			}
		}
	}
	return d[t]!=-1;
}

LL dfs(LL p=s,LL flow=INF)
{
	if(p==t) return flow;
	LL rest=flow;
	for(LL i=cur[p];i;i=ne[i])
	{
		cur[p]=i;
		LL y=ver[i];
		LL z=w[i];
		if(z>0&&d[y]==d[p]+1)
		{
			LL c=dfs(y,min(flow,z));
			rest-=c;
			w[i]-=c;
			w[i^1]+=c;
		}
	}
	return flow-rest;
}

LL dicnic()
{
	LL ans=0ll;
	while(bfs())
	{
		ans+=(LL)dfs();
	}
	return ans;
}

int main(){
	cin>>n>>m>>s>>t;
	for(LL i=1;i<=m;i++)
	{
		LL x,y;
		LL z;
		cin>>x>>y>>z;
		add(x,y,z);
		add(y,x,0ll);
	}
	printf("%lld",dicnic());
	return 0;
}

标签:head,return,LL,flow,网络,tot,0ll
From: https://www.cnblogs.com/mrkou/p/16805877.html

相关文章