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
 #include<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=;
int map[maxn][maxn];
int dist[maxn];
int n,m;
int bfs(int st,int en){
int t;
queue<int>q;
memset(dist,-,sizeof(int)*(m+));
q.push(st);
dist[st]=;
while(!q.empty()){
t=q.front();
q.pop();
for(int i=;i<=m;i++){
if(map[t][i]>&&dist[i]<){
dist[i]=dist[t]+;
q.push(i);
}
}
}
if(dist[en]>) return ;
return ;
}
int dfs(int st,int en,int flow){
int tem=;
if(st==en||flow==)return flow;
for(int i=;i<=m;i++)
{
if(dist[i]==dist[st]+&&map[st][i]>&&(tem=dfs(i,en,min(map[st][i],flow))))
{
map[st][i]-=tem;
map[i][st]+=tem;
return tem;
}
}
return ;
}
void Dinic(int st,int en)
{
int ans=;
while(bfs(st,en))
ans+=dfs(st,en,inf);
printf("%d\n",ans);
}
int main()
{
int i,a,b,c;
while(scanf("%d%d",&n,&m)!=EOF){
memset(map,,sizeof(map));
for(i=;i<=n;i++){
scanf("%d%d%d",&a,&b,&c);
map[a][b]+=c;
}
Dinic(,m);
}
return ;
}

优化优化,用一下邻接表做...

代码:内存立马减少到了 276k

代码:

 #include<stdio.h>
#include<string.h>
#include<queue>
#define ma 502
#define inf 0x3f3f3f3f
using namespace std;
int head[ma];
struct node
{
int to;
int w;
int next;
};
node mat[ma];
int dist[ma];
int pos,n,m;
int min(int a,int b){
return a>b?b:a;
}
void add(int a,int b,int flow){
mat[pos].to=b;
mat[pos].w=flow;
mat[pos].next=head[a];
head[a]=pos++;
} bool bfs(int st,int to){
memset(dist,-,sizeof(int)*(n+));
queue<int> q;
q.push(st);
dist[st]=;
int t;
while(!q.empty()){
t=q.front();
q.pop();
for(int i=head[t];~i;i=mat[i].next){
if(dist[mat[i].to]<&&mat[i].w>){
dist[mat[i].to]=dist[t]+;
if(mat[i].to==to) return ;
q.push(mat[i].to);
}
}
}
return ;
} int dfs(int st,int to,int flow){ int tem=;
if(st==to||flow==) return flow;
for(int i=head[st];~i;i=mat[i].next){
if(mat[i].w>&&dist[mat[i].to]==dist[st]+&&(tem=dfs(mat[i].to,to,min(flow,mat[i].w))))
{
mat[i].w-=tem;
mat[i^].w+=tem;
return tem;
}
}
return ;
}
int Dinic(int st,int to){
int ans=;
while(bfs(st,to))
ans+=dfs(st,to,inf);
return ans;
} int main()
{
int a,b,c;
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(head,-,sizeof(int)*(n+));
pos=;
while(m--){
scanf("%d%d%d",&a,&b,&c);
add(a,b,c); //单向边
add(b,a,);
}
printf("%d\n",Dinic(,n));
}
return ;
}
 

hdu-----(1532)Drainage Ditches(最大流问题)的更多相关文章

  1. hdu 1532 Drainage Ditches(最大流模板题)

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. HDU 1532 Drainage Ditches 分类: Brush Mode 2014-07-31 10:38 82人阅读 评论(0) 收藏

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  3. POJ 1273 || HDU 1532 Drainage Ditches (最大流模型)

    Drainage DitchesHal Burch Time Limit 1000 ms Memory Limit 65536 kb description Every time it rains o ...

  4. HDU 1532 Drainage Ditches (最大网络流)

    Drainage Ditches Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) To ...

  5. poj 1273 && hdu 1532 Drainage Ditches (网络最大流)

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 53640   Accepted: 2044 ...

  6. HDU 1532 Drainage Ditches (网络流)

    A - Drainage Ditches Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  7. hdu 1532 Drainage Ditches(最大流)

                                                                                            Drainage Dit ...

  8. HDU 1532 Drainage Ditches 最大流 (Edmonds_Karp)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1532 感觉题意不清楚,不知道是不是个人英语水平问题.本来还以为需要维护入度和出度来找源点和汇点呢,看 ...

  9. hdu 1532 Drainage Ditches (最大流)

    最大流的第一道题,刚开始学这玩意儿,感觉好难啊!哎····· 希望慢慢地能够理解一点吧! #include<stdio.h> #include<string.h> #inclu ...

  10. HDU 1532 Drainage Ditches(最大流 EK算法)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1532 思路: 网络流最大流的入门题,直接套模板即可~ 注意坑点是:有重边!!读数据的时候要用“+=”替 ...

随机推荐

  1. Gradle的配置实例

    错过了Maven,但是遇到了Gradle. 网上关于Gradle的讲解和培训已经很多了. 我就直接贴几个我测试过的配置文件吧: ① 依赖maven资源库 repositories { mavenCen ...

  2. poj 1279 -- Art Gallery (半平面交)

    鏈接:http://poj.org/problem?id=1279 Art Gallery Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...

  3. python_way day11 自定义线程池

    python_way day11 线程池 为什么需要线程池 线程多一些固然好,但是过多的线程反倒影响系统的负荷,所以我们就需要创建合适多的线程,哪我们把线程放到哪里?这时就放到线程池中. 线程池中存放 ...

  4. ubuntu1604安装体验

    昨天安装了ubuntu 16 安装成了双系统,这样的速度才能接受. 各种软件支持的都还算不错.除了启动时候原始的气息,启动之后挺稳定的. 最让人开心的是新的unity的设置更加丰富了,可以自动隐藏,更 ...

  5. Javascript Object Layout

  6. Scrum Meeting---Two(2015-10-26)

    这次会议主要有两个方面 一.讨论项目 经过我们团队的激烈讨论,我们团队决定专注于做二手交易这一块.即将之前决定要做的学习经验交流以及校园交由这两块删除. 二.后两天的任务规划 以下便是我们的任务规划: ...

  7. Nginx RTMP 专题

    说明: 记录器 - 记录器名称 path - 记录文件路径(recorded file path) (/tmp/rec/mystream-1389499351.flv)filename - 省略目录的 ...

  8. sql for xml 嵌套

    找很久.原来差一个ELEMENTS 关键字. 想到哪里插入子节点.就直接写一条语句,加一个ELEMENTS. 为什么baidu这么就都找不到.到处都是转来转去的东西.郁闷. select h.*,(s ...

  9. 【Todo】【转载】Spark学习 & 机器学习(实战部分)-监督学习、分类与回归

    理论原理部分可以看这一篇:http://www.cnblogs.com/charlesblc/p/6109551.html 这里是实战部分.参考了 http://www.cnblogs.com/shi ...

  10. html页面的绝对路径和相对路径

    在用springmvc架构开发网站的过程中,离不开开发前台html页面,html经常需要使用本地相关的资源,如:图片,js,css等,一般情况下,我们可以通过使用相对路径的方式来对这些资源进行指向和访 ...