标准的大白书式模板,除了变量名并不一样……在主函数中只需要用到 init 函数、add 函数以及 mf 函数

 #include<stdio.h>                //差不多要加这么些头文件
#include<string.h>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
const int maxm=+; //点的总数
const int INF=0x3f3f3f3f; struct edge{ //弧的结构体,变量:弧的出发点、结束点、容量、流量
int from,to,c,f;
edge(int a,int b,int m,int n):from(a),to(b),c(m),f(n){}
}; struct dinic{
int m,s,t; //边数、源点标号、汇点标号
vector<edge>e; //边
vector<int>g[maxm]; //g[i][j]表示第i个点出发的第j条边在e中的编号
bool vis[maxm];
int d[maxm],cur[maxm]; //d为源点到点的距离,cur为当前遍历到的边
void init(int n){ //初始化,n为点数量(标号0~n-1)
for(int i=;i<n+;i++)g[i].clear();
e.clear();
}
void add(int a,int b,int v){ //加入弧和反向弧
e.push_back(edge(a,b,v,)); //正向弧容量v,反向弧容量0
e.push_back(edge(b,a,,));
m=e.size();
g[a].push_back(m-);
g[b].push_back(m-);
}
bool bfs(){
memset(vis,,sizeof(vis));
queue<int>q;
q.push(s);
d[s]=;
vis[s]=;
while(!q.empty()){
int u=q.front();q.pop();
for(int i=;i<g[u].size();i++){
edge tmp=e[g[u][i]];
if(!vis[tmp.to]&&tmp.c>tmp.f){
vis[tmp.to]=;
d[tmp.to]=d[u]+;
q.push(tmp.to);
}
}
}
return vis[t];
}
int dfs(int x,int a){
if(x==t||a==)return a;
int flow=,f;
for(int& i=cur[x];i<g[x].size();i++){
edge &tmp=e[g[x][i]];
if(d[x]+==d[tmp.to]&&(f=dfs(tmp.to,min(a,tmp.c-tmp.f)))>){
tmp.f+=f;
e[g[x][i]^].f-=f;
flow+=f;
a-=f;
if(a==)break;
}
}
if(!flow)d[x]=-;
return flow;
}
int mf(int s,int t){ //在主函数中使用的函数,求s到t的最大流
this->s=s;
this->t=t;
int flow=;
while(bfs()){
memset(cur,,sizeof(cur));
flow+=dfs(s,INF);
}
return flow;
}
};

网络流--最大流dinic模板的更多相关文章

  1. 网络流-最大流 Dinic模板

    #include <bits/stdc++.h> using namespace std; #define MP make_pair #define PB push_back #defin ...

  2. 网络流--最大流--Dinic模板矩阵版(当前弧优化+非当前弧优化)

    //非当前弧优化版 #include <iostream> #include <cstdio> #include <math.h> #include <cst ...

  3. [讲解]网络流最大流dinic算法

    网络流最大流算法dinic ps:本文章不适合萌新,我写这个主要是为了复习一些细节,概念介绍比较模糊,建议多刷题去理解 例题:codevs草地排水,方格取数 [抒情一下] 虽然老师说这个多半不考,但是 ...

  4. 【模板】网络流-最大流 Dinic

    洛谷 3376 #include<cstdio> #include<algorithm> #include<cstring> #define N 10010 #de ...

  5. 网络流最大流——dinic算法

    前言 网络流问题是一个很深奥的问题,对应也有许多很优秀的算法.但是本文只会讲述dinic算法 最近写了好多网络流的题目,想想看还是写一篇来总结一下网络流和dinic算法以免以后自己忘了... 网络流问 ...

  6. Power Network(网络流最大流 & dinic算法 + 优化)

    Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 24019   Accepted: 12540 D ...

  7. POJ训练计划1459_Power Network(网络流最大流/Dinic)

    解题报告 这题建模实在是好建.,,好贱.., 给前向星给跪了,纯dinic的前向星居然TLE,sad.,,回头看看优化,.. 矩阵跑过了.2A,sad,,, /******************** ...

  8. 最大流dinic模板

    循环版,点的编号从0开始: ; ; const int INF = 0x3f3f3f3f; struct Edge { int to, next, cap, flow; }edge[MAXM]; in ...

  9. (网络流 最大流 Dinic || SAP)Control -- hdu --4289

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=4289 http://acm.hust.edu.cn/vjudge/contest/view.action ...

随机推荐

  1. React生命周期钩子

    最近的工作都很忙,所以很少完整的时间可以用来总结和回顾知识点,今天就趁着是周末,我准备在这里复习和回顾一下React的基础.工作中主要用的vue比较多,在工作中使用React也已经是一年前了,当时用的 ...

  2. 你真的了解Spring Framework吗?

    Java 框架 上世纪90年代,使用Java开发Web应用普遍使用J2EE标准,J2EE具有平台无关性,对事务.消息等企业级的特性都有很好的支持,但当时的J2EE仍存在一些问题: 非常复杂:EJB的诞 ...

  3. 雷林鹏分享:C# 索引器(Indexer)

    C# 索引器(Indexer) 索引器(Indexer) 允许一个对象可以像数组一样被索引.当您为类定义一个索引器时,该类的行为就会像一个 虚拟数组(virtual array) 一样.您可以使用数组 ...

  4. Java基础-String和StringBuilder类型(11)

    String类概述 字符串是由多个字符组成的一串数据字符串可以看成是字符数组 构造方法 public String(String original)public String(char[] value ...

  5. 50 Jquery 库

    一.概念: 1.jquery 的选择器和ccs 相同 2.jquery对象, dom对象的集合,类似python中list,有自己的各种方法和属性 // [dom1,dom2,.....] 3.方便之 ...

  6. Multi-target tracking by Lagrangian relaxation to min-cost network flow

    Multi-target tracking by Lagrangian relaxation to min-cost network flow high-order constraints min-c ...

  7. python-day30--粘包

    一. 什么是粘包 1.须知:只有TCP有粘包现象,UDP永远不会粘包 2.所谓粘包问题主要还是因为接收方不知道消息之间的界限,不知道一次性提取多少字节的数据所造成的. 二.两种情况下会发生粘包. 1. ...

  8. POJ-1129 Channel Allocation (DFS)

    Description When a radio station is broadcasting over a very large area, repeaters are used to retra ...

  9. FREETEXTBOX

    本文转自http://blog.csdn.net/JOHNCOOLS/archive/2006/04/08/655553.aspx感谢作者们的付出---------------版本: FreeText ...

  10. POJ 3176 Cow Bowling(dp)

    POJ 3176 Cow Bowling 题目简化即为从一个三角形数列的顶端沿对角线走到底端,所取得的和最大值 7 * 3 8 * 8 1 0 * 2 7 4 4 * 4 5 2 6 5 该走法即为最 ...