一道增广路网络流的模板题,但是毕竟是初学网络流,还是在一些细节上WA、T了几发。
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 <= 500) and M (2 <= M <= 500). 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 <= 1,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.
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
2 2
1 2 100
1 2 200
50
300
思路:
我通过BFS找增广路的方式来不断更新残余网络的流量,每次如果能从st点遍历到ed点,就说明还可以更新,直到不能更新,就是说明已经是最大流了。每次还要通过前驱节点更新正向边(减)和逆向边(加)的残余网络的流量值。
完整代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
using namespace std;
typedef long long ll;
const ll INF=(ll)1<<58;
const int maxN=505;
int N,M;
ll r[maxN][maxN]; //残余网络的流量
ll flow[maxN]; //到目前点的流量
int pre[maxN]; //前驱
queue<int> Q;
ll Bfs(int st, int ed)
{
fill(flow, flow+maxN, INF);
while(!Q.empty()) Q.pop();
for(int i=1; i<=M; i++) pre[i]=-1;
pre[st]=0;
flow[st]=INF;
Q.push(st);
while(!Q.empty())
{
int u=Q.front();
Q.pop();
if(u==ed) break;
for(int i=1; i<=M; i++)
{
if(i!=st && r[u][i]>0 && pre[i]==-1)
{
pre[i]=u; //前驱
flow[i]=min(r[u][i], flow[u]); //增量
Q.push(i);
}
}
}
return pre[ed]==-1?(-1):flow[ed]; //有没有增广路,有的话就加上增广路流量
}
ll maxFlow(int st, int ed)
{
ll incr=0;
ll sumflow=0;
while((incr=Bfs(st, ed))!=-1)
{
int k=ed; //通过前驱寻找路径
while(k!=st)
{
int last=pre[k];
r[last][k]-=incr; //正向边
r[k][last]+=incr; //逆向边
k=last;
}
sumflow+=incr;
}
return sumflow;
}
int main()
{
while(scanf("%d%d",&N,&M)!=EOF)
{
memset(r, 0, sizeof(r));
//memset(flow, 0, sizeof(flow)); //一定是初始化为0,不然二次操作会出问题(不然就是放在BFS中Fill(INF))
//fill(flow, flow+maxN, INF);
for(int i=1; i<=N; i++)
{
int e1,e2,e3;
scanf("%d%d%d",&e1,&e2,&e3);
if(e1==e2) continue;
r[e1][e2]+=e3;
}
printf("%lld\n",maxFlow(1, M));
}
return 0;
}
再补上网络流的Dinic算法:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define INF 1e9+7
#define pi 3.141592653589793
#define e 2.718281828459045
using namespace std;
typedef long long ll;
const int maxN=205;
int N,M;
int flow[maxN][maxN], dis[maxN]; //dis指的是到源点的层数
bool bfs()
{
memset(dis, -1, sizeof(dis));
dis[1]=0;
queue<int> Q;
Q.push(1); //源点
while(!Q.empty())
{
int u=Q.front();
Q.pop();
for(int i=1; i<=M; i++)
{
if(flow[u][i]>0 && dis[i]==-1)
{
dis[i]=dis[u]+1;
Q.push(i);
}
}
}
return dis[M]!=-1;
}
int dfs(int x, int maxflow_X) //点、路径上的最小流量
{
if(x==M) return maxflow_X;
int k;
for(int i=1; i<=M; i++)
{
if(flow[x][i]>0 && dis[i]==dis[x]+1 && (k=dfs(i, min(maxflow_X, flow[x][i]))) )
{
flow[x][i]-=k;
flow[i][x]+=k;
return k;
}
}
return 0;
}
int main()
{
while(scanf("%d%d",&N,&M)!=EOF)
{
memset(flow, 0, sizeof(flow));
int e1,e2,e3;
for(int i=0; i<N; i++)
{
scanf("%d%d%d",&e1,&e2,&e3);
flow[e1][e2]+=e3;
}
int ans=0, res;
while(bfs())
{
while( (res=dfs(1, INF)) ) ans+=res;
}
printf("%d\n", ans);
}
return 0;
}