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 0x3f3f3f3fusing 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 0x3f3f3f3fusing 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;}