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.
#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;
int s,t,m,n;
struct Node//邻接表存边
{
int v;//下一条边
int val;//边权 (流量)
int next;
}node[200005];
int s,t,m,n;
struct Node//邻接表存边
{
int v;//下一条边
int val;//边权 (流量)
int next;
}node[200005];
int top=1,head[200005];// top必须从奇数开始
//head存边所对应的链节头
struct Pre//存路径
{
int v;//该点的前一个点的下标(从起点来)
int edge;//与该点相连的边
}pre[200005];
bool vis[200005];
void add(int from,int to,int val)
{
top++;
node[top].val=val;
node[top].v=to;
node[top].next=head[from];
head[from]=top;
}
bool bfs()//广搜确定是此时否存在路
{
queue<int> q;
memset(vis,0,sizeof(vis));
memset(pre,-1,sizeof(pre));
vis[s]=1;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i;i=node[i].next)
{
int d=node[i].v;
if(!vis[d]&&node[i].val)
{
pre[d].v=u;
pre[d].edge=i;
if(d==t) return 1;
vis[d]=1;
q.push(d);
}
}
}
return 0;
}
int EK() //EK 算法求最大流
{
int ans=0;
while(bfs())
{
int mint=1000000009;
for(int i=t;i!=s;i=pre[i].v)
mint=min(mint,node[pre[i].edge].val);
for(int i=t;i!=s;i=pre[i].v)
{
node[pre[i].edge].val-=mint;
node[pre[i].edge^1].val+=mint;//更新反向边
}
ans+=mint;
}
return ans;
}
int main()
{
while(~scanf("%d%d",&m,&n))
{
s=1;
t=n;
top=1;
memset(head,0,sizeof(head));
memset(node,0,sizeof(node));
while(m--)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);//正向建边
add(v,u,0);//反向建边
}
printf("%d\n",EK());
}
return 0;
}
来源:https://blog.csdn.net/qq_43710815/article/details/102732880