最近在换代码布局,因为发现代码布局也可以引起一个人的兴趣
这个方法是算法Edmonds-Karp 最短增广路算法,不知道的话可以百度一下,基于Ford-Fulkerson算法的基础上延伸的
其实我不是很透彻的领悟这个算法的精髓,只知道怎样实现,现在的任务就是多刷几道题,见识见识题型,就可以更透彻领悟为什么这么做,之后再拐回来研究算法,这样就可以学习和实践相结合!

详解 : 就是每次广搜后都让走过的边减去这条通路的最小的通路,逆向通路加上这条通路的最小通路,也就是最大容纳量,形成新的通路
之后就记录最小通路 maxflow=0 ;maxflow+=minflow;
  1  2  3   4  连接矩阵初始化
1 0 40 0 20 
2 0  0 30  20                                                            
3 0 0 0 10
4 0 0 0 0
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define Min(a, b) a<b?a:b;
#define INF 0x3f3f3f3f

const
int N=;
int
river, land;
int
G[N][N], pre[N];//previous 先前的

int
EK(int s, int e);//start, end
bool BFS(int s, int e); int main()
{

while
(scanf("%d%d", &river, &land)!=EOF)
{

memset(G,, sizeof(G)); int a, b, flow; while(river--)
{

scanf("%d%d%d", &a, &b, &flow);
G[a][b]+=flow;//调试的时候发现少了一个‘+’,因为可能有重边
} int ans = EK(, land);
printf("%d\n", ans);
}

return
;
}
int EK(int s, int e)
{

int
maxflow=;//就是要求的最大流
while(BFS(s, e)==true)//注意这里是while 因为要进行BFS直到整个图都被搜完了,无法再搜了;
{
int
minflow=INF;//这是定义了最小流,根据木桶原理,其实就是这个通道最大容纳量

for
(int i=e; i!=s; i=pre[i])//这里for循环里面的东西, i从e开始每次都变为它的前一个节点,
minflow=Min(minflow, G[pre[i]][i]); for(int i=e; i!=s; i=pre[i])//修改路径
{
G[pre[i]][i]-=minflow;
G[i][pre[i]]+=minflow;
}
maxflow+=minflow;
}

return
maxflow;
}
bool BFS(int s, int e)
{

memset(pre,, sizeof(pre)); queue<int>Q;
Q.push(s); while(!Q.empty())
{

int
i=Q.front(); Q.pop();//队首出队列

if
(i==e)
return
true; for(int j=; j<=e; j++)//调试的时候发现少些一个‘=’ j<=e;
{
if
(G[i][j]&&pre[j]==)//当i和j点之间有通路时,且没有被访问过
{
pre[j]=i;
Q.push(j);
}
}
}
return false;
}
 

poj 1273 ---&&--- hdu 1532 最大流模板的更多相关文章

  1. poj 1273 && hdu 1532 Drainage Ditches (网络最大流)

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 53640   Accepted: 2044 ...

  2. POJ 1273 || HDU 1532 Drainage Ditches (最大流模型)

    Drainage DitchesHal Burch Time Limit 1000 ms Memory Limit 65536 kb description Every time it rains o ...

  3. POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]

    题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...

  4. HDU 1532 最大流模板题

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1532 最近在学网络流,学的还不好,先不写理解了,先放模板... 我觉得写得不错的博客:http://blo ...

  5. POJ 1273 Drainage Ditches | 最大流模板

    #include<cstdio> #include<algorithm> #include<cstring> #include<queue> #defi ...

  6. HDU 1532 最大流入门

    1.HDU 1532 最大流入门,n个n条边,求第1点到第m点的最大流.只用EK做了一下. #include<bits/stdc++.h> using namespace std; #pr ...

  7. POJ 1273 Drainage Ditches(最大流Dinic 模板)

    #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n, ...

  8. poj 1273 Drainage Ditches 最大流入门题

    题目链接:http://poj.org/problem?id=1273 Every time it rains on Farmer John's fields, a pond forms over B ...

  9. hdu 1532(最大流)

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

随机推荐

  1. UICollectionView sectionHeader and sectionFooter悬浮

    UICollectionViewFlowLayout *yLayout = [[UICollectionViewFlowLayout alloc] init]; yLayout.sectionHead ...

  2. OpenCV学习笔记十四:opencv_objdetect模块

    一,简介: 该库用于目标检测.

  3. orm查询基本操作

    orm  ----关系对象模型 orm 的查询两种操作 1.基于对象的查询 oneTo one 一对多 和多对多 对象.关联字段 对象.表名(_set)) 2.基于QUERYset的查询 双下划线即为 ...

  4. lumen 获得当前uri 如/xxx/{id}

    因为想实现通过url判断是否有权限,所有需要拿到当前的route方法的name,如下 $api->get('role/grant/{id}', 'RoleController@getGrant' ...

  5. libnids介

    转自:http://blog.chinaunix.net/uid-22832715-id-2111578.html Libnids开发包介绍     Libnids是一个用于网络入侵检测开发的专业编程 ...

  6. django的ORM中的2个易混点

    1.django数据模型中null=True和blank=True的区别 null 是针对数据库而言,如果 null=True, 表示数据库的该字段可以为空,即在Null字段显示为YES. blank ...

  7. Redis 持久化机制

    1.背景 之前在使用redis 时候,没有过多的考虑持久化! 但是这样即使你用了redis 也是徒劳,表面上你是用上了redis 进行缓存数据,感觉已经给自己的架构添加了一个道QPS 防护墙! 哈哈, ...

  8. PHP heredoc 用法2

    参考网上的一个heredoc php模板实现的简单代码:index.php文件: <?php function template($template,$EXT ='htm') { $path = ...

  9. Socket_leaks open socket #5024 left in connection

    open socket left in connection http://mailman.nginx.org/pipermail/nginx/2012-September/035627.html D ...

  10. jQuery获取input复选框的值

    var ipResolveValue =[]; //定义一个空数组$("input[name='ipResolve']:checked").each(function(){   / ...