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
题意:
池塘到河流之间有一些管道,管道的流率不同,求从池塘排水到河流的最大速率。(最大流模板题)
题目第一行输入m,n,分别表示m条管道和n个节点,其中节点1和n分别表示池塘和河流,之后m行输入a,b,c,表示节点a到节点b的速率为c;
注意:两节点之间可能有多条管道,所以速率要相加
EK算法
代码:
#include"queue"
#include"stdio.h"
#include"string.h"
#include"algorithm"
using namespace std;
#define INF 0x3f3f3f3f
int m,n;
int map[210][210],pre[210],vis[210];
bool bfs(int s,int t)//求s到t之间的一条增广路
{
int i,u;
memset(pre,-1,sizeof(pre));
memset(vis,0,sizeof(vis));
queue<int>q;
q.push(s);
while(!q.empty())
{
u=q.front();
q.pop();
for(i=1;i<=n;i++)
{
if(!vis[i]&&map[u][i]>0)
{
vis[i]=1;
pre[i]=u;//表示i节点的上一个节点是u
if(i==t)//如果此条增广路能到达汇点(t),返回1
return 1;
q.push(i);
}
}
}
return 0;
}
int EK(int s,int t)
{
int u,v,i,minn,sum=0;
while(bfs(s,t))//判断s到t之间是否存在增广路
{
u=t;
minn=INF;
while(u!=s)//找到此条增广路的中速率的最小值
{
v=pre[u];
if(map[v][u]<minn)
minn=map[v][u];
u=v;
}
sum+=minn;
u=t;
while(u!=s)//更新剩余网络
{
v=pre[u];
map[v][u]-=minn;
map[u][v]+=minn;
u=v;
}
}
return sum;
}
int main()
{
while(~scanf("%d %d",&m,&n))
{
int a,b,c,i;
memset(map,0,sizeof(map));
for(i=1;i<=m;i++)
{
scanf("%d %d %d",&a,&b,&c);
map[a][b]+=c;//速率相加
}
printf("%d\n",EK(1,n));
}
return 0;
}
Dinic算法
代码:
#include"queue"
#include"stdio.h"
#include"string.h"
#include"algorithm"
using namespace std;
#define INF 0x3f3f3f3f
int V,E,level[1010];
struct M
{
int c,f;
}p[1010][1010];
bool 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<=E;v++)
{
if(!level[v]&&p[u][v].c>p[u][v].f)
{
level[v]=level[u]+1;
q.push(v);
}
}
}
return level[E]!=0;
}
int dfs(int u,int cp)
{
int tmp=cp,v,t;
if(u==E)
return cp;
for(v=1;v<=E&&tmp;v++)
{
if(level[u]+1==level[v])
{
if(p[u][v].c>p[u][v].f)
{
t=dfs(v,min(tmp,p[u][v].c-p[u][v].f));
p[u][v].f+=t;
p[v][u].f-=t;
tmp-=t;
}
}
}
return cp-tmp;
}
int dinic()
{
int sum=0,s=0;
while(bfs())
{
while(s=dfs(1,INF))
sum+=s;
}
return sum;
}
int main()
{
while(~scanf("%d %d",&V,&E))
{
int a,b,c;
memset(p,0,sizeof(p));
while(V--)
{
scanf("%d %d %d",&a,&b,&c);
p[a][b].c+=c;
}
printf("%d\n",dinic());
}
return 0;
}