网络流dinic模板,邻接矩阵+链式前向星
//这个是邻接矩阵的
#include<iostream>
#include<queue>
#include<string.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
const int maxn=;
const int inf=0x3f3f3f;
int N;
int depth[maxn];
int a[maxn][maxn];
bool bfs(int s,int e)//广搜求深度
{
queue<int>q;
memset(depth,-,sizeof(depth));//初始-1
depth[s]=;
q.push(s);
while(!q.empty())
{
int now=q.front();
q.pop();
for(int i=;i<=N;i++)
{
if(depth[i]==-&&a[now][i])//两点有路,并且未搜过
{
depth[i]=depth[now]+;
q.push(i);
}
}
}
return depth[e]>;
}
int dfs(int now,int e,int nowflow)
{
if(now==N) return nowflow;//如果搜到最后 int findflow=;
for(int i=;i<=N;i++)
{
if(a[now][i]&&depth[i]==depth[now]+)//有路,并且为下一节点
{
findflow=dfs(i,e,min(nowflow,a[now][i]));//继续dfs
if(findflow)
{
a[now][i]-=findflow;
a[i][now]+=findflow;
return findflow;
}
}
}
if(!findflow) depth[now]=-;//炸点优化
return false;
}
int dinic(int s,int e)
{
int maxflow=;
while(bfs(s,e))
maxflow+=dfs(s,e,<<); return maxflow;
}
链式前向星
没听说过的同学 戳这里
#include<bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f;
const int maxn = 1e5+;
struct node{
int v,w,next;
node(int v1=,int w1=,int next1=):v(v1),w(w1),next(next1){}
};
node e[maxn<<];
int head[maxn];
int tot;
int N,E;//顶点数和边数
int dep[maxn];//求深度
int cur[maxn]//当前弧优化
void init()
{
tot=;
memset(head,-,sizeof(head));
}
void add(int u,int v,int w)
{
e[tot].v=v;
e[tot].w=w;
e[tot].next=head[u];
head[u]=tot++;
//反向边
e[tot].v=u;
e[tot].w=;
e[tot].next=head[v];
head[v]=tot++;
}
//bfs分层图
bool bfs(int ss,int ee)
{
memset(dep,-,sizeof(dep));
queue<int>q;
dep[ss]=;
q.push(ss);
while(!q.empty())
{
int now=q.front();
q.pop();
for(int i=head[now];i!=-;i=e[i].next)
{
if(dep[e[i].v]==- && e[i].w>)
{
dep[e[i].v]=dep[now]+;
q.push(e[i].v);
}
}
}
return dep[ee]!=-;
}
//dfs搜索增广路径,now搜索顶点 ee终点 nowflow当前最大流
int dfs(int now,int ee,int nowflow)
{
// 搜索到终点或者 最大流为0
if(now==ee||nowflow==) return nowflow;
//useflow 可用流量 达到nowflow时不再增加
//maxflow 递归深搜时的最大流
int useflow=,maxflow;
//&i=cur[now] 为cur[now]起了个别名,为当前弧优化,每次更新cur[now];
for(int &i=cur[now]; i != - ; i = e[i].next)
{
if(e[i].w > && dep[e[i].v] == dep[now] + )
{
maxflow = dfs(e[i].v, ee, min(nowflow-useflow, e[i].w));
if(maxflow>)
{
e[i].w-=maxflow;
e[i^].w+=maxflow;
useflow+=maxflow;
if(uswflow == nowflow) return nowflow;
}
}
}
if(!useflow) dep[now]=-;
return useflow;
}
int dinic(int ss,int ee)
{
int ans=;
while(bfs(ss,ee))
{
for(int i=;i<=N;i++)
cur[i]=head[i];
ans+=dfs(ss,ee,inf);
}
return ans;
}
网络流dinic模板,邻接矩阵+链式前向星的更多相关文章
- 【模板】链式前向星+spfa
洛谷传送门--分糖果 博客--链式前向星 团队中一道题,数据很大,只能用链式前向星存储,spfa求单源最短路. 可做模板. #include <cstdio> #include <q ...
- 模板 Dijkstra+链式前向星+堆优化(非原创)
我们首先来看一下什么是前向星. 前向星是一种特殊的边集数组,我们把边集数组中的每一条边按照起点从小到大排序,如果起点相同就按照终点从小到大排序, 并记录下以某个点为起点的所有边在数组中的起始位置和 ...
- poj-1459-最大流dinic+链式前向星-isap+bfs+stack
title: poj-1459-最大流dinic+链式前向星-isap+bfs+stack date: 2018-11-22 20:57:54 tags: acm 刷题 categories: ACM ...
- 单元最短路径算法模板汇总(Dijkstra, BF,SPFA),附链式前向星模板
一:dijkstra算法时间复杂度,用优先级队列优化的话,O((M+N)logN)求单源最短路径,要求所有边的权值非负.若图中出现权值为负的边,Dijkstra算法就会失效,求出的最短路径就可能是错的 ...
- 图的存储结构:邻接矩阵(邻接表)&链式前向星
[概念]疏松图&稠密图: 疏松图指,点连接的边不多的图,反之(点连接的边多)则为稠密图. Tips:邻接矩阵与邻接表相比,疏松图多用邻接表,稠密图多用邻接矩阵. 邻接矩阵: 开一个二维数组gr ...
- zzuli 2131 Can Win dinic+链式前向星(难点:抽象出网络模型+建边)
2131: Can Win Time Limit: 1 Sec Memory Limit: 128 MB Submit: 431 Solved: 50 SubmitStatusWeb Board ...
- 三种邻接表存图模板:vector邻接表、数组邻接表、链式前向星
vector邻接表: ; struct Edge{ int u,v,w; Edge(int _u=0,int _v=0,int _w=0){u=_u,v=_v,w=_w;} }; vector< ...
- hdu2647 逆拓扑,链式前向星。
pid=2647">原文地址 题目分析 题意 老板发工资,可是要保证发的工资数满足每一个人的期望,比方A期望工资大于B,仅仅需比B多1元钱就可以.老板发的最低工资为888元.输出老板最 ...
- HDU1532 Drainage Ditches SAP+链式前向星
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
随机推荐
- FreeRTOS+ WolfSSL + Lwip Demo
FreeRTOS+ WolfSSL Demo下载 LWIP 源码下载 2018年2月5日09:39:08 WolfSSL is about 10 times smaller than yaSSL, ...
- web性能优化之GZIP压缩
从服务端优化来说,通过对服务端做压缩配置可以大大减小文本文件的体积,从而使加载文本的速度成倍的加快.目前比较通用的压缩方法是启用gzip压缩.它会把浏览器请求的页面,以及页面中引用的静态资源以压缩包的 ...
- Page Object 设计模式-PO
1.传统测试用例实现的弊端: 易读性差 复用性差 可维护性差 扩展性差 2.PO 设计模式图: 3.Page Object 的核心要素: 抽象封装一个 BasePage 基类,基类应该拥有一个只想 w ...
- Oracle 数据库数据结构(包括存储过程,函数,表,触发器等)版本控制器
原理: 写系统触发器,在修改数据库结构的时候,把DDL写入表中 create sequence A_Ver_Control_seq minvalue nomaxvalue start incremen ...
- centos7字体中英文转化
[root@localhost oracle]#vi /etc/locate.conf,把里面的内容改为: 转化为英文: LANG="en_US.UTF-8"LANGUAGE=&q ...
- centos7 安装mysql5.7以及一些细节问题
突然发现我的新服务器上没有mysql,所以想安装一个,上次在我的window电脑上安装MySQL8.0我真的要气死了,和5.7修改密码的方式不一样,弄了很久,所以我决定还是不用安装8.0了,5.7就可 ...
- php中的Register Globals
参考: http://php.net/manual/zh/security.globals.php
- python range,xrange区别
range: 直接生成一个列表对象 xrange: 生成一个xrange对象 xrange使用: 操作一个非常大的数据时,而且没存比较吃紧的时,可以使用xrange来节省内存 xrange一般在循环里 ...
- Spring 注解学习
@GetMapping(value = "/hello/{id}")//需要获取Url=localhost:8080/hello/id中的id值 public String say ...
- week8课上实践
课上练习. 第一题: 参考 http://www.cnblogs.com/rocedu/p/6766748.html#SECCLA 在Linux下完成"求命令行传入整数参数的和" ...