网络流dinic ek模板 poj1273
这里只是用来存放模板,几乎没有讲解,要看讲解网上应该很多吧……
ek
bfs不停寻找增广路到找不到为止,找到终点时用pre回溯,O(VE^2)
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = ;
int cap[][], pre[], n, m, a[];
int bfs(){
queue<int> q;
q.push();
memset(a,,sizeof(a));
a[] = INF;
while(!q.empty()){
int front = q.front();
q.pop();
for(int i = ;i<=m;i++){
if(!a[i] && cap[front][i]){
a[i] = min(a[front], cap[front][i]);
pre[i] = front;
q.push(i);
}
}
}
return a[m];
}
int ek(){
int ans = ;
while(bfs()){
ans += a[m];
for(int i = m;i!=;i = pre[i]){
cap[pre[i]][i] -= a[m];
cap[i][pre[i]] += a[m];
}
}
return ans;
}
int main(){
while(~scanf("%d%d",&n,&m)){
memset(cap,,sizeof(cap));
for(int i = ;i<=n;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
cap[u][v] += w;
}
printf("%d\n",ek());
}
return ;
}
dinic
反复构造层次网络加找增广路,优势在于当某点的流入量较大时,可以一次完成多条增广路的累加,O(V^2E)
记得初始化lv,cnt=1
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn = ;
const int INF = 0x3f3f3f3f;
struct EDGE{
int to, next, cap, flow;
EDGE(){}
EDGE(int a, int b, int c, int d){
to = a, next = b, cap = c, flow = d;;
}
}e[];
int head[], cnt, lv[],m ,n;
void add(int from, int to, int cap){
e[++cnt] = EDGE(to, head[from], cap, );
head[from] = cnt;
e[++cnt] = EDGE(from, head[to], , );
head[to] = cnt;
}
int bfs(){
queue<int> q;
q.push();
memset(lv,,sizeof(lv));
lv[] = ;
while(!q.empty()){
int front = q.front();
q.pop();
for(int i = head[front];i;i = e[i].next){
int to = e[i].to;
if(!lv[to] && e[i].cap-e[i].flow){
lv[to] = lv[front]+;
q.push(to);
}
}
}
return lv[m];
}
int dfs(int now, int a){
int flow = ,f;
if(now == m) return a;
for(int i = head[now];i;i = e[i].next){
int to = e[i].to;
if(lv[to] == lv[now]+ && (f = dfs(to,min(a,e[i].cap-e[i].flow)))){
e[i].flow += f;
e[i^].flow -= f;
flow += f;
a -= f;
if(!a) break;
}
}
return flow;
}
int dinic(){
int ans = ;
while(bfs()){
ans += dfs(,INF);
}
return ans;
}
int main(){
while(~scanf("%d%d",&n,&m)){
cnt = ;
memset(head,,sizeof(head));
for(int i = ;i<=n;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
printf("%d\n",dinic());
}
return ;
}
网络流dinic ek模板 poj1273的更多相关文章
- 网络流Dinic算法模板 POJ1273
这就是以后我的板子啦~~~ #include <queue> #include <cstdio> #include <cstring> #include <a ...
- POJ 1273 Drainage Ditches(网络流dinic算法模板)
POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...
- Drainage Ditches(POJ1273+网络流+Dinic+EK)
题目链接:poj.org/problem?id=1273 题目: 题意:求最大流. 思路:测板子题,分别用Dinic和EK实现(我的板子跑得时间均为0ms). Dinic代码实现如下: #includ ...
- POJ 3281 [网络流dinic算法模板]
题意: 农场主有f种食物,d种饮料,n头牛. 接下来的n行每行第一个数代表第i头牛喜欢吃的食物数量,和第i头牛喜欢喝的饮料数目. 接下来分别是喜欢的食物和饮料的编号. 求解:农场主最多能保证几头牛同时 ...
- HDU1532_Drainage Ditches(网络流/EK模板/Dinic模板(邻接矩阵/前向星))
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 网络流小记(EK&dinic&当前弧优化&费用流)
欢 迎 来 到 网 络 瘤 的 世 界 什么是网络流? 现在我们有一座水库,周围有n个村庄,每个村庄都需要水,所以会修水管(每个水管都有一定的容量,流过的水量不能超过容量).最终水一定会流向唯一一个废 ...
- POJ 1273 Drainage Ditches (网络流Dinic模板)
Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...
- HDU1532最大流 Edmonds-Karp,Dinic算法 模板
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- POJ 1815 - Friendship - [拆点最大流求最小点割集][暴力枚举求升序割点] - [Dinic算法模板 - 邻接矩阵型]
妖怪题目,做到现在:2017/8/19 - 1:41…… 不过想想还是值得的,至少邻接矩阵型的Dinic算法模板get√ 题目链接:http://poj.org/problem?id=1815 Tim ...
随机推荐
- vmware安装密钥
VMware虚拟机已升级至14版本,之前的12版本的秘钥已经无法使用,在此分享一下VMware Workstation 14永久激活密钥: CG54H-D8D0H-H8DHY-C6X7X-N2KG6 ...
- <configSections> 位置引起的错误
今天在配置一个项目的时候,花了挺长时间配置完成,然后一启动项目,懵了,启动报错:错误显示 Configuration Error Description: An error occurred duri ...
- iOS-AVPlayer
MPMoviePlayerController足够强大,几乎不用写几行代码就能完成一个播放器,但是正是由于它的高度封装使得要自定义这个播放 器变得很复杂,甚至是不可能完成.例如有些时候需要自定义播放器 ...
- 希望对自学的javascript新手有帮助
- python变量 - python基础入门(6)
何为python变量,即数据类型.python变量一共六种类型:整数/浮点数/字符串/BOOL/列表/元组/字典,今天先讲解前四种,后三种留到后面的文章在讲解. 首先讲解print() 函数,prin ...
- Java线程池的使用方式,核心运行原理、以及注意事项
为什么需要线程池 java中为了提高并发度,可以使用多线程共同执行,但是如果有大量线程短时间之内被创建和销毁,会占用大量的系统时间,影响系统效率. 为了解决上面的问题,java中引入了线程池,可以使创 ...
- sql复合索引使用和注意事项
1.定义: 单一索引: 单一索引是指索引列为一列的情况,即新建索引的语句只实施在一列上; 复合索引: 复合索引也叫组合索引: 用户可以在多个列上建立索引,这种索引叫做复合索引(组合索引). 复合索引在 ...
- HTML札记
HTML 指的是 超文本标记语言 (Hyper Text Markup Language) 文档后缀名: 当您保存 HTML 文件时,既可以使用 .htm 也可以使用 .html 扩展名.两者没有区别 ...
- c++ string类型成员变量在调用构造函数后未能正确赋值
struct RelItem{ string segName; Elf32_Rel* rel; string relName; RelItem(string seg, int addr, string ...
- destoon 增删改查
switch($action) { case 'add': //添加页面 if($submit) { // 不允许重名,直接添加时 $old = $db->get_one("SELEC ...