HDU 1532 Drainage Ditches EK算法 flod算法
题意:
输入m n, m是边数,n是点数。
接下来m行: 起点,终点,容量。
求以 1 为源点, n为汇点的最大流。
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std; const int INF = 0xfffffff;
const int MAXN = + ; //邻接矩阵存放图。
int flow[MAXN][MAXN];
//mark[]标记是否访问过,pre[]记录增广路。
int mark[MAXN], pre[MAXN];
int m, n, f; //f为最大流。 void max_flow()
{
//不断寻找增广路,知道找不到为止,找不到的标志为 mark[n]==0.
while() {
memset(mark, , sizeof(mark));
memset(pre, , sizeof(pre)); queue<int> Q;
mark[] = ;
Q.push();
while( !Q.empty() ) {
int cnt = Q.front();
Q.pop();
//找到增广路,跳出。
if(cnt == n) {
break;
} for(int i = ; i <= n; i++) {
if(!mark[i] && flow[cnt][i] > ) {
mark[i] = ;
Q.push(i);
pre[i] = cnt;
}
}
} //如果没找到可增广的路,直接跳出.
if( !mark[n] ) {
break;
} //计算该增广路最大可增加的流量.
int minx = INF;
for(int i = n; i != ; i = pre[i]) {
minx = min( flow[pre[i]][i], minx );
} for(int i = n; i != ; i = pre[i]) {
flow[pre[i]][i] -= minx; //更新正向流量。
flow[i][pre[i]] += minx; //更新反向流量。
}
f += minx;
}
} int main()
{
while(scanf("%d %d", &m, &n) != EOF) {
memset(flow, , sizeof(flow));
for(int i = ; i < m; i++) {
int u, v, len;
scanf("%d %d %d", &u, &v, &len);
//这个题有重边的情况,所以要flow[u][v] += len;直接等于过不去。
flow[u][v] += len;
}
f = ;
max_flow();
printf("%d\n", f);
}
return ;
}
上面是EK、
下面是flod 还有一个区别就是 上面用邻接矩阵,而下面用邻接表。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
const int N = ;
const int INF = 0x3f3f3f3f; struct Node
{
int to;//终点
int cap; //容量
int rev; //反向边
}; vector<Node> v[N];
bool used[N]; void add_Node(int from,int to,int cap) //重边情况不影响
{
v[from].push_back((Node){to,cap,v[to].size()});
v[to].push_back((Node){from,,v[from].size()-});
} int dfs(int s,int t,int f)
{
if(s==t)
return f;
used[s]=true;
for(int i=;i<v[s].size();i++)
{
Node &tmp = v[s][i]; //注意
if(used[tmp.to]==false && tmp.cap>)
{
int d=dfs(tmp.to,t,min(f,tmp.cap));
if(d>)
{
tmp.cap-=d;
v[tmp.to][tmp.rev].cap+=d;
return d;
}
}
}
return ;
} int max_flow(int s,int t)
{
int flow=;
for(;;){
memset(used,false,sizeof(used));
int f=dfs(s,t,INF);
if(f==)
return flow;
flow+=f;
}
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
memset(v,,sizeof(v));
for(int i=;i<n;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add_Node(x,y,z);
}
printf("%d\n",max_flow(,m));
}
}
另附这题大神BLOG:http://blog.csdn.net/y990041769/article/details/22276101
HDU 1532 Drainage Ditches EK算法 flod算法的更多相关文章
- poj 1273 && hdu 1532 Drainage Ditches (网络最大流)
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 53640 Accepted: 2044 ...
- 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 (网络流)
A - Drainage Ditches Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- hdu 1532 Drainage Ditches(最大流模板题)
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu 1532 Drainage Ditches(最大流)
Drainage Dit ...
- 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) ...
- 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(网络流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532 题目大意是:农夫约翰要把多个小池塘的水通过池塘间连接的水渠排出去,从池塘1到池塘M最多可以排多少 ...
随机推荐
- Discuz论坛搭建过程
1.系统环境 操作系统版本:CentOS Linux 5.7 内核版本:2.6.18-274.el5 arch:x86_64 apache版本:Apache/2.4.6 (Unix) mysql版本 ...
- CString用法总结
概述:CString是MFC中提供的用于处理字符串的类,是一种很有用的数据类型. 它很大程度上简化了MFC中的许多操作,使得MFC在做字符串操作时方便了很多. 不管怎样,使用CString有很多的特殊 ...
- hdu1078 记忆化搜索(DP+DFS)
题意:一张n*n的格子表格,每个格子里有个数,每次能够水平或竖直走k个格子,允许上下左右走,每次走的格子上的数必须比上一个走的格子的数大,问最大的路径和. 我一开始的思路是,或许是普通的最大路径和,只 ...
- Vi个人学习使用心得
找句首/句末 光标所在行:句首: shift+^; 句末: shift +$; 括号匹配:光标在某一括号上,然后shift+% 快速对齐 针对括号的内容:将括号中的内容全部选中之后(按V后, shif ...
- Linux定时任务Crontab执行PHP脚本
http://blog.chinaunix.net/uid-7552018-id-182133.html crontab执行php脚本 http://www.jb51.net/article/2913 ...
- 引用POPUI来实现弹窗效果,且弹窗中的内容可以点击事件
seajs.use(['../js/ui/dialog'],function(){ $('.center-button').bind('click',function(){ var $dlg = $. ...
- HTTP缓存ETAG和Last-Modified
1) 什么是"Last-Modified"? 在浏览器第一次请求某一个URL时,服务器端的返回状态会是200,内容是你请求的资源,同时有一个Last-Modified的属性标记此文 ...
- linux包之dmidecode
http://www.dmtf.org/standards/smbios Dmidecode 这款软件允许你在 Linux 系统下获取有关硬件方面的信息.Dmidecode 遵循 SMBIOS/DMI ...
- Openjudge计算概论-计算矩阵边缘元素之和
/*======================================================================== 计算矩阵边缘元素之和 总时间限制: 1000ms ...
- thymeleaf之fragment
MUEAS项目,web前端采用thymeleaf作为展示层.这个view解析器,个人觉得非常不错.简单而且性能也比较好!个人觉得比JSP和freemarker之类,简单易用! 今天简单记录一下frag ...