hdu-----(1532)Drainage Ditches(最大流问题)
Drainage Ditches
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9580 Accepted Submission(s): 4541
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.
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
题意: 有关输水网络,给出每一段水管的最大输水流量,问整个系统出水的最大流量是多少......
看到这道题,完全惊呆了,一道原生态的网络流问题,而且问题还这么的直白。╮(╯▽╰)╭,但是呵呵,杭电有展现了他机智的一面,说的数据和给的数据不一致,说好的200,然后要到了500,然后果断的献上了好几个wa。
看了一下discuss里的僵尸版的提示,又去修改一下代码....O(∩_∩)O~呵~呵,感觉不会再爱了,居然还有重边,我真的是....,优秀改了一下矩阵的构图,又大方的献了几个wa....
最后终于AC...当然对于这样的一个水体(带坑),也还是要高兴高兴....
首先献上一个EK算法的代码:(不知道什么时候学的,貌似是算法竞赛里刘奴佳介绍的)..时间复杂度有点小高...但是数据很弱居然oms过了,我又.......感受到了这个世界的恶意啊╮(╯▽╰)╭
0ms
1 #include<cstdio>
2 #include<cstring>
3 #include<queue>
4 #include<iostream>
5 #include<algorithm>
6 using namespace std;
7 const int inf=0x3f3f3f3f;
8 const int maxn=501;
9 int map[maxn][maxn];
10 int dist[maxn];
11 int n,m;
12 int bfs(int st,int en){
13 int t;
14 queue<int>q;
15 memset(dist,-1,sizeof(int)*(m+1));
16 q.push(st);
17 dist[st]=0;
18 while(!q.empty()){
19 t=q.front();
20 q.pop();
21 for(int i=1;i<=m;i++){
22 if(map[t][i]>0&&dist[i]<0){
23 dist[i]=dist[t]+1;
24 q.push(i);
25 }
26 }
27 }
28 if(dist[en]>0) return 1;
29 return 0;
30 }
31 int dfs(int st,int en,int flow){
32 int tem=0;
33 if(st==en||flow==0)return flow;
34 for(int i=1;i<=m;i++)
35 {
36 if(dist[i]==dist[st]+1&&map[st][i]>0&&(tem=dfs(i,en,min(map[st][i],flow))))
37 {
38 map[st][i]-=tem;
39 map[i][st]+=tem;
40 return tem;
41 }
42 }
43 return 0;
44 }
45 void Dinic(int st,int en)
46 {
47 int ans=0;
48 while(bfs(st,en))
49 ans+=dfs(st,en,inf);
50 printf("%dn",ans);
51 }
52 int main()
53 {
54 int i,a,b,c;
55 while(scanf("%d%d",&n,&m)!=EOF){
56 memset(map,0,sizeof(map));
57 for(i=1;i<=n;i++){
58 scanf("%d%d%d",&a,&b,&c);
59 map[a][b]+=c;
60 }
61 Dinic(1,m);
62 }
63 return 0;
64 }
优化优化,用一下邻接表做...
代码:内存立马减少到了 276k
代码:
1 #include<stdio.h>
2 #include<string.h>
3 #include<queue>
4 #define ma 502
5 #define inf 0x3f3f3f3f
6 using namespace std;
7 int head[ma];
8 struct node
9 {
10 int to;
11 int w;
12 int next;
13 };
14 node mat[ma];
15 int dist[ma];
16 int pos,n,m;
17 int min(int a,int b){
18 return a>b?b:a;
19 }
20 void add(int a,int b,int flow){
21 mat[pos].to=b;
22 mat[pos].w=flow;
23 mat[pos].next=head[a];
24 head[a]=pos++;
25 }
26
27 bool bfs(int st,int to){
28 memset(dist,-1,sizeof(int)*(n+1));
29 queue<int> q;
30 q.push(st);
31 dist[st]=0;
32 int t;
33 while(!q.empty()){
34 t=q.front();
35 q.pop();
36 for(int i=head[t];~i;i=mat[i].next){
37 if(dist[mat[i].to]<0&&mat[i].w>0){
38 dist[mat[i].to]=dist[t]+1;
39 if(mat[i].to==to) return 1;
40 q.push(mat[i].to);
41 }
42 }
43 }
44 return 0;
45 }
46
47 int dfs(int st,int to,int flow){
48
49 int tem=0;
50 if(st==to||flow==0) return flow;
51 for(int i=head[st];~i;i=mat[i].next){
52 if(mat[i].w>0&&dist[mat[i].to]==dist[st]+1&&(tem=dfs(mat[i].to,to,min(flow,mat[i].w))))
53 {
54 mat[i].w-=tem;
55 mat[i^1].w+=tem;
56 return tem;
57 }
58 }
59 return 0;
60 }
61 int Dinic(int st,int to){
62 int ans=0;
63 while(bfs(st,to))
64 ans+=dfs(st,to,inf);
65 return ans;
66 }
67
68 int main()
69 {
70 int a,b,c;
71 while(scanf("%d%d",&m,&n)!=EOF)
72 {
73 memset(head,-1,sizeof(int)*(n+1));
74 pos=0;
75 while(m--){
76 scanf("%d%d%d",&a,&b,&c);
77 add(a,b,c); //单向边
78 add(b,a,0);
79 }
80 printf("%dn",Dinic(1,n));
81 }
82 return 0;
83 }
- PostQueuedCompletionStatus
- tomcat源码解读四 tomcat中的processer
- tomcat源码解读三(2) tomcat中JMX的源码分析
- 程序的入口
- tomcat源码解读三(1) tomcat的jmx管理
- 利用xinetd实现简单web服务器(镜像站)
- tomcat源码解读二 tomcat的生命周期
- IOCP反射服务器
- 给PHP开发者讲讲PHP源码-第二部分
- 给PHP开发者讲讲PHP源码-第一部分
- tomcat源码解读一 Digester的解析方式
- Markdown 语法说明(简体中文版)
- C++中_onexit()用法简述
- tomcat请求处理分析(六)servlet的处理过程
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 一文搞定冒泡排序算法
- 剑指Offer LeetCode 面试题25. 合并两个排序的链表
- LeetCode 20200601 打卡 1431. 拥有最多糖果的孩子
- 剑指Offer LeetCode 面试题24. 反转链表
- 剑指Offer LeetCode 面试题22. 链表中倒数第k个节点
- 剑指Offer LeetCode 面试题18. 删除链表的节点
- 剑指Offer LeetCode 面试题06. 从尾到头打印链表
- 最详细的docker中安装并配置redis
- 剑指Offer LeetCode 面试题59 - I. 滑动窗口的最大
- 剑指Offer LeetCode 面试题58 - II. 左旋转字符串
- 剑指Offer LeetCode 面试题58 - I. 翻转单词顺序
- 剑指Offer LeetCode 面试题56 - II. 数组中数字出现的次数 II
- 站在软件工程的角度重新思考面向对象(含高清图谱)
- 剑指Offer LeetCode 面试题57. 和为s的两个数字
- 白嫖Layui树型可折叠,可自定义,可搜索表格的实例