zoukankan      html  css  js  c++  java
  • poj 1273 Drainage Ditches

    <span style="font-size: 18pt; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">Drainage Ditches</span>
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 54939   Accepted: 20946

    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
    
    
    
    

    给出关键部分伪代码来介绍一下网络流的标号法:

    1. Repeat  
    2.     队列置空;  
    3.     全部点设为未标号;  
    4.     将源点增加队列,并标号为(0,+∞);  
    5.     while 队列非空  
    6.         {  
    7.             头指针+1  
    8.             依次检查与头指针指向的元素相连的边  
    9.             if 还有一点没有标号 and 流量可改进  
    10.                 {  
    11.                     尾指针+1,该点入队  
    12.                     a[还有一点]<-队列头指针元素  
    13.                     b[还有一点]<-Min{b[头指针元素],边的最大流量};  
    14.                 }  
    15.         }  
    16.         if 汇点已标号  
    17.             {  
    18.                 从汇点出发依次改动各条边的流量  
    19.             }  
    20. Until 汇点未标号;  
    21. 答案<-全部与汇点相连的边的流量  
    EK算法:是一种最短路径增值的算法。通过不断从源点广搜寻找最短路径,然后记录路径中的最小容量。再给这条路径上的边上flow增值,(增值之后当然会有一部分边是满流的,那么再次广搜的时候当然也就不能正向搜索到此边了,这条路径上的边的流量都增大了,容量不变,可增值量当然也就会降低),直到从源点广搜不到汇点为止,来实现最大流。

    因为每次都要广搜所以时间复杂度会达到O(m*m+n),m为边的个数。n为点的个数。

    
    
    
    
    
    
    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<string.h>
    #include<queue>
    #include<limits.h>
    #define MAX 250
    using namespace std;
    int cap[MAX][MAX];
    int m;
    int EKarp(int s,int t)
    {
    	queue<int> Q;
    	int flow[MAX][MAX],a[MAX],u,v,f,pre[MAX];
    	f=0;
    	memset(flow,0,sizeof(flow));
    	while(1)
    	{
    		Q.push(s);
    		memset(a,0,sizeof(a));
    		a[s]=INT_MAX;
    		while(!Q.empty())
    		{
    			u=Q.front();
    			Q.pop();
    			for(v=1;v<=m;v++)
    				if(!a[v]&&cap[u][v]>flow[u][v])
    				{
    					
    					Q.push(v);
    					a[v]=a[u]<cap[u][v]-flow[u][v]?a[u]:cap[u][v]-flow[u][v];
    					pre[v]=u;
    				}
    		}
    		if(a[t]==0)break;
    		for(u=t;u!=s;u=pre[u])
    		{
    			flow[pre[u]][u]+=a[t];
    			flow[u][pre[u]]-=a[t];
    		}
    		f+=a[t];
    	}
    	return f;
    }
    int main()
    {
    	int from,to,c,n,ans;
    	while(~scanf("%d%d",&n,&m))
    	{
    		memset(cap,0,sizeof(cap));
    		while(n--)
    		{
    			scanf("%d%d%d",&from,&to,&c);
    			cap[from][to]+=c;
    		}
    		ans=EKarp(1,m);
    		printf("%d
    ",ans);
    	}
    	return 0;
    }
    
    
    
    
       
    
  • 相关阅读:
    强化学习_PolicyGradient(策略梯度)_代码解析
    leetcode_1053. Previous Permutation With One Swap
    leetcode_1052. Grumpy Bookstore Owner
    Tensorflow_入门学习_2_一个神经网络栗子
    通过批处理快速设置IP
    汇编、编译、反汇编、反编译的简单概念介绍
    Logistic回归
    基于概率论的分类方法:朴素贝叶斯
    决策树 预测隐形眼镜类型
    k-近邻算法2(kNN)手写识别系统
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/5053643.html
Copyright © 2011-2022 走看看