HDU 1532 Drainage Ditches(网络流模板题)
题目大意:就是由于下大雨的时候约翰的农场就会被雨水给淹没,无奈下约翰不得不修建水沟,而且是网络水沟,并且聪明的约翰还控制了水的流速,
本题就是让你求出最大流速,无疑要运用到求最大流了。题中m为水沟数,n为水沟的顶点,接下来Si,Ei,Ci分别是水沟的起点,终点以及其容量。求源点1到终点m的最大流速。
EK模板
#include<iostream>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
#define MaxInt 0x3f3f3f3f
using namespace std;
int m,n,f[210][210],cap[210][210],a[210],p[210];
queue<int> que;
int Edmond_Karp(int s,int t)
{
int flow=0;
while(1){//BFS找增广路
memset(a,0,sizeof(a));
a[s]=MaxInt;
que.push(s);
while(!que.empty()){
int u=que.front();
que.pop();
for(int v=1;v<=m;v++){//m为节点个数
if(!a[v]&&cap[u][v]>f[u][v]){
p[v]=u;//记录v的前驱
que.push(v);
a[v]=min(a[u],cap[u][v]-f[u][v]);//a[v]为s-v路径上的最小流量
}
}
}
if(a[t]==0) break;//找不到,则当前已是最大流量
for(int v=t;v!=s;v=p[v])//从汇点往回走
{
f[p[v]][v]+=a[t];//更新正向流量
f[v][p[v]]-=a[t];//更新反向流量
}
flow+=a[t];//更新从s流出的总流量
}
return flow;
}
int main()
{
int u,v,i,c,ans;
while(scanf("%d%d",&n,&m)!=EOF){
memset(cap,0,sizeof(cap));
memset(f,0,sizeof(f));
for(i=1;i<=n;i++){
scanf("%d%d%d",&u,&v,&c);
cap[u][v]+=c;//注意重边
}
ans=Edmond_Karp(1,m);
printf("%d\n",ans);
}
return 0;
}
Dinic模板
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
#define inf 0x7fffffff
#define min(a,b) a<b?a:b
int n,m,u,v,w;
int level[];
struct Dinic
{
int cap;
int flow;
}edge[][];
//构造层次网络
bool Dinic_bfs(int s,int t)
{
queue<int> que;
memset(level,,sizeof(level));//初始化所有顶点的层次为 0
que.push(s);
level[s]=;
while(!que.empty()){
int u=que.front();
que.pop();
for(int v=;v<=m;v++){//即顶点v未被访问过,只考虑残量网络中顶点u,v是否存在边
if(!level[v]&&edge[u][v].flow<edge[u][v].cap){
level[v]=level[u]+;
que.push(v);
}
}
}
return level[t];
}
//u为当前节点,a为当前最小残量进行多路增广
int Dinic_dfs(int u,int a)
{
int flow=;
if(u==m||a==)
return a;
for(int v=;v<=m;v++){
if(level[u]+==level[v]){
if(edge[u][v].cap>edge[u][v].flow){
int r=Dinic_dfs(v,min(a,edge[u][v].cap-edge[u][v].flow));
edge[u][v].flow+=r;
edge[v][u].flow-=r;
a-=r,flow+=r;
}
}
}
return flow;
}
//求出最大流
int Dinic()
{
int flow=;
while(Dinic_bfs(,m))
flow+=Dinic_dfs(,inf);
return flow;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF){
memset(edge,,sizeof(edge));
for(int i=;i<=n;i++){
scanf("%d%d%d",&u,&v,&w);
edge[u][v].cap+=w;//防止重边
}
printf("%d\n",Dinic());
}
return ;
}
HDU 1532 Drainage Ditches(网络流模板题)的更多相关文章
- POJ 1273:Drainage Ditches 网络流模板题
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63339 Accepted: 2443 ...
- USACO 4.2 Drainage Ditches(网络流模板题)
Drainage DitchesHal Burch Every time it rains on Farmer John's fields, a pond forms over Bessie's fa ...
- hdu 1532 Drainage Ditches(网络流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532 题目大意是:农夫约翰要把多个小池塘的水通过池塘间连接的水渠排出去,从池塘1到池塘M最多可以排多少 ...
- hdu 1532 Drainage Ditches(最大流模板题)
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1532 Drainage Ditches (网络流)
A - Drainage Ditches Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1532 Drainage Ditches (最大网络流)
Drainage Ditches Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) To ...
- HDU 1532 Drainage Ditches(最大流 EK算法)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1532 思路: 网络流最大流的入门题,直接套模板即可~ 注意坑点是:有重边!!读数据的时候要用“+=”替 ...
- 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 Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 53640 Accepted: 2044 ...
随机推荐
- EasyDarwin开发出相似于美拍、秒拍的短视频拍摄SDK:EasyVideoRecorder
EasyVideoRecorder Github:https://github.com/EasyDarwin/EasyVideoRecorder EasyVideoRecorder作为一款短视频拍摄的 ...
- asp.net 动态添加多个用户控件
动态添加多个相同用户控件,并使每个用户控件获取不同的内容. 用户控件代码: 代码WebControls using System; using System.Collections.Generic; ...
- AuthorizeAttribute示例
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- [4] 算法之路 - 插入排序之Shell间隔与Sedgewick间隔
题目 插入排序法由未排序的后半部前端取出一个值.插入已排序前半部的适当位置.概念简单但速度不快. 排序要加快的基本原则之中的一个: 是让后一次的排序进行时,尽量利用前一次排序后的结果,以加快排序的速度 ...
- Struts2_day01--访问action的方法
访问action的方法(重点) 1 有三种方式实现 第一种 使用action标签的method属性,在这个属性里面写执行的action的方法名称 第二种 使用通配符方式实现 第三种 动态访问实现(不用 ...
- iOS7Status bar适配
一 Status bar重叠问题: - Zherui if ([[[UIDevicecurrentDevice] systemVersion] floatValue] >= 7.0) { sel ...
- [SCOI2010]传送带[三分]
//point(AB)->point(CD) 距离满足下凸性,用三分套三分实现 #include<cmath> #include<cstdio> #include< ...
- 7624:山区建小学(划分dp)
7624:山区建小学 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 政府在某山区修建了一条道路,恰好穿越总共m个村庄的每个村庄一次,没有回路或交叉,任意两个村庄 ...
- Animate a custom Dialog,自定义Dialog动画
Inside res/style.xml <style name="AppTheme" parent="android:Theme.Light" /> ...
- LAMP集群项目五 部署NFS存储服务并设置WEB服务挂载
yum install nfs-utils portmap -y 在centos6.5中portmap已经改为rpcbind 先启动rpcbind /etc/init.d/rpcbind start ...