HDU 3549【网络流入门--然而我还是XXX】

0 0.。。。。我记得上个学期说要入门网络流。。。0 0.。。。。。。对不起后来为什么我都忘了这茬。。。然而现在已经没什么卵用了。。。

再见

其实这代码还是挺形象的。。。。就像水在咕咕地流过去。。。第一篇网络流。。。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
#define maxn 16
#define inf 1100
int cap[maxn][maxn],c[maxn],flow[maxn][maxn],p[maxn];
queue<int>que;
int n,ans;
void solve()
{
	int flag=1;
	memset(flow,0,sizeof(flow));
	while(flag)
	{
		memset(c,0,sizeof(c));
		que.push(1);
		c[1]=inf;p[1]=-1;
		while(!que.empty())
		{
			int pos=que.front();
			que.pop();
			for(int i=1;i<=n;i++)
			{
				if(!c[i]&&flow[pos][i]<cap[pos][i])
				{
					c[i]=min(cap[pos][i]-flow[pos][i],c[pos]); //c表示当前节点在这次的循环中流过了多少水,如果是0的话就表示没有水流过,可以流,但是流过的水应该是从父节点流来的水和现实中能流过的水的最小值,这样才保证合法。c[1]为inf,表示这里可以有无穷多的水流流过。戳萌点了。。
					que.push(i);
					p[i]=pos;
				}
			}
		}
		if(c[n]==0)
		{
			flag=0;
		}
		ans+=c[n];
		int tmp=n;
		while(tmp!=-1)
		{
			flow[p[tmp]][tmp]+=c[n]; //如果这次成立,就更新每个管道流过的水流
			flow[tmp][p[tmp]]-=c[n]; //回流?这里不是很懂
			tmp=p[tmp];
		}
	}
}
int main()
{
	int T,cas=0;
	cin>>T;
	while(T--)
	{
		memset(cap,0,sizeof(cap));
		ans=0;
		int m;
		int x,y,z;
		cin>>n>>m;
		while(m--)
		{
			cin>>x>>y>>z;
			cap[x][y]+=z; //注意这里,因为可能有多个通道,但是达到的效果是叠加的
		}
		solve();
		printf("Case %d: ",++cas);
		cout<<ans<<endl;
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值