网络最大流之dicnic

#include<iostream>
#include<cstring>
#include<queue>
#define ll long long 
using namespace std;
#define maxn 500
int Depth[maxn + 5], Head[maxn + 5], Next[maxn + 5], V[maxn + 5], W[maxn + 5];
int cnt, s, t;
void add(int x, int y, int w)//建边
{
	++cnt;
	Next[cnt] = Head[x];
	V[cnt] = y; W[cnt] = w;
	Head[x] = cnt;
	++cnt;
	Next[cnt]=Head[y];
	V[cnt]=x;W[cnt]=0;
	Head[y]=cnt; 
}
bool bfs()
{
	queue<int>Q;
	while (!Q.empty())Q.pop();
	memset(Depth, -1, sizeof(Depth));
	Depth[s] = 0;
	Q.push(s);
	while (!Q.empty())
	{
		int u = Q.front();
		Q.pop();
		for (int i = Head[u]; i != -1; i = Next[i])
		{
			if (W[i] > 0 && Depth[V[i]] == -1)
			{
				Q.push(V[i]);
				Depth[V[i]] = Depth[u] + 1;
			}
		}
	}
	if (Depth[t] == -1)return 0;
	return 1;
}
int dfs(int u, int flow)//求流量
{
	if (u == t)
	{
		return flow;
	}
	for (int i = Head[u]; i != -1; i = Next[i])
	{
		if ((Depth[V[i]] == Depth[u] + 1) && (W[i] != 0))
		{
			int di = dfs(V[i], min(flow, W[i]));
			if (di>0)
			{
				W[i] -= di;
				W[i ^ 1] += di;
				return di;
			}
		}
	}
	return 0;
}
int main()
{
	int N,M;
	while(cin>>N>>M)
	{
		cnt = -1;
		memset(Head, -1, sizeof(Head));
		memset(Next, -1, sizeof(Next));
		s=0;t=M;
		add(s,1,1000000000);
		for(int i=1;i<=N;++i)
		{
			int u,v,w;
			cin>>u>>v>>w;
			add(u,v,w);
		}
		int ans=0;
		while(bfs())ans+=dfs(s,1000000000);
		cout<<ans<<'\n';
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值