HDU1532 Drainage Ditches 网络流之dinic算法(省内存,但耗时)
来源:互联网 发布:张国荣为什么自杀知乎 编辑:程序博客网 时间:2024/05/19 02:04
HDU1532:http://acm.hdu.edu.cn/showproblem.php?pid=1532
Drainage Ditches
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 19000 Accepted Submission(s): 9058
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
输入m个点,和n条边,求从1到m的最大流。(我的代码中,n和m颠倒了)
直接上dinic的模板跑一遍即可
【代码】:#include <stdio.h>#include <math.h> #include <stdlib.h> #include <string.h> #include <iostream> #include <algorithm> #include <queue> #define mset(a,i) memset(a,i,sizeof(a))#define S1(n) scanf("%d",&n)#define S2(n,m) scanf("%d%d",&n,&m)#define P(n) printf("%d\n",n);using namespace std;typedef long long ll;const int INF=0x3f3f3f3f;const int MAX=1e6+5;int n,m,END;struct node{int s,t,cap,next;}edge[6*MAX];int head[MAX];int pre[MAX];//前驱边 int dep[MAX];//节点的标号int gap[MAX];//gap[i]表示编号i出现的次数void add(int s,int t,int cap){static int cnt=0;edge[cnt]={s,t,cap,head[s]};head[s]=cnt++;edge[cnt]={t,s,0,head[t]};head[t]=cnt++;}int BFS(){mset(dep,-1);dep[1]=0;queue<int>q;q.push(1);while(!q.empty()){int s=q.front();q.pop();for(int i=head[s];~i;i=edge[i].next){int t=edge[i].t;if(dep[t]<0&&edge[i].cap){dep[t]=dep[s]+1;q.push(t);}}}return dep[END]!=-1;}int dfs(int s,int low=INF){int cap;if(s==END)return low;for(int i=head[s];~i;i=edge[i].next){int t=edge[i].t;if(dep[t]==dep[s]+1&&edge[i].cap&&(cap=dfs(t,min(low,edge[i].cap)))){edge[i].cap-=cap;edge[i^1].cap+=cap;return cap;}}return 0;}int dinic(int s,int t){int ans=0;while(BFS()){ans+=dfs(1);}return ans;}int main(){while(~S2(m,n))//m边,n点{mset(head,-1);END=n;for(int i=0;i<m;i++){int s,t,l;S2(s,t);S1(l);add(s,t,l);}int ans=dinic(1,END);P(ans);}return 0;}
阅读全文
0 0
- HDU1532 Drainage Ditches 网络流之dinic算法(省内存,但耗时)
- HDU1532——Drainage Ditches(网络流Dinic算法)
- HDU1532 Drainage Ditches Dinic算法
- HDU1532 Drainage Ditches 网络流之SAP算法
- hdu1532 Drainage Ditches (Dinic)
- 增广路算法(网络流) HDU1532 Drainage Ditches
- Drainage Ditches(HDU1532,网络流)
- HDU1532 Drainage Ditches 最大流Dinic模板
- HDU1532 - Drainage Ditches(网络流)
- POJ 1273 Drainage Ditches(网络流【Dinic算法】)
- HDU1532 - Drainage Ditches 网络流之最大流
- HDU1532 Drainage Ditches (网络流基础水题)
- hdu1532——Drainage Ditches(网络流模板)
- HDU1532 Drainage Ditches(网络流模板)
- HDU1532 Drainage Ditches 网络流模板
- HDU1532 Drainage Ditches(网络流EdmondsKarp)
- HDU1532 Drainage Ditches(网络流、EdmondsKarp)
- hdu1532 & pku1273 Drainage Ditches 网络流(最大流EK算法)
- java多线程
- Java知识(IO流、.InputStream和OutputStream)
- tensorflow 中tf.concat()用法
- 异常处理
- MYSQL5.7 ZIP版本安装及相关问题解决
- HDU1532 Drainage Ditches 网络流之dinic算法(省内存,但耗时)
- LINUX 下载ftp文件夹下的所有内容
- 算法题练习系列之(二十九): 跟奥巴马一起编程
- HDU6180Schedule(贪心)
- HDU 6047
- Java三大器之过滤器(Filter)的工作原理和代码演示
- tensorflow-激活函数及tf.nn.dropout
- uva 116 动态规划 多阶段决策问题 路径记录 lrj-P270
- java内存区域