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 思路: 网络流最大流的入门题,直接套模板即可~ 注意坑点是:有重边!!读数据的时候要用“+=”替 ...
随机推荐
- 手把手教你把Vim改装成一个IDE编程环境(图文)
http://blog.csdn.net/wooin/article/details/1858917
- ps aux 查看进程信息
[root@localhost Desktop]# ps auxUSER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDroot 1 0.0 0.3 ...
- Python基础学习笔记(七)常用元组内置函数
参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-tuples.html 3. http://www.liaoxue ...
- SQL 语句转换格式函数Cast、Convert
CAST和CONVERT都经常被使用.特别提取出来作为一篇文章,方便查找. CAST.CONVERT都可以执行数据类型转换.在大部分情况下,两者执行同样的功能,不同的是CONVERT还提供一些特别的日 ...
- cocos2d-x 3.X(一)环境搭建问题
在按照官网上的教程(http://cn.cocos2d-x.org/tutorial/show?id=1478)步骤一步一步安装完成同时也添加了各项环境变量,运行cocos命令也没有任何问题,但就是在 ...
- SQL VIEW 使用语法
之前一直都不知道VIEW有什么作用,写程序的时候也很少遇到过,复习SQL语句的时候碰到了,就记录下来吧. 什么是视图? 在 SQL 中,视图是基于 SQL 语句的结果集的可视化的表. 视图包含行和列, ...
- iOS - NSURLSession 网络请求
前言 NS_CLASS_AVAILABLE(NSURLSESSION_AVAILABLE, 7_0) @interface NSURLSession : NSObject @available(iOS ...
- iOS - UITabBarController
前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UITabBarController : UIViewController <UITabBarDelegate ...
- iOS - UIImageView
前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIImageView : UIView @available(iOS 2.0, *) public class U ...
- [转载] 跟着实例学习zookeeper 的用法
原文: http://ifeve.com/zookeeper-curato-framework/ zookeeper 的原生客户端库过于底层, 用户为了使用 zookeeper需要编写大量的代码, 为 ...