hdu_1532

网络流裸题, ek
#include <stdio.h>
#include <string.h>
#include <queue>
#define MAXSIZE 201
#define INF 0x7fffffff
using namespace std;

int edge;
int vertex;
int level[MAXSIZE];
int r[MAXSIZE][MAXSIZE];

bool bfs(void)
{
	int cur;
	queue<int> q;

	memset(level, -1, sizeof(level));
	q.push(1);
	level[1] = 0;

	while( !q.empty() )
	{
		cur = q.front();
		q.pop();

		for(int i = 1; i <= vertex; i ++)
		{
			if( -1 != level[i] || !r[cur][i] )
				continue;

			level[i] = level[cur]+1;
			q.push(i);
		}
	}

	return (-1 == level[vertex])? false : true;
}

int dfs(int u, int cur_val)
{
	int tmp_val;
	int val;

	if( u == vertex )
		return cur_val;

	tmp_val = cur_val;
	for(int i = 1; i <= vertex; i ++)
	{
		if( (level[i] != level[u]+1) || !r[u][i] )
			continue;
		val = dfs(i, min(tmp_val, r[u][i]));
		tmp_val -= val;
		r[u][i] -= val;
		r[i][u] += val;
	}
	return cur_val - tmp_val;
}

int dinic_algorithm(void)
{
	int tmp_val;
	int ans;

	ans = tmp_val = 0;
	while( true == bfs() )
		while( tmp_val = dfs(1, INF) )
			ans += tmp_val;
	return ans;
}

int main(int argc, char const *argv[])
{
	//freopen("test.in", "r", stdin);
	int from;
	int to;
	int val;

	while( ~scanf("%d %d", &edge, &vertex) )
	{
		memset(r, 0, sizeof(r));
		for(int i = 0; i < edge; i ++)
		{
			scanf("%d %d %d", &from, &to, &val);
			r[from][to] += val;
		}
		printf("%d\n", dinic_algorithm());
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值