HDU 3549 — Flow Problem 入门题

原题:http://acm.hdu.edu.cn/showproblem.php?pid=3549

题意:给定n个点,m条边,以及边上的容量,问1到n的最大流;


#include<stdio.h>
#include<queue>
#include<string.h>
#include<iostream>
#include<algorithm>

using namespace std;
#define inf 999999999;
const int N = 20;
int cap[N][N], flow[N][N];			//cap表示容量,flow表示当前流量; 
int a[N], p[N];			//a[i]表示从s到i的最小残量,p表示增广路上一节点; 
int n, m;

int bfs(int s, int t)
{
	queue<int>q;
	memset(flow, 0, sizeof(flow));
	int ans = 0;
	while(1)
	{
		memset(a, 0, sizeof(a));
		a[s] = inf;
		q.push(s);
		while(!q.empty())
		{
			int u = q.front();
			q.pop();
			for(int v = 1;v<=n;v++)
			{
				if(!a[v] && cap[u][v]>flow[u][v])
				{
					p[v] = u;
					a[v] = min(a[u], cap[u][v]-flow[u][v]);
					q.push(v);
				}
			}
		}
		if(a[t] == 0)
		break;
		for(int u = t;u!=s;u = p[u])
		{
			flow[p[u]][u]+=a[t];
			flow[u][p[u]]-=a[t];
		}
		ans+=a[t];
	}
	return ans;
}

int main()
{
	int cas;
	int T = 0;
	scanf("%d", &cas);
	while(cas--)
	{
		memset(cap, 0, sizeof(cap));
		scanf("%d%d", &n, &m);
		for(int i = 1;i<=m;i++)
		{
			int u, v, w;
			scanf("%d%d%d", &u, &v, &w);
			cap[u][v]+=w;
		}
		printf("Case %d: %d\n", ++T, bfs(1, n));
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值