//这个是邻接矩阵的
#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模板,邻接矩阵+链式前向星的更多相关文章

  1. 【模板】链式前向星+spfa

    洛谷传送门--分糖果 博客--链式前向星 团队中一道题,数据很大,只能用链式前向星存储,spfa求单源最短路. 可做模板. #include <cstdio> #include <q ...

  2. 模板 Dijkstra+链式前向星+堆优化(非原创)

    我们首先来看一下什么是前向星.   前向星是一种特殊的边集数组,我们把边集数组中的每一条边按照起点从小到大排序,如果起点相同就按照终点从小到大排序, 并记录下以某个点为起点的所有边在数组中的起始位置和 ...

  3. poj-1459-最大流dinic+链式前向星-isap+bfs+stack

    title: poj-1459-最大流dinic+链式前向星-isap+bfs+stack date: 2018-11-22 20:57:54 tags: acm 刷题 categories: ACM ...

  4. 单元最短路径算法模板汇总(Dijkstra, BF,SPFA),附链式前向星模板

    一:dijkstra算法时间复杂度,用优先级队列优化的话,O((M+N)logN)求单源最短路径,要求所有边的权值非负.若图中出现权值为负的边,Dijkstra算法就会失效,求出的最短路径就可能是错的 ...

  5. 图的存储结构:邻接矩阵(邻接表)&链式前向星

    [概念]疏松图&稠密图: 疏松图指,点连接的边不多的图,反之(点连接的边多)则为稠密图. Tips:邻接矩阵与邻接表相比,疏松图多用邻接表,稠密图多用邻接矩阵. 邻接矩阵: 开一个二维数组gr ...

  6. zzuli 2131 Can Win dinic+链式前向星(难点:抽象出网络模型+建边)

    2131: Can Win Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 431  Solved: 50 SubmitStatusWeb Board ...

  7. 三种邻接表存图模板: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< ...

  8. hdu2647 逆拓扑,链式前向星。

    pid=2647">原文地址 题目分析 题意 老板发工资,可是要保证发的工资数满足每一个人的期望,比方A期望工资大于B,仅仅需比B多1元钱就可以.老板发的最低工资为888元.输出老板最 ...

  9. HDU1532 Drainage Ditches SAP+链式前向星

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

随机推荐

  1. idea 和 maven学习

    创建maven项目:http://www.cnblogs.com/wql025/p/5215570.html

  2. ansible实用例子

    寻找/etc/ 名为"hosts" 递归查找 ansible webserver -m find -a ' path=/etc/ file_type=any recurse=yes ...

  3. 在Red Hat Enterprise Linux 7.3上安装SQL Server 2017

    必要条件: 1.在此快速安装过程中,您需要安装SQL Server 2017或SQL Server 2019上Red Hat Enterprise Linux (RHEL) 7.3 +.然后使用sql ...

  4. XML第一次简单入门(Lab分析)

    In this tutorial you will create a well-formed and verified XML file. Consider the XML document belo ...

  5. 购物车功能:使用jQuery实现购物车全选反选,单选,商品增删,小计等功能

    效果图: html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  6. 前端基础-jQuery的事件的用法

    阅读目录 常用事件 事件绑定 移除事件 页面载入 一.常用事件 1.鼠标事件之click事件 用交互操作中,最简单直接的操作就是点击操作.jQuery提供了两个方法一个是click方法用于监听用户单击 ...

  7. 笔记:javascript操作iframe内的DOM元素,及调用iframe内的方法

    iframe相当于一个嵌入在网页内的浏览器,它与当前网页是隔离的. 但有时我们也需要在当前网页操作iframe内的元素或操作iframe内的javascript方法. 在网页内操作DOM元素,是使用d ...

  8. MyEclipse报错:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException Communications link failure

    数据库服务没有开或者是驱动那块的问题

  9. mongo数据集合属性中存在点号(.)

    基本知识点: 1.似乎mongo3.6之前不允许插入带点(.)或美元符号($)的键,但是当我使用mongoimport工具导入包含点的JSON文件时,它工作正常. 2.在使用spring-data-m ...

  10. 批处理之 for /f 中的delims和tokens

    0x00 前言 今天在对windows进行提权之前的系统信息收集的时候,需要使用到一条批处理语句把特定部分的内容从一个txt的文本当中提取出来:该条语句是如下: for /f "tokens ...