poj 1273 ---&&--- hdu 1532 最大流模板
最近在换代码布局,因为发现代码布局也可以引起一个人的兴趣
这个方法是算法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 最大流模板的更多相关文章
- 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 ...
- POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]
题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...
- HDU 1532 最大流模板题
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1532 最近在学网络流,学的还不好,先不写理解了,先放模板... 我觉得写得不错的博客:http://blo ...
- POJ 1273 Drainage Ditches | 最大流模板
#include<cstdio> #include<algorithm> #include<cstring> #include<queue> #defi ...
- HDU 1532 最大流入门
1.HDU 1532 最大流入门,n个n条边,求第1点到第m点的最大流.只用EK做了一下. #include<bits/stdc++.h> using namespace std; #pr ...
- POJ 1273 Drainage Ditches(最大流Dinic 模板)
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n, ...
- poj 1273 Drainage Ditches 最大流入门题
题目链接:http://poj.org/problem?id=1273 Every time it rains on Farmer John's fields, a pond forms over B ...
- hdu 1532(最大流)
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
随机推荐
- OpenCV学习笔记廿一:opencv_contrib模块
一,简介: 该库为新加入代码库的算法.
- Scrapy爬虫笔记
Scrapy是一个优秀的Python爬虫框架,可以很方便的爬取web站点的信息供我们分析和挖掘,在这记录下最近使用的一些心得. 1.安装 通过pip或者easy_install安装: 1 sudo p ...
- go反射----2值
声明:文章内容取自雨痕老师<Go语言学习笔记> 和Type获取类型信息不同,Value专注于对象实例数据读写. 在前面章节曾提到过,接口变量会复制对象,且是unaddressable的,所 ...
- MariaDB二进制包简单安装部署
一.简介: MySQL最早是由Michael Widenius在所研发,而在后来Michael先生以10亿美元的价格把MySQL卖给了SUN以后不久SUN就被Oracle公司给收购了,在Oracle收 ...
- MongoDB save()方法和insert()方法的区别
MongoDB save()方法和insert()方法的区别 首先看官方文档怎么说的 Updates an existing document or inserts a new document, d ...
- iOS-获取当前网页的 url 和 title 和 html
本文转载至 http://www.th7.cn/Program/IOS/201310/156916.shtml @property (strong,nonatomic)UIWebView *web ...
- codevs2894、2837、1669、2503、3231
6.25动态规划之背包回顾 2894 Txx考试 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description Txx是一个 ...
- GridView实现编辑删除
前台界面: <asp:GridView ID=" ForeColor="#333333" AutoGenerateColumns=" OnRowCance ...
- VS中没有为此解决方案配置选中要生成的项目
菜单->生成->配置管理器->给要生成的项目打钩
- C++常备知识总结
1.extern表示是外部函数或外部变量,比如: 1.extern void add(int x,inty);表示该函数主体不在当前模块中,在另一个模块中(文件)2.extern int total; ...

