Drainage Ditches
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 53640 | Accepted: 20448 |
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
Source
n是点数,m是边数
EK算法:
O(n*m*m)
效率最低,没有任何优化,直接吧标号法实现。
算法思想:一直重复搜索增广路径的过程,知道不存在增广路径。而此处对于增广路径的搜索,则采用了邻接矩阵的方法,耗时比较大,使用邻接表会更清晰的表现出标号法的过程。
1 //312K 0MS C++ 1400B 2014-05-03 17:50:04 2 //最大流模板题 3 #include<iostream> 4 #include<queue> 5 #define INF 0x7ffffff 6 #define N 205 7 using namespace std; 8 int flow[N],vis[N]; 9 int g[N][N],father[N]; 10 int maxflow,n,m; 11 int Min(int a,int b) 12 { 13 return a<b?a:b; 14 } 15 void EK(int s,int e) 16 { 17 queue<int>Q; 18 while(1){ 19 memset(vis,0,sizeof(vis)); 20 memset(flow,0,sizeof(flow)); 21 Q.push(s); 22 flow[s]=INF; 23 while(!Q.empty()){ 24 int u=Q.front(); 25 Q.pop(); 26 for(int v=1;v<=n;v++){ 27 if(!vis[v] && g[u][v]>0){ 28 vis[v]=1; 29 father[v]=u; 30 Q.push(v); 31 flow[v]=Min(flow[u],g[u][v]); 32 } 33 } 34 if(flow[e]>0){ 35 while(!Q.empty()) Q.pop(); 36 break; 37 } 38 } 39 if(flow[e]==0) break; 40 for(int i=e;i!=s;i=father[i]){ 41 g[father[i]][i]-=flow[e]; 42 g[i][father[i]]+=flow[e]; 43 } 44 maxflow+=flow[e]; 45 } 46 } 47 int main(void) 48 { 49 int a,b,c; 50 while(scanf("%d%d",&m,&n)!=EOF) 51 { 52 memset(g,0,sizeof(g)); 53 maxflow=0; 54 while(m--){ 55 scanf("%d%d%d",&a,&b,&c); 56 g[a][b]+=c; 57 } 58 EK(1,n); 59 printf("%d ",maxflow); 60 } 61 return 0; 62 }
dinic算法:
O(n*n*m)(如果用bfs实现增广路径的话则是O(n*m*m),好像是另一个算法了)
效率比EK高,因为多了优化。
算法思想:重复搜索增广路径的动作,而判定条件改为直到不能分层为止,能避免很多不必要的过程。(因为偷懒所以用了STL实现队列和邻接表)
1 //15MS 340K 1496 B G++ 2 #include<iostream> 3 #include<queue> 4 #include<vector> 5 #define N 205 6 #define INF 0x7ffffff 7 using namespace std; 8 struct node{ 9 int v,c; 10 node(int a,int b){ 11 v=a;c=b; 12 } 13 }; 14 int n,m; 15 vector<node>V[N]; 16 int dis[N]; 17 queue<int>Q; 18 bool bfs() //分层 19 { 20 memset(dis,-1,sizeof(dis)); 21 dis[1]=0; 22 int u=1; 23 Q.push(u); 24 while(!Q.empty()){ 25 u=Q.front(); 26 Q.pop(); 27 for(int i=0;i<V[u].size();i++){ 28 if(dis[V[u][i].v]==-1 && V[u][i].c>0){ 29 dis[V[u][i].v]=dis[u]+1; 30 Q.push(V[u][i].v); 31 } 32 } 33 } 34 return dis[n]>=0; 35 } 36 int dfs(int u,int minn) //增广 37 { 38 if(u==n) return minn; 39 for(int i=0;i<V[u].size();i++){ 40 if(dis[V[u][i].v]==dis[u]+1 && V[u][i].c>0){ 41 int c=V[u][i].c; 42 minn=minn<c?minn:c; 43 int ans=dfs(V[u][i].v,minn); 44 if(ans>0){ 45 V[u][i].c-=ans; 46 return ans; 47 } 48 } 49 } 50 return 0; 51 } 52 int dinic() 53 { 54 int ans=0; 55 while(bfs()){ 56 ans+=dfs(1,INF); 57 //printf("*%d ",ans); 58 } 59 return ans; 60 } 61 int main(void) 62 { 63 int u,v,c; 64 while(scanf("%d%d",&m,&n)!=EOF) 65 { 66 for(int i=0;i<=n;i++) 67 V[i].clear(); 68 for(int i=0;i<m;i++){ 69 scanf("%d%d%d",&u,&v,&c); 70 V[u].push_back(node(v,c)); 71 } 72 printf("%d ",dinic()); 73 } 74 return 0; 75 }
ISAP:
O(n*n*m)
算法思想:这个算法看的时间比较长,因为比较难找到合理化的解释,第一次基本是套用别人的模板,现在写一下自己的理解。
算法复杂度上的分析我理解的不是很透彻,因此先借用别人的结果。而ISAP算法就分为几部分,开始先逆向BFS初始化距离标号,然后进入ISAP主函数,和标号法的思想差不多,(1)一直循环查找增广路径;(2)找到增广路径时则进行augment()处理,找不到时则回退retreat()处理;(3)当出现断层或则d[source]>=n时算法结束。
推荐一个网站:http://blog.csdn.net/yuhailin060/article/details/4897608
伪代码:
1 algorithm Improved-Shortest-Augmenting-Path 2 1 f <-- 0 3 2 从终点 t 开始进行一遍反向 BFS 求得所有顶点的起始距离标号 d(i) 4 3 i <-- s 5 4 while d(s) < n do 6 5 if i = t then // 找到增广路 7 6 Augment 8 7 i <-- s // 从源点 s 开始下次寻找 9 8 if Gf 包含从 i 出发的一条允许弧 (i,j) 10 9 then Advance(i) 11 10 else Retreat(i) // 没有从 i 出发的允许弧则回退 12 11 return f 13 14 procedure Advance(i) 15 1 设 (i,j) 为从 i 出发的一条允许弧 16 2 pi(j) <-- i // 保存一条反向路径,为回退时准备 17 3 i <-- j // 前进一步,使 j 成为当前结点 18 19 procedure Retreat(i) 20 1 d(i) <-- 1 + min{d(j):(i,j)属于残量网络Gf} // 称为重标号的操作 21 2 if i != s 22 3 then i <-- pi(i) // 回退一步 23 24 procedure Augment 25 1 pi 中记录为当前找到的增广路 P 26 2 delta <-- min{rij:(i,j)属于P} 27 3 沿路径 P 增广 delta 的流量 28 4 更新残量网络 Gf
题目解决的实现:
邻接矩阵:
1 //15MS 532K 2612 B G++ 2 #include<stdio.h> 3 #include<string.h> 4 #define N 202 5 #define inf 0x7fffffff 6 int n,m,source,sink; 7 int g[N][N],f[N][N]; 8 int pi[N]; //增广路径 9 int curnode[N]; //当前节点,优化循环 10 11 int queue[N]; //队列 12 13 int d[N]; //距离标号 14 int num[N]; //num[i] 为 d[j]=i 的数量,记录是否断层,断层结束算法 15 16 int bfs() //逆向bfs初始化距离标号 17 { 18 int head=0,tail=0; 19 for(int i=1;i<=n;i++) 20 num[d[i]=n]++; 21 22 num[n]--; 23 d[sink]=0; 24 num[0]++; 25 26 queue[++tail]=sink; 27 28 while(head!=tail){ 29 int u=queue[++head]; 30 31 for(int i=1;i<=n;i++){ 32 if(d[i]<n || g[i][u]==0) continue; 33 34 queue[++tail]=i; 35 36 num[n]--; 37 d[i]=d[u]+1; 38 num[d[i]]++; 39 } 40 } 41 return 0; 42 } 43 44 int augment() //处理增广路径 45 { 46 int width=inf; 47 for(int i=sink,j=pi[i];i!=source;i=j,j=pi[j]){ 48 if(g[j][i]<width) width=g[j][i]; 49 } 50 51 for(int i=sink,j=pi[i];i!=source;i=j,j=pi[j]){ 52 g[j][i]-=width; f[j][i]+=width; 53 g[i][j]+=width; f[i][j]-=width; 54 } 55 56 return width; 57 } 58 59 int retreat(int &i) //处理回退,重标号 60 { 61 int temp; 62 int mind=n-1; 63 for(int j=1;j<=n;j++) 64 if(g[i][j]>0 && d[j]<mind) 65 mind=d[j]; 66 67 temp=d[i]; 68 69 num[d[i]]--; 70 d[i]=mind+1; 71 num[d[i]]++; 72 73 if(i!=source) i=pi[i]; 74 75 return num[temp]; 76 } 77 78 int maxflow(int s,int e) //ISAP求最大流 79 { 80 int i,j,flow=0; 81 82 source=s; 83 sink=e; 84 bfs(); 85 //for(int i=1;i<=n;i++) printf("*%d ",d[i]); 86 for(i=1;i<=n;i++) curnode[i]=1; 87 88 i=source; 89 90 for( ; d[source]<n ; ){ //主循环 91 for(j=curnode[i];j<=n;j++) //查找允许弧 92 if(g[i][j]>0 && d[i]==d[j]+1) 93 break; 94 if(j<=n){ //存在允许弧 95 curnode[i]=j; 96 pi[j]=i; 97 i=j; 98 99 if(i==sink){ //找到增广路径 100 flow+=augment(); 101 //printf("*%d %d ",pi[i],flow); 102 i=source; 103 } 104 105 }else{ //不存在允许弧 106 curnode[i]=1; 107 if(retreat(i)==0) break; 108 } 109 } 110 return flow; 111 } 112 113 int main(void) 114 { 115 int a,b,c; 116 while(scanf("%d%d",&m,&n)!=EOF) 117 { 118 memset(g,0,sizeof(g)); 119 memset(f,0,sizeof(f)); 120 for(int i=0;i<m;i++){ 121 scanf("%d%d%d",&a,&b,&c); 122 g[a][b]+=c; 123 } 124 printf("%d ",maxflow(1,n)); 125 } 126 return 0; 127 }
邻接表:
1 //15MS 320K 1811 B G++ 2 #include<iostream> 3 #define N 1005 4 #define inf 0x7fffffff 5 using namespace std; 6 struct node{ 7 int v; 8 int c; 9 int next; 10 }edge[N]; 11 12 int head[N],eid; 13 int n,m; 14 15 int h[N]; //距离标号 16 int gap[N]; //记录是否断层 17 18 void addedge(int u,int v,int c) //有向图 19 { 20 edge[eid].v=v; 21 edge[eid].c=c; 22 edge[eid].next=head[u]; 23 head[u]=eid++; 24 edge[eid].v=u; 25 edge[eid].c=0; 26 //edge[eid].c=c; //有向图 27 edge[eid].next=head[v]; 28 head[v]=eid++; 29 } 30 int dfs(int u,int tc) 31 { 32 if(u==n) return tc; //找到增广路径 33 int c0; 34 int minh=n-1; 35 int temp=tc; 36 for(int i=head[u];i!=-1;i=edge[i].next){ //遍历与u有关的弧 37 int v=edge[i].v; 38 int c=edge[i].c; 39 if(c>0){ 40 if(h[v]+1==h[u]){ //可行弧 41 c0=temp<c?temp:c; 42 c0=dfs(v,c0); 43 edge[i].c-=c0; 44 edge[i^1].c+=c0; 45 temp-=c0; 46 if(h[1]>=n) return tc-temp; 47 if(temp==0) break; 48 } 49 if(h[v]<minh) minh=h[v]; //更新最短距离标号 50 } 51 } 52 if(temp==tc){ //不存在可行弧,回退操作 53 --gap[h[u]]; 54 if(gap[h[u]]==0) h[1]=n; //出现断层 55 h[u]=minh+1; 56 ++gap[h[u]]; 57 } 58 return tc-temp; 59 } 60 int ISAP() //isap算法 61 { 62 int ans=0; 63 memset(gap,0,sizeof(gap)); 64 memset(h,0,sizeof(h)); 65 gap[1]=n; 66 while(h[1]<n){ 67 ans+=dfs(1,inf); 68 } 69 return ans; 70 } 71 int main(void) 72 { 73 int u,v,c; 74 while(scanf("%d%d",&m,&n)!=EOF) 75 { 76 memset(head,-1,sizeof(head)); 77 eid=0; 78 for(int i=0;i<m;i++){ 79 scanf("%d%d%d",&u,&v,&c); 80 addedge(u,v,c); 81 } 82 printf("%d ",ISAP()); 83 } 84 return 0; 85 } 86