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

[POJ 1273] Drainage Ditches & 最大流Dinic模板

2015-06-12 06:09 567 查看

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

USACO 93

【题目大意】
就是最普通的网络流问题,什么条件都没变。
【题解】
终于会dinic了TAT啊,这是我A的第一道网络流题啊
要纪念啊=-=
纯dinic模板,我用邻接矩阵,然而什么时候要学一学边集数组版本=-= 908K 0ms TAT终于过了!

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<string>
#include<math.h>
#include<algorithm>
#include<queue>
using namespace std;
int c[251];              // 分层图
int a[251][251];          //邻接矩阵
int n,m,ans;
inline int min(int a,int b) {return a<b?a:b;}
int bfs() {
memset(c,0xff,sizeof(c));
c[1]=0;
queue<int> q;
q.push(1);
while(!q.empty()) {
int top=q.front();
q.pop();
for (int i=1;i<=n;++i) {
if(c[i]<0&&a[top][i]>0) {
c[i]=c[top]+1;
q.push(i);
}
}
}
if(c
>0) return 1;
else return 0;
}
int dfs(int x,int low) {
if(x==n) return low;
for (int i=1,k;i<=n;++i)
if(a[x][i]>0&&c[i]==c[x]+1&&(k=dfs(i,min(low,a[x][i])))) {
a[x][i]-=k;
a[i][x]+=k;
return k;
}
return 0;
}
int main() {
while(~scanf("%d%d",&m,&n)) {
memset(a,0,sizeof(a));
for (int i=1;i<=m;++i) {
int aa,bb,cc;
scanf("%d%d%d",&aa,&bb,&cc);
a[aa][bb]+=cc;
}
ans=0;
int t;
while(bfs()) {
while(t=dfs(1,0x7fffff)) ans+=t;
}
printf("%d\n",ans);
}
}


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