您的位置:首页 > 大数据 > 人工智能

poj1273 Drainage Ditches(最大流)

2015-08-06 09:49 573 查看
Drainage Ditches

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 62364 Accepted: 23992
Description

Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's
clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch. 

Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network. 

Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle. 

Input

The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points
for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow
through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.
Output

For each case, output a single integer, the maximum rate at which water may emptied from the pond.
Sample Input
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10

Sample Output
50


题意:n条水管,m个结点,给出每条水管的最大流量,求从第一个结点流向最后一个结点,最多的水流量。

典型的最大流模板题,套用Dinic算法的模板。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define maxn 205

using namespace std;

int m, n, s, e, c;
int maps[maxn][maxn], q[maxn * maxn];
int d[maxn];//记录每个点所在的层

int BFS()//BFS分层,判断是否存在增广路
{
memset(d, -1, sizeof(d));
d[1] = 0;
int f, r;
f = 1;
r = 1;
q[r++] = 1;
while(f < r)
{
int x = q[f++];
for(int i = 1; i <= n; i++)
{
if(d[i] < 0 && maps[x][i] > 0)
{
d[i] = d[x] + 1;
q[r++] = i;
}
}
}
if(d
> 0)
return 1;
else
return 0;
}

int Find(int x, int v)//找出某一条增广路的最大流
{
int a;
if(x == n)
return v;
for(int i = 1; i <= n; i++)
{
if(maps[x][i] > 0 && d[i] == d[x] + 1 && (a = Find(i, min(v, maps[x][i]))))//x到i有流量 且 i是x的下一层 且 i到汇点存在最大流
{
maps[x][i] -= a;//增广路
maps[i][x] += a;//回退边
return a;
}
}
return 0;
}

int main()
{
int ans, t;
while (scanf("%d%d", &m, &n) != EOF)
{
ans = 0;
memset(maps, 0, sizeof(maps));
for(int i = 0; i < m; i++)
{
scanf("%d%d%d", &s, &e, &c);
maps[s][e] += c;
}
while(BFS())//BFS搜索判断是否有从源点到汇点的通路
{
while(t = Find(1, 2000000005))//查找增广路,求其最大流
ans += t;
}
printf("%d\n", ans);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj 最大流