POJ 4273 Drainage Ditches

大意不再赘述。

网络流入门题,这里我用的是Dinic算法,这里需要注意的是需要建立容量为0的一条反向弧。

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;

const int MAXN = 210;
const int MAXM = 210*210;
const int INF = 0x3f3f3f3f;

struct Edge
{
	int v, f;
	int next;
}edge[MAXM];

int n, m;
int cnt;

int first[MAXN], level[MAXN];
int q[MAXN];

void init()
{
	cnt = 0;
	memset(first, -1, sizeof(first));
}

void read_graph(int u, int v, int f)
{
	edge[cnt].v = v, edge[cnt].f = f;
	edge[cnt].next = first[u], first[u] = cnt++;
	edge[cnt].v = u, edge[cnt].f = 0;  //增加一条反向弧,容量为0 
	edge[cnt].next = first[v], first[v] = cnt++;
}

int bfs(int s, int t)
{
	memset(level, 0, sizeof(level));
	level[s] = 1;
	int front = 0, rear = 1;
	q[front] = s;
	while(front < rear)
	{
		int x = q[front++];
		if(x == t) return 1;
		for(int e = first[x]; e != -1; e = edge[e].next)
		{
			int v = edge[e].v, f = edge[e].f;
			if(!level[v] && f)
			{
				level[v] = level[x] + 1;
				q[rear++] = v;
			}
		}
	}
	return 0;
}

int dfs(int u, int maxf, int t)
{
	if(u == t) return maxf;
	int ret = 0;
	for(int e = first[u]; e != -1; e = edge[e].next)
	{
		int v = edge[e].v, f = edge[e].f;
		if(level[u] + 1 == level[v] && f)
		{
			int Min = min(maxf-ret, f);
			f = dfs(v, Min, t);
			edge[e].f -= f;
			edge[e^1].f += f;
			ret += f;
			if(ret == maxf) return ret;
		}
	}
	return ret;
}

int Dinic(int s, int t) //Dinic
{
	int ans = 0;
	while(bfs(s, t)) ans += dfs(s, INF, t);
	return ans;
}

void read_case()
{
	init();
	while(m--)
	{
		int u, v, f;
		scanf("%d%d%d", &u, &v, &f);
		read_graph(u, v, f);
		//read_graph(v, u, 0); //建立反向弧。 
	}
}

void solve()
{
	int ans = Dinic(1, n);
	printf("%d\n", ans);
}

int main()
{
	while(~scanf("%d%d", &m, &n))
	{
		read_case();
		solve();
	}
	return 0;
}



你可能感兴趣的:(POJ 4273 Drainage Ditches)