网络流(最大流---Edmonds-Karp算法)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string.h>
#include <vector>
#include <queue>
#define inf 100000000
#define N 250
using namespace std;         //poj1273  hdu1532 网络流(最大流---Edmonds-Karp算法)
int map[N][N], flow[N][N], a[N], p[N];  //flow[u][v]表示从u到v的流量,a记录的是s->e这条线路中的最小残余量的值, p记录的是节点的父节点值(逆推时用到)
int EK(int s, int e)   //s为起点,e为终点
{
	queue<int> q;
	int t, u, f=0;
	memset(flow, 0, sizeof(flow));
	memset(p, 0, sizeof(p));
	while(1)
	{
		q.push(s);
		memset(a, 0, sizeof(a));
		a[s]=inf;
		while(!q.empty())  //计算出从s到e的一条可行路径
		{
			u=q.front();
			q.pop();
			for(t=1; t<=e; ++t)
			{
				if(!a[t]&&map[u][t]>flow[u][t])
				{
					p[t]=u;
					a[t]=(a[u]<map[u][t]-flow[u][t]?a[u]:map[u][t]-flow[u][t]);
					q.push(t);
				}
			}
		}
		if(a[e]==0)break;  //没有残余容量,当前流已经是最大流
		for(t=e; t!=s; t=p[t])   //更新从s到t的一条支路的流量
		{
			flow[t][p[t]]-=a[e];
			flow[p[t]][t]+=a[e];
		}
		f+=a[e];
	}
	return f;
}

int main()
{
	int n, m, t, b, c, d;
	while(scanf("%d%d", &n, &m)!=EOF)
	{
		memset(map, 0, sizeof(map));
		for(t=0; t<n; ++t)
		{
			scanf("%d%d%d", &b, &c, &d);
			map[b][c]+=d;   //防止重边
		}
		printf("%d\n", EK(1, m));
	}
	return 0;
}
	


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值