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

(POJ1273)Drainage Ditches(裸最大流,EK模板)

2016-07-21 16:09 477 查看
Drainage Ditches

Time Limit: 1000MS Memory Limit: 10000K

Total Submissions: 68869 Accepted: 26665

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

USACO 93

分析:

这题是个裸的最大流问题:在写了些其他的网络流的问题后,找到了一个很好的模板,今后可以调用了。如果一时看不懂可以看看我的另一篇博客:“初识网络流”http://blog.csdn.net/stillxjy/article/details/51983773

(WA了几次,当时就懵逼了。。。后来发现是忘了注释掉freopen()…..)

/*Ek模板:
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
const int N = 210;
#define MIN -99999999
#define MAX 1e6

using namespace std;
int max(int a,int b)
{if(a>b)return a;else return b;}
int min(int a,int b)
{if(a<b)return a;else return b;}
int c

;//容量网络
int f

;//实际流量网络
int p
; // 增广路径
int re
;//残亮网络
int n,m;
void init()
{
for(int i = 0;i<=n;i++)
{
for(int j = 0;j<=n;j++)
{
c[i][j] = f[i][j] = 0;
}
}
}
void EK(int s,int t)
{
queue<int>q;
while(q.empty()==false) q.pop();
int sum = 0;
while(1)
{
memset(re,0,sizeof(re));//注意,每次循环都要初始残亮网络
re[s] = MAX;
p[s] = -1;
q.push(s);
while(!q.empty())
{
int u = q.front();
q.pop();
for(int i = 1;i<=t;i++) //更新残亮网络,并记录增广路径
{
if(!re[i]&&f[u][i] < c[u][i])
{
q.push(i);
p[i] = u;
re[i] = min(re[u], c[u][i]-f[u][i]);
//整个网络的最小残量
}
}
}
if(re[t]==0) break;
for(int st = t;st!=s;st = p[st])
{
f[p[st]][st] += re[t]; //更新正向流量
f[st][p[st]] -= re[t]; //更新反向流量
}
// printf("re[t] = %d\n",re[t]);
sum += re[t];
}
printf("%d\n",sum);
}
int main()
{
int a,b,w;
while(~scanf("%d%d",&n,&m))
{
init();
for(int i = 0;i<n;i++)
{
scanf("%d%d%d",&a,&b,&w);
c[a][b] +=  w;
}
EK(1,m);
}
return 0;
}

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: