【问题描述】

  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.

【输入】

  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.

【输出】

  For each case, output a single integer, the maximum rate at which water may emptied from the pond.

【算法分析】

  裸的最大流,采用最短增广路算法的Dinic算法可以通过所有测试数据。

【程序代码】

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #define maxn 10000
 5 #define inf 1000000
 6 using namespace std;
 7 int n,m,x,y,z;
 8 struct graph{
 9     int start,end,pointsize,tot;
10     int c[maxn<<1],to[maxn<<1],next[maxn<<1],head[maxn],dis[maxn],nowhead[maxn];
11     void clear(){
12         tot=1;
13         memset(c,0,sizeof(c));
14         memset(to,0,sizeof(to));
15         memset(next,0,sizeof(next));
16         memset(head,0,sizeof(head));
17         memset(dis,0,sizeof(dis));
18         memset(nowhead,0,sizeof(nowhead));
19     }
20     void addedge(int a,int b,int l){
21         c[++tot]=l;to[tot]=b;next[tot]=head[a];head[a]=tot;
22         c[++tot]=0;to[tot]=a;next[tot]=head[b];head[b]=tot;
23     }
24     int q[maxn],ql,qr;
25     bool BFS(){
26         for (int i=1;i<=pointsize;++i) nowhead[i]=head[i],dis[i]=0;
27         ql=1; qr=0; q[++qr]=end;
28         while (ql<=qr){
29             for (int k=q[ql++],p=head[k];p;p=next[p])
30                 if(c[p^1]&&!dis[to[p]]&&to[p]!=end) dis[q[++qr]=to[p]]=dis[k]+1;
31         }
32         return dis[start];
33     }
34     int DFS(int k,int maxflow){
35         if (k==end) return maxflow;
36         int flow=0,tflow;
37         for (int&p=nowhead[k];p&&maxflow;p=next[p])
38             if(c[p]&&dis[to[p]]+1==dis[k]&&(tflow=DFS(to[p],min(maxflow,c[p]))))
39                 c[p]-=tflow,c[p^1]+=tflow,maxflow-=tflow,flow+=tflow;
40         return flow;
41     }
42     int dinic(int a,int b){
43         int flow=0;
44         start=a; end=b;
45         while (BFS()) flow+=DFS(a,inf);
46         return flow;
47     }
48     graph(){
49         tot=1;
50     }
51 } G;
52 int main(){
53     while (scanf("%d%d",&n,&m)!=EOF){
54         G.clear();
55         G.pointsize=m;
56         for (int i=1;i<=n;++i){
57             scanf("%d%d%d",&x,&y,&z);
58             G.addedge(x,y,z);
59         }
60         printf("%d\n",G.dinic(1,m));
61     }
62     return 0;
63 }

View Code

用C++中vector和queue实现(参考《算法艺术入门经典训练指南》):

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<vector>
 5 #include<queue>
 6 using namespace std;
 7 const int maxn=10010;
 8 const int INF=1e8;
 9 struct Edge{
10     int from,to,cap,flow;
11 };
12 struct Dinic{
13     int n,m,s,t;
14     vector<Edge> edges;
15     vector<int> G[maxn];
16     void AddEdge(int from,int to,int cap){
17         edges.push_back((Edge){from,to,cap,0});
18         edges.push_back((Edge){to,from,0,0});
19         m=edges.size();
20         G[from].push_back(m-2);
21         G[to].push_back(m-1);
22     }
23     bool vis[maxn];
24     int d[maxn],cur[maxn];
25     bool BFS(){
26         memset(vis,0,sizeof(vis));
27         queue<int> Q;
28         Q.push(s); d[s]=0; vis[s]=1;
29         while (!Q.empty()){
30             int x=Q.front(); Q.pop();
31             for (int i=0;i<G[x].size();i++){
32                 Edge &e=edges[G[x][i]];
33                 if (!vis[e.to]&&e.cap>e.flow){
34                     vis[e.to]=1;
35                     d[e.to]=d[x]+1;
36                     Q.push(e.to);
37                 }
38             }
39         }
40         return vis[t];
41     }
42     int DFS(int x,int a){
43         if (x==t||a==0) return a;
44         int flow=0,f;
45         for (int &i=cur[x];i<G[x].size();i++){
46             Edge &e=edges[G[x][i]];
47             if (d[x]+1==d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>0){
48                 e.flow+=f;
49                 edges[G[x][i]^1].flow-=f;
50                 flow+=f; a-=f;
51                 if (a==0) break;
52             }
53         }
54         return flow;
55     }
56     int Maxflow(int ss,int tt){
57         int flow=0;
58         s=ss; t=tt;
59         while (BFS()){
60             memset(cur,0,sizeof(cur));
61             flow+=DFS(s,INF);
62         }
63         return flow;
64     }
65 }Graph;
66 int main(){
67     int nn,mm,from,to,cap;
68     while (scanf("%d%d",&nn,&mm)!=EOF){
69         Graph.edges.clear();
70         memset(Graph.G,0,sizeof(Graph.G));
71         for (int i=1;i<=nn;++i){
72             scanf("%d%d%d",&from,&to,&cap);
73             Graph.AddEdge(from,to,cap);
74         }
75         printf("%d\n",Graph.Maxflow(1,mm));
76     }
77     return 0;
78 }

