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
模板题,好好体会反向边的添加(提供反悔的机会) 参考博客:http://www.cnblogs.com/zsboy/archive/2013/01/27/2878810.html
EK 算法: 每次增广都是通过bfs,通过DFS仅可以寻找一条路线 而Dinic DFS寻找的是多条路线,比较快捷
#include <cstdio> #include <algorithm> #include <cstring> #include <queue> #define inf 0x3f3f3f3f using namespace std; int n,m, path[209][209],flow[209],pre[209];//记录残留网络正向和反向流量,到某个节点的流量,记录前驱结点 int bfs(int src,int des) { memset(pre,-1,sizeof pre); pre[src]=0; flow[src]=inf; queue<int> qq; qq.push(src); while(!qq.empty()) { int t=qq.front(); qq.pop(); if(t==des) break; for(int i=1; i<=m; i++) { if(i!=src&&path[t][i]>0&&pre[i]==-1)//很重要的判断条件,原点是不可能有前驱结点的,只有当路径中剩余流量大于0才可,且此节点未被访问过 { pre[i]=t; qq.push(i); flow[i]=min(flow[t],path[t][i]);//更新节点流量 } } } if(pre[des]==-1)//证明没有找到增广路径 return -1; return flow[des]; } int maxflow(int src,int des) { int sum=0,add=0; while((add=bfs(src,des))!=-1)//括号!!!优先级问题导致超时!!! { int cur=des; while(cur!=src)//每次只能找到一条路线 { int last=pre[cur]; path[last][cur]-=add;//更新残余网络的流量图正向减,反向加 path[cur][last]+=add; cur=last; } sum+=add; } return sum; } int main() { while(~scanf("%d%d",&n,&m)) { memset(path,0,sizeof path); for(int i=0; i<n; i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); if(a==b) continue; path[a][b]+=c; } printf("%d\n",maxflow(1,m)); } return 0; }Dinic算法会更快一些: 用bfs将图分层,每次分层后都是最短路线,然后用dfs寻找增广路线;
#include <cstdio> #include <algorithm> #include <cstring> #include <queue> #define inf 0x3f3f3f3f using namespace std; const int maxn=209; int mp[maxn][maxn],level[maxn],n,m;//邻接矩阵,离原点的距离 int bfs(int src,int des)//增广路线,分层图 { memset(level,-1,sizeof level); level[src]=0; queue<int> q; q.push(src); while(!q.empty()) { int t=q.front(); q.pop(); for(int i=1; i<=m; i++) { if(mp[t][i]>0&&level[i]==-1) { level[i]=level[t]+1; q.push(i); } } } return level[des]; } int dfs(int x,int flow)//寻找曾广路,保存上一节点和经过上一节点的流量 { int a; if(x==m) return flow; for(int i=1; i<=m; i++)//每次可寻找多条路线 { if(level[i]==level[x]+1&&mp[x][i]>0&&(a=dfs(i,min(mp[x][i],flow)))) { mp[i][x]+=a; mp[x][i]-=a; return a; } } return 0; } int maxflow() { int ans=0,add=0; while(bfs(1,m)!=-1)//一直分层图 { while((add=dfs(1,inf)))//一直寻找増广路,直至找不到再将图分层 ans+=add; } return ans; } int main() { while(~scanf("%d%d",&n,&m)) { memset(mp,0,sizeof mp); for(int i=0; i<n; i++) { int s,t,c; scanf("%d%d%d",&s,&t,&c); mp[s][t]+=c; } printf("%d\n",maxflow()); } return 0; }