hdu1532 Drainage Ditches(最大流+EK)
Posted changeg1824
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu1532 Drainage Ditches(最大流+EK)相关的知识,希望对你有一定的参考价值。
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25940 Accepted Submission(s): 12214
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.
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
题目大意:
n条边,m个结点,每条边有个流量的最大值,1为源点,m为汇点,最后求源点到汇点的最大流量
EK模板
1 #include <bits/stdc++.h> 2 const int INF=1e9; 3 const int maxn=300; 4 using namespace std; 5 6 int n,m,graph[maxn][maxn],pre[maxn];///graph[][]不仅记录图,还是残留网络 7 8 int bfs(int s,int t){ 9 int flow[maxn]; 10 memset(pre,-1,sizeof pre); 11 flow[s]=INF; pre[s]=0;///初始化起点 12 queue<int> Q; Q.push(s);///起点入栈,开始BFS 13 while(!Q.empty()){ 14 int u=Q.front(); Q.pop(); 15 if(u==t)break;///搜到一个路径,这次BFS结束 16 for(int i=1;i<=m;i++){///BFS所有的点 17 if(i!=s&&graph[u][i]>0&&pre[i]==-1){ 18 pre[i]=u;///记录路径 19 Q.push(i); 20 flow[i]=min(flow[u],graph[u][i]);///更新结点流量 21 } 22 } 23 } 24 if(pre[t]==-1)return -1; 25 return flow[t]; 26 } 27 28 int maxflow(int s,int t){ 29 int Maxflow=0; 30 while(1){ 31 int flow=bfs(s,t);///执行一次BFS,找到一条路径,返回路径的流量 32 if(flow==-1)break;///没有找到新的增光路,结束 33 int cur=t;///更新路径上的残留网络 34 while(cur!=s){///一直沿路径回溯到起点 35 int father=pre[cur];///pre[]记录路径上的前一个点 36 graph[father][cur]-=flow;///更新残留网络:正向减 37 graph[cur][father]+=flow;///更新残留网络:反向加 38 cur=father; 39 } 40 Maxflow+=flow; 41 } 42 return Maxflow; 43 } 44 45 int main(){ 46 while(~scanf("%d%d",&n,&m)){ 47 memset(graph,0,sizeof graph); 48 for(int i=0;i<n;i++){ 49 int u,v,w; 50 scanf("%d%d%d",&u,&v,&w); 51 graph[u][v]+=w;///可能有重边 52 } 53 printf("%d ",maxflow(1,m)); 54 } 55 }
无注释版本
1 #include <bits/stdc++.h> 2 const int INF=1e9; 3 const int maxn=300; 4 using namespace std; 5 6 int n,m,graph[maxn][maxn],pre[maxn]; 7 8 int bfs(int s,int t){ 9 int flow[maxn]; 10 memset(pre,-1,sizeof pre); 11 flow[s]=INF; pre[s]=0; 12 queue<int> Q; Q.push(s); 13 while(!Q.empty()){ 14 int u=Q.front(); Q.pop(); 15 if(u==t)break; 16 for(int i=1;i<=m;i++){ 17 if(i!=s&&graph[u][i]>0&&pre[i]==-1){ 18 pre[i]=u; 19 Q.push(i); 20 flow[i]=min(flow[u],graph[u][i]); 21 } 22 } 23 } 24 if(pre[t]==-1)return -1; 25 return flow[t]; 26 } 27 28 int maxflow(int s,int t){ 29 int Maxflow=0; 30 while(1){ 31 int flow=bfs(s,t); 32 if(flow==-1)break; 33 int cur=t; 34 while(cur!=s){ 35 int father=pre[cur]; 36 graph[father][cur]-=flow; 37 graph[cur][father]+=flow; 38 cur=father; 39 } 40 Maxflow+=flow; 41 } 42 return Maxflow; 43 } 44 45 int main(){ 46 while(~scanf("%d%d",&n,&m)){ 47 memset(graph,0,sizeof graph); 48 for(int i=0;i<n;i++){ 49 int u,v,w; 50 scanf("%d%d%d",&u,&v,&w); 51 graph[u][v]+=w; 52 } 53 printf("%d ",maxflow(1,m)); 54 } 55 }
以上是关于hdu1532 Drainage Ditches(最大流+EK)的主要内容,如果未能解决你的问题,请参考以下文章