网络流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) ...
随机推荐
- SDOI2018一轮NOI培训 题目整理
\(qwq\)首先,这些题对于我而言--类似于\(emmm\)洪水猛兽 \(\mathcal{Day \ \ 1}\) T1 \(\mathcal{\color{red}{Description}}\ ...
- ASP.NET Core MVC中的IActionFilter.OnActionExecuted方法执行时,Controller中Action返回的对象是否已经输出到Http Response中
我们在ASP.NET Core MVC项目中有如下HomeController: using Microsoft.AspNetCore.Mvc; namespace AspNetCoreActionF ...
- VB.NET & Visual Basic
当看到VB.NET者这本书籍的时候,翻开文件夹唯一的感受就是:这不和VB一样吗?究竟有什么差别呢? 1)版本号: 又一次回想VB,能够发现事实上他是Microsoft退出的基于Windows操作系统环 ...
- centos6.4 安装code::blocks
今天下午闲着没事尝试在自己的PC中的CentOS上装一个Code::Blocks,因为是Linux菜鸟折腾了一下午才基本算搞定但依然有疑惑: 在网上各种谷哥度娘最后才发现还是官方的文档最靠谱:看这里. ...
- selenium java maven 自动化测试(二) 页面元素获取与操作
在第一节中,我们已经成功打开了页面,但是自动化测试必然包含了表单的填写与按钮的点击. 所以在第二章中我以博客园为例,完成按钮点击,表单填写 还是以代码为准,先上代码: package com.ryan ...
- c#采用emit将DataTable转List
前面已经说了List转DataTable,也整理了代码. 现在转回来说说DataTable转List. 先举一个例子 public class Person { public int Age{get; ...
- 建立复数类Complex,并且进行赋值,求和,取模等操作
#include "pch.h" #include <iostream> #include<cmath> using namespace std; clas ...
- activemq的高级特性:消息存储持久化
activemq的高级特性之消息存储持久化 有基于文件的,数据库的,内存的.默认的是基于文件的,在安装目录/data/kahadb.在conf/activemq.xml文件中. <persist ...
- [译文]程序员能力矩阵 Programmer Competency Matrix
注意:每个层次的知识都是渐增的,位于层次n,也蕴涵了你需了解所有低于层次n的知识. 计算机科学 Computer Science 2n (Level 0) n2 (Level 1) n (Leve ...
- JavaScript中的this详解(彻底弄懂js中的this用法)!
要想学好js,那么其中那些特别令人混淆迷惑的知识点,就一定要弄清楚.this关键字就是其中让初学者比较迷惑的知识点之一,不过灵活运用this可以提升代码的性能和复用性,那么今天我就和大家一起来了解th ...