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

165人阅读 评论(0)

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];
void addedge(int from,int to,int cap)
{
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;
}
void addedge(int from,int to,int cap)
{
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;
}


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：113486次
• 积分：4948
• 等级：
• 排名：第6366名
• 原创：396篇
• 转载：11篇
• 译文：0篇
• 评论：21条
最新评论