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
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 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.
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
#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(最大流问题)的更多相关文章
- hdu 1532 Drainage Ditches(最大流模板题)
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 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) ...
- 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 (最大网络流)
Drainage Ditches Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) To ...
- poj 1273 && hdu 1532 Drainage Ditches (网络最大流)
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 53640 Accepted: 2044 ...
- HDU 1532 Drainage Ditches (网络流)
A - Drainage Ditches Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- hdu 1532 Drainage Ditches(最大流)
Drainage Dit ...
- HDU 1532 Drainage Ditches 最大流 (Edmonds_Karp)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1532 感觉题意不清楚,不知道是不是个人英语水平问题.本来还以为需要维护入度和出度来找源点和汇点呢,看 ...
- 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 思路: 网络流最大流的入门题,直接套模板即可~ 注意坑点是:有重边!!读数据的时候要用“+=”替 ...
随机推荐
- Gradle的配置实例
错过了Maven,但是遇到了Gradle. 网上关于Gradle的讲解和培训已经很多了. 我就直接贴几个我测试过的配置文件吧: ① 依赖maven资源库 repositories { mavenCen ...
- poj 1279 -- Art Gallery (半平面交)
鏈接:http://poj.org/problem?id=1279 Art Gallery Time Limit: 1000MS Memory Limit: 10000K Total Submis ...
- python_way day11 自定义线程池
python_way day11 线程池 为什么需要线程池 线程多一些固然好,但是过多的线程反倒影响系统的负荷,所以我们就需要创建合适多的线程,哪我们把线程放到哪里?这时就放到线程池中. 线程池中存放 ...
- ubuntu1604安装体验
昨天安装了ubuntu 16 安装成了双系统,这样的速度才能接受. 各种软件支持的都还算不错.除了启动时候原始的气息,启动之后挺稳定的. 最让人开心的是新的unity的设置更加丰富了,可以自动隐藏,更 ...
- Javascript Object Layout
- Scrum Meeting---Two(2015-10-26)
这次会议主要有两个方面 一.讨论项目 经过我们团队的激烈讨论,我们团队决定专注于做二手交易这一块.即将之前决定要做的学习经验交流以及校园交由这两块删除. 二.后两天的任务规划 以下便是我们的任务规划: ...
- Nginx RTMP 专题
说明: 记录器 - 记录器名称 path - 记录文件路径(recorded file path) (/tmp/rec/mystream-1389499351.flv)filename - 省略目录的 ...
- sql for xml 嵌套
找很久.原来差一个ELEMENTS 关键字. 想到哪里插入子节点.就直接写一条语句,加一个ELEMENTS. 为什么baidu这么就都找不到.到处都是转来转去的东西.郁闷. select h.*,(s ...
- 【Todo】【转载】Spark学习 & 机器学习(实战部分)-监督学习、分类与回归
理论原理部分可以看这一篇:http://www.cnblogs.com/charlesblc/p/6109551.html 这里是实战部分.参考了 http://www.cnblogs.com/shi ...
- html页面的绝对路径和相对路径
在用springmvc架构开发网站的过程中,离不开开发前台html页面,html经常需要使用本地相关的资源,如:图片,js,css等,一般情况下,我们可以通过使用相对路径的方式来对这些资源进行指向和访 ...