hdu Drainage Ditches(网络流)
来源:互联网 发布:手机淘宝进入支付宝 编辑:程序博客网 时间:2024/06/05 11:29
Drainage Ditches
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 7 Accepted Submission(s) : 3
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 41 2 401 4 202 4 202 3 303 4 10
Sample Output
50
注意输入,
算法1:
#include<stdio.h>#include<queue>#include<string.h>#define min(x,y) x>y?y:x#define N 205#define INT_MAX 10000005using namespace std;int flow[N];int c[N][N];int p[N];int n,m;int bfs(int s,int e){ queue<int> q; for(int i=1; i<=n; i++) p[i]=-1; p[s]=0; q.push(s); flow[s]=INT_MAX; while(!q.empty()) { int temp=q.front(); q.pop(); if(temp==e) break; for(int i=1; i<=n; i++) { if(i!=temp&&c[temp][i]>0&&p[i]==-1) { flow[i]=min(flow[temp],c[temp][i]); p[i]=temp; q.push(i); } } } if(p[e]==-1) return -1; else return flow[e];}int max_flow(int s,int e){ int ans=0; int sum=0; while((ans=bfs(s,e))!=-1) { sum+=ans; int last=e; while(last!=s) { int pre=p[last]; c[pre][last]-=ans; c[last][pre]+=ans; last=pre; } } return sum;}int main(){ int aa,bb,cc; while(scanf("%d%d",&n,&m)!=EOF) { memset(c,0,sizeof(c)); memset(flow,0,sizeof(flow)); for(int i=1; i<=n; i++) { scanf("%d%d%d",&aa,&bb,&cc); if(aa==bb) continue; c[aa][bb]+=cc; } printf("%d\n",max_flow(1,m)); }}
算法2
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<iostream>#include<queue>using namespace std;#define INF 10000005int s,u;int c[1000][1000];int gap[1000];int dis[1000];void init(){ int v,x; queue<int> qu; memset(gap,0,sizeof(gap)); memset(dis,0xff,sizeof(dis)); qu.push(u); dis[u]=0; while(!qu.empty()) { x=qu.front(); qu.pop(); gap[dis[x]]++; for(v=1;v<u;v++) { if(dis[v]==-1&&c[v][x]>0) { dis[v]=dis[x]+1; qu.push(v); } } }}int max_flow(){ init(); int flow=0,top=s,i,j,k,pre[1200],low[1000]; memset(low,0,sizeof(low)); while(dis[s]<u) { int flag=0; low[s]=INF; for(i=1;i<=u;i++) if(c[top][i]>0&&dis[top]==dis[i]+1&&dis[i]>=0) { flag=1; break; } if(flag) { low[i]=c[top][i]; if(low[i]>low[top]) low[i]=low[top]; pre[i]=top; top=i; if(top==u) { flow+=low[u]; j=top; while(j!=s) { k=pre[j]; c[k][j]-=low[u]; c[j][k]+=low[u]; j=k; } top=s; memset(low,0,sizeof(low)); } } else { int min=u-1; for(j=1;j<u;j++) { if(c[top][j]>0&&dis[j]+1<min&&dis[j]>=0) min=dis[j]+1; } gap[dis[top]]--; if(gap[dis[top]]==0) break; gap[min]++; dis[top]=min; if(top!=s) top=pre[top]; } } return flow;}int main(){ int n,m; int ss,e,cc; while(scanf("%d%d",&n,&m)!=EOF) { s=1; u=m; memset(c,0,sizeof(c)); for(int i=1;i<=n;i++) { scanf("%d%d%d",&ss,&e,&cc); if(ss==e) continue; c[ss][e]+=cc; } printf("%d\n",max_flow()); } return 0;}
- hdu Drainage Ditches(网络流)
- HDU 1532 Drainage Ditches (最大网络流)
- HDU 1532 Drainage Ditches (最大网络流)
- hdu 1532 Drainage Ditches(最大网络流)
- Hdu 1532 Drainage Ditches【网络流】
- hdu 1532 Drainage Ditches(网络流)
- hdu 1532 最大网络流 Drainage Ditches
- 【网络流基础】hdu 1532 Drainage Ditches
- hdu 1533 Drainage Ditches EK 网络流
- HDU 1532 Drainage Ditches 网络流模板
- HDU 1532 Drainage Ditches 最大网络流
- 网络最大流--Drainage Ditches
- HDU1532 - Drainage Ditches(网络流)
- 【网络流第一步】Hdu 1532——Drainage Ditches
- HDU 1532 Drainage Ditches 网络流 模板题
- hdu 1532 Drainage Ditches(网络流dinic模板)
- HDU 1532 Drainage Ditches 网络流模板题(Dinic)
- HDU 1523 Drainage Ditches (网络流/dinic/模板)
- INSTALL_FAILED_DEXOPT LinearAlloc exceeded capacity
- Installshield2010实现web部署和数据库安装示例
- Loadrunner 报错: Error: The table 'E:\*性能测试脚本\login1\userName.dat' does not exist.
- 织梦(dedecms)仿站教程第六讲——通用头部(二)会员登陆框(上)
- 关于clearfix:after的解释
- hdu Drainage Ditches(网络流)
- Junk-Mail Filter(并查集,删除结点,虚父节点)
- BITMAPINFO结构详解
- 关于Linux允许远程访问mysql数据库
- Smart Pointers to boost your code
- 织梦(dedecms)仿站教程第七讲——通用头部(三)会员登陆框(下)
- Android开发 返回键的复写 onBackPressed()
- 如何基于rst书写openstack文档 ( by quqi99 )
- crosstool-ng 编译生成交叉编译链