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最多可以排多少 ...
随机推荐
- spark共享变量
boradcast例子代码: scala版本 spark共享变量之Accumulator 例子代码: scala版本
- knockoutjs foreach array绑定 表格 下拉框绑定
转载http://desert3.iteye.com/blog/1480471 knockoutjs foreach array绑定 表格 下拉框绑定 博客分类: Javascript 动态表格使 ...
- kuangbin_ShortPath N (POJ 1847)
模板题辣很简单的 只有两种val 0 和1 #include <iostream> #include <string> #include <cstdio> #inc ...
- freeswitch 接通后再录音
需求 录音时不要将前置媒体录制进去. 用法 <action application="set" data="media_bug_answer_req=true&qu ...
- 使用 NGUI 实现头顶文字及血条
以下是 NGUI HUD Text 实现的: 基本原理: 1. 在角色头顶绑一个点 Pivot,用于对齐 2. 因为界面总是覆盖在人物头顶信息的上面,所以将 UIRoot 分为2个 Panel:1) ...
- 递归遍历多维数组(树数据结构)的超级简单方式,并且可以递归超过200层,摘自<<PHP精粹:编写高效PHP代码>>
<?php $array = array( "Hello", // Level 1 array( "World" // Level 2 ), array( ...
- weblogic管理2 - 创建并启动一个managed server
创建一个managed server. 1. 进入网页console管理页面,如:http://10.100.25.14:7001/console , 先点击->服务器 (红色标记框) ...
- audition输出参数设置
- unity3d中Find的用法
在unity3d中用Find可以直接查找组件 例子一: 该脚本时绑在main Camera上的,"/Scene/player"这是在Hierarchy目录下直接找寻Scene ...
- 靠边伸缩菜单的做法(类似QQ,碰到就会伸出来)
这段脚本主要实现一个group的伸缩功能,group里面的内容也就是菜单的内容可以自由添加. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ...