# hdu 1532/poj 1273 Drainage Ditches（最大流模板）

Drainage Ditches
 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 67890 Accepted: 26235

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

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
#define N 1111
#define INF 0x3f3f3f3f
struct Node
{
int to;
int cap;///容量
int next;
int rev;///反向边序
}edge[N];
{
edge[cnt].to=to;edge[cnt].cap=cap;
edge[cnt].to=from;edge[cnt].cap=0;
}
int dfs(int s,int t,int f)
{
if(s==t) return f;
used[s]=1;
{
Node &temp=edge[i];
if(!used[temp.to]&&temp.cap>0)
{
int d=dfs(temp.to,t,min(f,temp.cap));
if(d>0)
{
temp.cap-=d;
edge[temp.rev].cap+=d;
return d;
}
}
}
return 0;
}
int max_flow(int s,int t)
{
int flow=0;
while(1)
{
memset(used,0,sizeof(used));
int d=dfs(s,t,INF);
if(d==0) return flow;
flow+=d;
}
}
int main()
{
int n,m;
int from,to,cap;
while(~scanf("%d %d",&m,&n))
{
cnt=0;
for(int i=0; i<m; i++)
{
scanf("%d %d %d",&from,&to,&cap);
}
printf("%d\n",max_flow(1,n));
}
return 0;
}


Dinic算法模板：

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define N 1111
#define INF 0x3f3f3f3f
struct Node
{
int to,cap,next;
}edge[N];
int d[N];
int cnt;
void init()
{
cnt=0;
}
{
edge[cnt].to=to;edge[cnt].cap=cap;
edge[cnt].to=from;edge[cnt].cap=0;
}
int bfs(int s,int t)
{
memset(d,-1,sizeof(d));
queue<int> q;
q.push(s);
d[s]=0;
while(!q.empty())
{
int u=q.front();
q.pop();
{
int v=edge[i].to;
if(d[v]==-1&&edge[i].cap>0)
{
d[v]=d[u]+1;

q.push(v);
}
}
}
return d[t]!=-1;
}
int dfs(int s,int t,int f)
{
if(s==t||f==0) return f;
int flow=0;
{
int v=edge[i].to;
if(d[v]==d[s]+1&&edge[i].cap>0)
{
int x=min(f-flow,edge[i].cap);
x=dfs(v,t,x);
flow+=x;
edge[i].cap-=x;
edge[i^1].cap+=x;
}
}
if(!flow) d[s]=-2;
return flow;
}
int Dinic(int s,int t)///起点s终点t
{
int flow=0,f;
while(bfs(s,t))
{
while(f=dfs(s,t,INF))
flow+=f;
}
return flow;
}
int main()
{
int n,m;
int from,to,cap;
while(~scanf("%d %d",&m,&n))
{
init();
for(int i=0;i<m;i++)
{
scanf("%d %d %d",&from,&to,&cap);
}
printf("%d\n",Dinic(1,n));
}
return 0;
}


#### hdu1532Drainage Ditches 最大流模板水题

2015-08-10 16:37:19

#### poj-1273-Drainage Ditches(最大流问题)

2016-04-03 19:46:04

#### hdoj 1532 Drainage Ditches 题解（最大流）

2014-03-27 11:27:18

#### 【网络流之最大流】POJ1273-Drainage Ditche【模板题】

2015-08-29 11:21:44

#### POJ_1273_Drainage Ditches（USACO 93）_最大流

2016-06-13 09:39:08

#### HDU 1532 Drainage Ditches【最大流入门题， EdmondKarp算法】

2016-11-04 17:58:07

#### HDU-1532 Drainage Ditches (最大流E-K算法)

2016-11-07 14:32:11

#### HDU_1532 && HDU_3549(最大流EK算法模板)

2015-09-08 21:06:35

#### hdu 1532 Drainage Ditches （网络流两种方法）

2014-03-04 17:54:56

#### HDU--杭电--1532--Drainage Ditches--最大流

2014-02-02 20:34:18