您的位置:首页 > 理论基础 > 计算机网络

PKU-1273 Drainage Ditches (网络最大流)

2013-05-21 10:31 141 查看
原题链接 http://poj.org/problem?id=1273
Drainage Ditches

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

Source Code

#include <iostream>
#include <queue>
using namespace std;

#define Max 205

//net存储网络邻接矩阵
int net[Max][Max];

//M点的个数
//path存储增广路径,并通过path是否为-1可以判断找当前增广路是否已找过该结点
//neck存储当前找到的增广路每个点允许的最大流
int M, path[Max], neck[Max];
queue<int> Q;

//寻找增广路
int BFS(int start, int end) {

int i;

memset(path, -1, sizeof(path));
while (!Q.empty()) {
Q.pop();
}
Q.push(start);
path[start] = 0;
neck[start] = INT_MAX;

int top, next;
while (!Q.empty()) {
top = Q.front();
Q.pop();

if (top == end) {
break;
}

for (i = 1; i <= M; i++) {
if (path[i] == -1 && net[top][i] > 0) {
path[i] = top;
if (net[top][i] < neck[top]) {
neck[i] = net[top][i];
} else {
neck[i] = neck[top];
}
Q.push(i);
}
}
}
if (path[end] == -1)
return -1;
return neck[end];
}

int Ford_Fulkerson(int start, int end) {

int newflow, maxflow = 0;
int cur, pre;

//如果存在增广路径,更新残留网络(正向和反向)
while ((newflow = BFS(start, end)) > 0) {
maxflow += newflow;
cur = end;
while (cur != start) {
pre = path[cur];
net[pre][cur] -= newflow;
net[cur][pre] += newflow;
cur = pre;
}
}
return maxflow;
}

int main() {
int N, i;
int si, ei, ci;

while (scanf("%d %d", &N, &M) != EOF) {
memset(net, 0, sizeof(net));
for (i = 0; i < N; i++) {
scanf("%d %d %d", &si, &ei, &ci);
net[si][ei] += ci;
}
printf("%d\n",Ford_Fulkerson(1,M));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: