hdoj 1532 Drainage Ditches【最大流模板题】
Drainage Ditches
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13064 Accepted Submission(s):
6196
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.
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.
rate at which water may emptied from the pond.
- #include<stdio.h>
- #include<string.h>
- #include<stack>
- #include<queue>
- #include<algorithm>
- #define MAX 1100
- #define MAXM 40010
- #define INF 0x7fffff
- using namespace std;
- struct node
- {
- int from,to,cap,flow,next;
- }edge[MAXM];
- int n,f,d,m;
- int ans,head[MAX];
- int vis[MAX];//用bfs求路径时判断当前点是否进队列,
- int dis[MAX];//当前点到源点的距离
- int cur[MAX];//保存该节点正在参加计算的弧避免重复计算
- void init()
- {
- ans=0;
- memset(head,-1,sizeof(head));
- }
- void add(int u,int v,int w)
- {
- node E1={u,v,w,0,head[u]};
- edge[ans]=E1;
- head[u]=ans++;
- node E2={v,u,0,0,head[v]};
- edge[ans]=E2;
- head[v]=ans++;
- }
- void getmap()
- {
- int a,b,c,i;
- while(n--)
- {
- scanf("%d%d%d",&a,&b,&c);
- add(a,b,c);
- }
- }
- int bfs(int beg,int end)
- {
- int i;
- memset(vis,0,sizeof(vis));
- memset(dis,-1,sizeof(dis));
- queue<int>q;
- while(!q.empty())
- q.pop();
- vis[beg]=1;
- dis[beg]=0;
- q.push(beg);
- while(!q.empty())
- {
- int u=q.front();
- q.pop();
- for(i=head[u];i!=-1;i=edge[i].next)//遍历所有的与u相连的边
- {
- node E=edge[i];
- if(!vis[E.to]&&E.cap>E.flow)//如果边未被访问且流量未满继续操作
- {
- dis[E.to]=dis[u]+1;//建立层次图
- vis[E.to]=1;//将当前点标记
- if(E.to==end)//如果当前点搜索到终点则停止搜索 返回1表示有从原点到达汇点的路径
- return 1;
- q.push(E.to);//将当前点入队
- }
- }
- }
- return 0;//返回0表示未找到从源点到汇点的路径
- }
- int dfs(int x,int a,int end)//把找到的这条边上的所有当前流量加上a(a是这条路径中的最小残余流量)
- {
- //int i;
- if(x==end||a==0)//如果搜索到终点或者最小的残余流量为0
- return a;
- int flow=0,f;
- for(int& i=cur[x];i!=-1;i=edge[i].next)//i从上次结束时的弧开始
- {
- node& E=edge[i];
- if(dis[E.to]==dis[x]+1&&(f=dfs(E.to,min(a,E.cap-E.flow),end))>0)//如果
- {//bfs中我们已经建立过层次图,现在如果 dis[E.to]==dis[x]+1表示是我们找到的路径
- //如果dfs>0表明最小的残余流量还有,我们要一直找到最小残余流量为0
- E.flow+=f;//正向边当前流量加上最小的残余流量
- edge[i^1].flow-=f;//反向边
- flow+=f;//总流量加上f
- a-=f;//最小可增流量减去f
- if(a==0)
- break;
- }
- }
- return flow;//所有边加上最小残余流量后的值
- }
- int Maxflow(int beg,int end)
- {
- int flow=0;
- while(bfs(beg,end))//存在最短路径
- {
- memcpy(cur,head,sizeof(head));//复制数组
- flow+=dfs(beg,INF,end);
- }
- return flow;//最大流量
- }
- int main()
- {
- int i,j;
- while(scanf("%d%d",&n,&m)!=EOF)
- {
- init();
- getmap();
- printf("%d\n",Maxflow(1,m));
- }
- return 0;
- }
hdoj 1532 Drainage Ditches【最大流模板题】的更多相关文章
- POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]
题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...
- poj-1273 Drainage Ditches(最大流基础题)
题目链接: Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 67475 Accepted ...
- hdu 1532 Drainage Ditches(最大流)
Drainage Dit ...
- poj 1273 Drainage Ditches 最大流入门题
题目链接:http://poj.org/problem?id=1273 Every time it rains on Farmer John's fields, a pond forms over B ...
- POJ 1273 || HDU 1532 Drainage Ditches (最大流模型)
Drainage DitchesHal Burch Time Limit 1000 ms Memory Limit 65536 kb description Every time it rains o ...
- hdu 1532 Drainage Ditches (最大流)
最大流的第一道题,刚开始学这玩意儿,感觉好难啊!哎····· 希望慢慢地能够理解一点吧! #include<stdio.h> #include<string.h> #inclu ...
- HDU 1532 Drainage Ditches(最大流 EK算法)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1532 思路: 网络流最大流的入门题,直接套模板即可~ 注意坑点是:有重边!!读数据的时候要用“+=”替 ...
- hdu-1532 Drainage Ditches---最大流模板题
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1532 题目大意: 给出有向图以及边的最大容量,求从1到n的最大流 思路: 传送门:最大流的增广路算法 ...
- HDU 1532 Drainage Ditches 最大流 (Edmonds_Karp)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1532 感觉题意不清楚,不知道是不是个人英语水平问题.本来还以为需要维护入度和出度来找源点和汇点呢,看 ...
随机推荐
- 视图--bai
/*视图的必要性 create view population_all_view as select xxxx 详细信息 from qgck where rownum<500 -- sql语句不 ...
- Amazon Alexa 语音识别1 : 简介
Alexa是Amazon自家的语音识别技术,需要配合自家的Echo音箱使用.开发者可以在Amazon上建立自己的程序(Skill)来连接到自己的应用或是硬件.例如,用户家里有一套xx牌的智能灯,现在希 ...
- <一> jQuery 简单介绍
jQuery 库位于一个 JavaScript 文件中,其中包含了所有的 jQuery 函数. 可以通过下面的标记把 jQuery 添加到网页中: <head> <script ty ...
- [转载]Asp.net MVC2 与 MVC3 路由调试好帮手RouteDebug 与 RouteDebugger
RouteDebug 与 RouteDebugger是什么? 在Asp.Net MVC程序中,路由(Route)是一个非常核心的概念,可以说是MVC程序的入口,因为每一个Http请求都要经过路由计算, ...
- Android版:验证手机号码的正则表达式
http://blog.csdn.net/dai_zhenliang/article/details/8186249 /** * 验证手机格式 */ public static boolean isM ...
- 李洪强漫谈iOS开发[C语言-017]-printf函数
- 乐1/MACBOOK/ N1 Type-C接口新体验
经过在华强北电子市场排队一个小时,笔者顺利买到了期待已久的乐1,结合之前的NOKIA平板电脑N1,苹果全新MACBOOK,终于集齐了手机.平板.笔记本电脑三种TYPE-C接口设备(能兑换极品装备吗?^ ...
- 使用Oracle的存储过程批量插入数据
原文地址:http://www.cnblogs.com/liaoyu/p/oracle-procedure-batch-insert.html 作者:L君还在说之乎者也 最近在工作中,需要使用生成一些 ...
- Android-相册效果(图片缩放 自由滑动)
先上图: 很多时候 我们会有这么一个需求: 展示一组图片 每个Item的图片 可以自由拉伸 滑动 焦点不冲突 网上有很多实现方法 通过自定义Gallery和ImageView来实现 个人不是很推荐 在 ...
- poj3373Changing Digits(dp)
链接 dfs倒着搜 返回的路径不能满足相同的数最多 借鉴了下别人的代码.. 先dp出来 再倒着标记一下 然后正回来一定可以满足了 dp保存的是最小的不相同数 #include <iostream ...