Drainage Ditches---hdu1532(最大流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532
题意:
每次下雨的时候,农场主John的农场里就会形成一个池塘,这样就会淹没其中一小块土地,在这块土地上种植了Bessie最喜欢的苜蓿。这意味着苜蓿要被水淹没一段时间,而后要花很长时间才能重新长出来。因此,John修建了一套排水系统,这样种植了苜蓿的土地就不会被淹没。雨水被排到了附近的一条小河中。作为一个一流的工程师,John还在每条排水沟的起点安装了调节阀门,这样可以控制流入排水沟的水流的速度。
John不仅知道每条排水沟每分钟能排多少加仑的水,而且还知道整个排水系统的布局,池塘里的水通过这个排水系统排到排水沟,并最终排到小何中,构成一个复杂的排水网络。
给定排水系统,计算池塘能通过这个排水系统排水到小河中的最大水流速度。每条排水沟的流水方向是单方向的,但在排水系统中,流水可能构成循环。
求最大流模板;
有重边所以要累加
Dinic算法:
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std; #define N 220
#define INF 0xfffffff int n, m, maps[N][N], d[N]; bool bfs(int s, int e)
{
memset(d, , sizeof(d));
queue<int>Q;
int p;
Q.push(s);
d[s] = ;
while(!Q.empty())
{
p=Q.front();Q.pop();
if(p==e)
return true;
for(int i=; i<=n; i++)
{
if(maps[p][i] && !d[i])
{
d[i] = d[p] + ;
Q.push(i);
}
}
}
return false;
}
int dfs(int u, int e, int Maxflow)
{
int uflow=;
if(u==e)
return Maxflow;
for(int i=; i<=n; i++)
{
if(maps[u][i] && d[i]==d[u]+)
{
int flow = min(maps[u][i], Maxflow-uflow);
flow = dfs(i, e, flow); maps[u][i]-=flow;
maps[i][u]+=flow; uflow += flow;
if(uflow==Maxflow)break;
}
}
if(uflow==)
d[u]=;///减少循坏次数;
return uflow;
}
int Dinic(int s, int e)
{
int ans = ; while(bfs(s, e))
{
ans += dfs(s, e, INF);
}
return ans;
}
int main()
{
int a, b, c;
while(scanf("%d%d", &m, &n)!=EOF)
{
memset(maps, , sizeof(maps));
for(int i=; i<=m; i++)
{
scanf("%d%d%d", &a, &b, &c);
maps[a][b]+=c;
}
printf("%d\n", Dinic(,n));
}
return ;
}
EK算法:
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
#define INF 0xfffffff
#define N 220
int maps[N][N], pre[N], ans;
bool bfs(int s, int e)
{
memset(pre, , sizeof(pre));
queue<int>Q;
Q.push(s);
while(Q.size())
{
int i = Q.front();
Q.pop();
if(i == e)
return true;
for(int j=; j<=e; j++)
{
if(pre[j]== && maps[i][j] > )
{
pre[j] = i;
Q.push(j);
}
}
}
return false;
}
void EK(int s, int e)
{
while(bfs(s, e))
{
int Min = INF;
for(int i=e; i!=s; i=pre[i])
Min=min(maps[pre[i]][i], Min);
for(int i=e; i!=s; i=pre[i])
{
maps[pre[i]][i]-=Min;
maps[i][pre[i]]+=Min;
}
ans+=Min;
}
}
int main()
{
int n, m, x, y,c;
while(scanf("%d%d", &m, &n)!=EOF)
{
memset(maps, , sizeof(maps));
for(int i=; i<=m; i++)
{
scanf("%d%d%d", &x, &y, &c);
maps[x][y] += c;
}
ans = ;
EK(, n);
printf("%d\n", ans);
}
return ;
}
Drainage Ditches---hdu1532(最大流)的更多相关文章
- POJ1273&&Hdu1532 Drainage Ditches(最大流dinic) 2017-02-11 16:28 54人阅读 评论(0) 收藏
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- poj 1273 (nyoj 323) Drainage Ditches : 最大流
点击打开链接 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 49648 Accepte ...
- poj 1273 Drainage Ditches(最大流)
http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Subm ...
- poj 1273 Drainage Ditches【最大流入门】
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63924 Accepted: 2467 ...
- 2018.07.06 POJ1273 Drainage Ditches(最大流)
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Description Every time it rains on Farmer J ...
- POJ 1273 Drainage Ditches (网络最大流)
http://poj.org/problem? id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- poj 1273 && hdu 1532 Drainage Ditches (网络最大流)
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 53640 Accepted: 2044 ...
- poj1273 Drainage Ditches Dinic最大流
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 76000 Accepted: 2953 ...
- poj 1273 Drainage Ditches(最大流,E-K算法)
一.Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clove ...
- HDU 1532||POJ1273:Drainage Ditches(最大流)
pid=1532">Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/327 ...
随机推荐
- 转载:MySQL数据库INSERT、UPDATE、DELETE以及REPLACE语句的用法详解
转自:http://www.jb51.net/article/39199.htm 本篇文章是对MySQL数据库INSERT.UPDATE.DELETE以及REPLACE语句的用法进行了详细的分析介绍, ...
- 二分求幂,快速求解a的b次幂
一个引子 如何求得a的b次幂呢,那还不简单,一个for循环就可以实现! void main(void) { int a, b; ; cin >> a >> b; ; i < ...
- 基于bootstrap的Dialog
function yms_Dialog(container_id, modal_path, handle_function) { /// <summary> /// ...
- 【Java面试题】52 java中会存在内存泄漏吗,请简单描述。
所谓内存泄露就是指一个不再被程序使用的对象或变量一直被占据在内存中.Java中有垃圾回收机制,它可以保证一对象不再被引用的时候,即对象编程了孤儿的时候,对象将自动被垃圾回收器从内存中清除掉.由于Jav ...
- ThinkPHP重写规则优化URL及Rewrite规则详细说明
示例如下: http://www.topstack.cn/Article/detail/id/198.html 优化为 http://www.topstack.cn/article-198.html ...
- 小技巧处理div内容溢出
前几天遇到一个问题,代码是这样一个层次: <div class="province"> <ul> <li>1</li& ...
- 帝国留言板管理员回复发送EMAIL通知客户
说明:修改1:e/admin/tool/ReGook.php /*回复表单*/ 43行处添加代码 ------------------------------------------------- ...
- VMware 12安装虚拟机Mac OS X 10.10(VMware12安装/共享文件夹)
推荐电脑配置 1:Inter I5及以上 (A卡请自行百度大神解决方案) 必须开启CPU虚拟化:开机进入BIOS--->Intel Virtualization Technology---> ...
- React如何进行事件传参
今天在学习React的es6语法的时候,发现了个有趣的现象,就是this的指向问题.es6的this不同于es5,它在创立函数伊始便已经存在了,而不是像es5一样,睡调用的函数,this指向谁.但是这 ...
- block基本使用和底层
block基础使用语法 一.block与函数的对比 定义函数指针 int (*myFn)(); 定义Blocks int (^MyBlocks)(int,int); 调用函数指针 (*myFn)( ...