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 思路: 网络流最大流的入门题,直接套模板即可~ 注意坑点是:有重边!!读数据的时候要用“+=”替 ...
随机推荐
- Oracle同义词学习
oracle的同义词总结 从字面上理解就是别名的意思,和视图的功能类似.就是一种映射关系. 同义词拥有如下好处: 节省大量的数据库空间,对不同用户的操作同一张表没有多少差别; 扩展的数 ...
- Deep Learning Workbench Installation Notes
1. ROS Indigo (30 min) Just flow ROSWiki: http://wiki.ros.org/indigo/Installation/Ubuntu NOW simply ...
- Codeforces Round #281 (Div. 2) D. Vasya and Chess 水
D. Vasya and Chess time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- sql for xml 嵌套
找很久.原来差一个ELEMENTS 关键字. 想到哪里插入子节点.就直接写一条语句,加一个ELEMENTS. 为什么baidu这么就都找不到.到处都是转来转去的东西.郁闷. select h.*,(s ...
- XP_版本
1. Windows XP sp3 cd 和Windows XP sp3 cd vl的区别?VL的意思是大客户版,就是使用VL的KEY安装的系统是不需要激活的,不带VL的是安装完后需要激活的零售版 2 ...
- LCM兼容
1.project-1998-trunk-bootable-bootloader-lk-project: 复制zaw1998aa_platform.mk为zaw2000aa_platform.mk ...
- JavaSE复习_11 IO流复习
△FileReader是使用默认码表读取文件, 如果需要使用指定码表读取, 那么可以使用InputStreamReader(字节流,编码表) FileWriter是使用默认码表写出文件, 如果需 ...
- 【Todo】单例模式各种实现方式及并发安全
Java 40道面试题不错:http://www.tuicool.com/articles/VRVFZb 其中有一道题目: 单例模式的线程安全性 老生常谈的问题了,首先要说的是单例模式的线程安全意味着 ...
- Java后端开发
Java后端开发 名称 内容 基本框架 Spring.Mybatis Linux服务器 数据库优化 消息服务 rabbitMQ.activeMq rocketMq 缓存服务 memcached ...
- 本地存储 localStorage/sessionStorage/cookie
cookie是个基础的东西.是服务器发送到客户端,存储在客户端的一小段数据.可以存储一些配置信息,客户标识信息等.用户下次访问这个网站时,会把上次网站发来的cookie一同发送回去.cookie保存在 ...