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 }
- Step By Step 一步一步写网站[1] —— 填加数据(二)
- [WCF-Discovery]让服务自动发送上/下线通知[原理篇]
- [WCF-Discovery]让服务自动发送上/下线通知[实例篇]
- [WCF权限控制]利用WCF自定义授权模式提供当前Principal[实例篇]
- [WCF权限控制]利用WCF自定义授权模式提供当前Principal[原理篇]
- [自定义服务器控件] 第一步:文本框。
- 检查两个数据库里的表名、字段是否一致的一种方法
- 如何在Python中从零开始实现随机森林
- 基类、接口的应用——表单控件:一次添加、修改一条记录,一次修改多条记录。(上)
- 使命必达: 深入剖析WCF的可靠会话[协议篇](下)
- 分页控件和几个相关控件的源代码
- Python编程中的反模式
- Python机器学习库:Scikit-Learn简介
- 很简单的企业管理器---我写程序的方式,几个自定义控件。
- 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 数组属性和方法
- Solr集群(即SolrCloud)搭建与使用
- 在CentOS8上安装Surelog
- sqlplus执行错误的问题探究
- 不借助Maven,使用Eclipse创建Hello World级别的Spring项目
- Spring Bean的生命周期
- Spring Bean 后置处理器PostProcessor
- 潘石屹用Python解决100个问题 | 排列组合
- 潘石屹用Python解决100个问题 | 企业根据利润提成发奖金
- 【STM32F407开发板用户手册】第27章
- Spring Bean的继承
- 移动端点击事件延迟的诞生消亡史
- 谈谈电商系统中的商品模块设计
- [数据结构与算法] 排序算法之冒泡排序与快速排序(快排)
- [数据结构与算法] 邂逅栈
- Hive建表异常