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

hdu 1532 Drainage Ditches (最大流 : EK && Dinic &&Dinic 当前弧优化 4000 )

2017-09-22 21:11 459 查看


Drainage Ditches

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 19338    Accepted Submission(s): 9234


Problem 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

 

Recommend

lwg

EK 解法 

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
queue<int>Q;
#define inf 0x3f3f3f3f
#define N 1005
int Flow

;
int pre
;
int n,m;
bool bfs(int Start, int End)
{
memset(pre,-1,sizeof(pre));
while(!Q.empty())Q.pop();
pre[Start] = 0;
Q.push(Start);
while(!Q.empty())
{
int index = Q.front();
Q.pop();
for(int i = 1; i <= n; i++)
{
if(pre[i] == -1 && Flow[index][i] > 0)
{
pre[i] = index;
if(i == End)
return true;
Q.push(i);
}
}
}
return false;
}
int MaxFlow(int Start, int End)
{
int maxflow = 0;
while(bfs(Start,End))
{
int minflow = inf;
for(int i = End; i != Start ;i=pre[i])
{
minflow = min(minflow,Flow[pre[i]][i]);
}
for(int i = End; i != Start ;i=pre[i])
{
Flow[pre[i]][i] -= minflow;
Flow[i][pre[i]] += minflow;
}
maxflow += minflow;
//printf("%d\n",minflow);
}
return maxflow;
}
int main(){
while(scanf("%d%d" ,&n, &m)==2)
{
int u,v,f;
memset(Flow,0,sizeof(Flow));
for(int i=1;i<=n;i++)
{
scanf("%d%d%d",&u,&v,&f);
Flow[u][v]+=f;
}
printf("%d\n",MaxFlow(1,m));
}
}


Dinic 解法

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define N 1005
#define inf 0x3f3f3f3f
using namespace std;

struct Dinic
{
int c,f;
}edge

;
int n,m;
int level
;
bool dinic_bfs()///分层
{
queue<int>q;
memset(level,0,sizeof(level));
q.push(1);
level[1] = 1;
int u, v;
while(!q.empty())
{
u = q.front();
q.pop();
for(v=1;v<=n;v++)
{
if(!level[v]&&edge[u][v].c>edge[u][v].f)
{
level[v]=level[u]+1;
q.push(v);
}
}
}
return level
!=0;
}
int dinic_dfs(int u, int cp)///找增广路
{
int tmp = cp;
int v,t;
if(u==n)return cp;
for(int v = 1; v <= n&&tmp; v++)///tmp 等于 0 的时候 跳出 不跳出会tle 的。。或者cp == 0  的时候 return
{
if(level[u]+1 == level[v])
{
t=dinic_dfs(v,min(tmp,edge[u][v].c-edge[u][v].f));
edge[u][v].f+=t;
edge[v][u].f-=t;
tmp-=t;
}
}
return cp - tmp;
}
int dinic()
{
int sum ,tmp;
sum = tmp = 0;
while(dinic_bfs())
{
while(tmp = dinic_dfs(1,inf))
sum += tmp;
}
return sum;
}
int main()
{
while(scanf("%d%d",&m,&n)==2)
{
memset(edge,0,sizeof(edge));
while(m--)
{
int u,v,f;
scanf("%d%d%d",&u,&v,&f);
edge[u][v].c+=f;
}
printf("%d\n",dinic());
}
}


Dinic 当前弧优化

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
#define N 1005
#define inf 0x3f3f3f3f
int cur
;///记录循环到那一条边了。。
int Head
;///记录以该点为起点的最近的一条边的编号
int Next
;///记录每条边与之相连的上一条边的编号
int W
;///该边的花费
int V
;///每条边到达的点
int Depth
;
int cnt;///边的编号
void init()
{
cnt=-1;
memset(Head,-1,sizeof(Head));
memset(Next,-1,sizeof(Next));
}
void _Add(int u,int v,int w)
{
cnt++;
Next[cnt]=Head[u];
Head[u]=cnt;
V[cnt]=v;
W[cnt]=w;
}
void Add_Edge(int u, int v, int w)
{
_Add(u,v,w);
_Add(v,u,0);
}
int dfs(int u,int flow,int Aim)
{
if(u == Aim)
{
return flow;
}
for(int &i = cur[u]; i!=-1; i=Next[i])
{
if(Depth[V[i]] == Depth[u]+1&&W[i]!=0)
{
int tmp = dfs(V[i],min(flow,W[i]),Aim);
if(tmp > 0)
{
W[i] -= tmp;
W[i^1] += tmp;
return tmp;
}
}
}
return 0;
}
int bfs(int Start,int End)
{
queue<int>Q;
while(!Q.empty())Q.pop();
memset(Depth,0,sizeof(Depth));
Depth[Start] = 1;
Q.push(Start);
while(!Q.empty())
{
int u = Q.front();
Q.pop();
for(int i = Head[u]; i != -1; i=Next[i])
{
if(Depth[V[i]]==0&&W[i]>0)
{
Depth[V[i]]=Depth[u]+1;
Q.push(V[i]);
}
}
}
if(Depth[End]>0)return 1;
else return 0;
}
int Dinic(int Start,int End)
{
int Flow = 0;
while(bfs(Start,End))
{
for(int i=0;i<cnt;i++)
cur[i] = Head[i];
while(int Tmp_Flow = dfs(Start,inf,End))
Flow += Tmp_Flow;
}
return Flow;
}
int main()
{
int n,m;
while(~scanf("%d%d",&m,&n))
{
init();
for(int i=1;i<=m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
Add_Edge(u,v,w);
}
printf("%d\n",Dinic(1,n));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: