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.
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.
5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10
50
题解:
源点为1,汇点为n,给num个流,让你求最大流。。。EK模板题目,虽然网络流刚刚入门的我不太懂但是直接按着紫书上的模板打了一遍,修改了些就ac了
模板会用就行了qwq
代码:
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<math.h>
#include<string>
#include<stdio.h>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<deque>
#include<algorithm>
using namespace std;
#define INF 100861111
#define ll long long
#define eps 1e-5
#define maxn 205
struct edge//存边
{
int from,to,cap,flow;//cap为容量,flow为流量
edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
vector<edge>ed;
vector<int>G[maxn];//存第i个点连接的第j条边的标号
int a[maxn];//存起点到i的可改进量。。不太懂,照着紫书打的
int p[maxn];//最短路树上p的入弧编号
int m,n;
void init()
{
for(int i=1;i<=n;i++)
G[i].clear();
ed.clear();
}
void addEdge(int from,int to,int cap)
{
ed.push_back(edge(from,to,cap,0));
ed.push_back(edge(to,from,0,0));//反向加边
m=ed.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
int Maxflow(int s,int t)//s为源点,t为汇点
{
int flow=0;
while(1)
{
memset(a,0,sizeof(a));
queue<int>q;
q.push(s);
a[s]=INF;
while(!q.empty())
{
int x=q.front();
q.pop();
for(int i=0;i<G[x].size();i++)
{
edge &e=ed[G[x][i]];
if(!a[e.to]&&e.cap>e.flow)
{
p[e.to]=G[x][i];
a[e.to]=min(a[x],e.cap-e.flow);
q.push(e.to);
}
}
if(a[t])
break;
}
if(!a[t])
break;
for(int e=t;e!=s;e=ed[p[e]].from)
{
ed[p[e]].flow+=a[t];
ed[p[e]^1].flow-=a[t];
}
flow+=a[t];
}
return flow;
}
int main()
{
int i,j,x,y,c,num;
while(scanf("%d%d",&num,&n)!=EOF)
{
init();
for(i=0;i<num;i++)
{
scanf("%d%d%d",&x,&y,&c);
addEdge(x,y,c);
}
printf("%d\n",Maxflow(1,n));
}
return 0;
}