您的位置:首页 > 理论基础 > 计算机网络

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

2015-09-08 20:31 716 查看
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
==0)
		{
			flag=0;
		}
		ans+=c
;
		int tmp=n;
		while(tmp!=-1)
		{
			flow[p[tmp]][tmp]+=c
; //如果这次成立,就更新每个管道流过的水流
			flow[tmp][p[tmp]]-=c
; //回流?这里不是很懂
			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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: