网络流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) ...
随机推荐
- Http_load的安装和使用
Http_load的安装和使用 http_load基于linux平台的一种性能测工具.以并行复用的方式运行,用以测试web服务器的吞吐量与负载,测试web页面的性能. 安装: 进入工作目录:#cd / ...
- HDU 3746 Cyclic Nacklace(求补齐循环节最小长度 KMP中next数组的使用 好题!!!)
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- iOS 使约束带动画效果(Animate NSLayoutconstraints)
http://stackoverflow.com/questions/12926566/are-nslayoutconstraints-animatable http://stackoverflow. ...
- 【js】Redux基本原理和使用
Redux不是说任何的应用都要用到它,如果遇到了react解决不了得问题,可以考虑使用它. 例如: 用户的使用方式复杂不同身份的用户有不同的使用方式(比如普通用户和管理员)多个用户之间可以协作与服务器 ...
- 购物车业务逻辑(vuex)
list(列表页): 1:发送ajax请求,获取相应的数据 2:给每一个上平添加一个点击事件 3:每一个商品都要有一个ID 4:当点击商品时,将商品id值传递给详情页 details(详情页): 1: ...
- React学习(一)
一. 允许HTML和JavaScript代码混写,使用JSX语法:遇到HTML标签就用HTML规则解析,遇到{}的代码块就用js解析 var names = ['Alice', 'Emily', 'K ...
- centos7 远程连接mongodb时,27017端口连接不上的解决办法
一.问题描述:centos 7 上安装mongogdb,然后通过另外一台电脑用pymongo连接mongodb时,报错:连接拒绝 解决过程: 1.修改mongo.conf文件 命令:sudo vi ...
- IP组播 MulticastChannel接口 DatagramChannel实现
监听者 import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; impo ...
- uva 210 - Concurrency Simulator (并行程序模拟)
from CSDN: https://blog.csdn.net/su_cicada/article/details/87898579 例题6-1 并行程序模拟( Concurrency Simula ...
- 003.2---asyncio模块(上)
asyncio(上) asyncio 的几个概念 event_loop(事件循环):程序开启一个无线的循环,程序员会把一些函数(协程)注册到事件循环上,当满足事件发生的时候,调用相应的协程函数. co ...