View Code

声明:本博文为博主原创博文,未经允许请勿转载。

转载于:https://www.cnblogs.com/Double680/p/5207178.html

[POJ 1273]Drainage Ditches相关推荐

  1. 网络流--最大流--POJ 1273 Drainage Ditches

    链接 Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clov ...

  2. POJ 1273 Drainage Ditches

    网络流. 题意非常easy.给出单向边,容量. 找最大流.注意重边要加起来.g[u][v].c+=c; 第一次写网络流. 也是第一个网络流的题. 看了两天,理解了之后就唰唰唰的写出来了. 大概可能是E ...

  3. POJ - 1273 Drainage Ditches(最大流)

    题目链接:点击查看 题目大意:现在一共有n个节点,需要修建m条水渠将池塘的水引入小溪,点1代表池塘,点n代表小溪,每条水渠都有一定的排水能力,问最大的排水效率是多少 题目分析:网络流的模板题,直接套板 ...

  4. POJ 1273 Drainage Ditches 最大流

    很裸的最大流问题,不过注意会有重边,o(╯□╰)o,被阴了WA了一发 还有就是要用long long #include <cstdio> #include <cstring> ...

  5. POJ 1273 Dinic

    题意 传送门 POJ 1273 Drainage Ditches 题解 最大流模板题,使用 DinicDinicDinic 算法求解. #include <algorithm> #incl ...

  6. poj1273 Drainage Ditches

    蒟蒻的blog POJ 1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Memory Limit: 10000K Total ...

  7. Drainage Ditches POJ1273

    Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 93263 Accepted: 36174 试题链接 文章目录 Descripti ...

  8. USACO Section 4.2 Drainage Ditches(最大流)

    最大流问题.ISAP算法.注意可能会有重边,不过我用的数据结构支持重边.距离d我直接初始化为0,也可以用BFS逆向找一次. -------------------------------------- ...

  9. 网络流 - Drainage Ditches - HDU - 1532

    Problem Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite ...

最新文章

  1. jboss项目导入idea_如何导入任何JBoss BRMS示例项目
  2. mysql怎么实现生日字段前一个小时提醒_MySql学习笔记(二) 索引的设计和使用...
  3. Ansi与Unicode编码
  4. sql如何遍历几百万的表_SQl SERVER 2000 遍历表中数据的方法
  5. python单词的含义-Python常用英文单词有哪些?
  6. chrome扩展程序安装_如何在Windows上删除“由企业策略安装”的Chrome扩展程序
  7. php呼叫平台,php – Twilio呼叫转发
  8. 开题报告的前景_开题报告全分析,写出一份满意的答卷
  9. el表达式设置option标签selected
  10. 达叔的正交化(第三课3.2)
  11. opencv读取文件夹下的所有图片
  12. 基于上下采样的adaboost模型对信用卡欺诈数据进行识别
  13. openstack部署过程5
  14. java调adobe打印_Java报表工具打印方案集锦 | 改变自己
  15. C#编写画直线,简单画线,鼠标交互画线,画一条线
  16. python计算图形面积的方法_Python计算任意多边形面积算法
  17. Android源码分析 - Framework层的Binder(客户端篇)
  18. 关键词热度查询导出工具-自动每天实时关键词更新导出工具
  19. 【MQ我可以讲一个小时】
  20. 数字图像处理(基本知识点二)

热门文章

  1. com/fasterxml/jackson/core/exc/InputCoercionException
  2. 简述关系型数据库和非关系型数据库
  3. Android Studio 快捷键、Debug的使用
  4. 用juniversalchardet解决爬虫乱码问题
  5. java 获取 classpath下的配置文件
  6. 点击按钮抓不到页面的参数
  7. Cocos2dx游戏开发系列笔记3:牛刀小试-忍者飞镖射幽灵的Demo
  8. RealView MDK在链接时提示空间不够的解决方案总结
  9. leaflet自定标签json_Windows Terminal更新后,自定义配置失效?快捷键无法使用?
  10. X86汇编语言从实模式到保护模式20:平坦模型