hdu 1532 Drainage Ditches 增广路求最大流

裸的Edmonds-Karp,注意初始化就行了。

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstdlib>
#include <vector>
#include <cstring>
#define maxn 500
#define INF 0x3f3f3f3f
using namespace std;

typedef struct Edge{
	int from, to, cap, flow;
	Edge(int u=0, int v=0, int w=0, int o=0):from(u), to(v), cap(w), flow(o){}
}Edge;

vector<Edge> e;
vector<int> G[maxn];
int a[maxn];
int p[maxn];
int n, m;

void init(){
	for(int i = 0; i < maxn; i++)
		G[i].clear();
	e.clear();
}

long long ek()
{
	long long flow = 0;
	for(;;)
	{
		memset(a, 0, sizeof(a));
		queue<int> que;
		que.push(1);
		a[1] = INF;
		while(!que.empty())
		{
			int x = que.front();
			que.pop();
			for(int i = 0; i < G[x].size(); i++)
			{
				Edge& ee = e[G[x][i]];
				if(!a[ee.to] && ee.cap > ee.flow)
				{
					p[ee.to] = G[x][i];
					a[ee.to] = min(a[x], ee.cap-ee.flow);
					que.push(ee.to);
				}
			}
			if(a[m]) break;
		}
		if(!a[m]) break;
		for(int u = m; u != 1; u = e[p[u]].from)
		{
			e[p[u]].flow += a[m];
			e[p[u]^1].flow -= a[m];
		}
		flow += a[m];
	}
	return flow;
}

int main()
{
	//freopen("ztest.txt","r",stdin);
	while(scanf("%d%d", &n, &m) == 2)
	{
		init();
		for(int i = 0; i < n; i++)
		{
			int s, t, c;
			int mm;
			scanf("%d%d%d", &s, &t, &c);
			e.push_back(Edge(s, t, c, 0));
			e.push_back(Edge(t, s, 0, 0));
			mm = e.size();
			G[s].push_back(mm-2);
			G[t].push_back(mm-1);
		}
		printf("%lld\n",ek());
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/WukongAKK/article/details/81126678