我觉得这个dinic的算法和之前的增广路法差不多
、使用BFS对残余网络进行分层,在分层时,只要进行到汇点的层次数被算出即可停止,
因为按照该DFS的规则,和汇点同层或更下一层的节点,是不可能走到汇点的。 、分完层后,从源点开始,用DFS从前一层向后一层反复寻找增广路(即要求DFS的每一步都必须要走到下一层的节点)。 、DFS过程中,要是碰到了汇点,则说明找到了一条增广路径。此时要增加总流量的值,消减路径上各边的容量,
并添加反向边,即所谓的进行增广。 、DFS找到一条增广路径后,并不立即结束,而是回溯后继续DFS寻找下一个增广路径。回溯到的结点满足以下的条件:   ) DFS搜索树的树边(u,v)上的容量已经变成0。即刚刚找到的增广路径上所增加的流量,等于(u,v)本次增广前的容量。
(DFS的过程中,是从u走到更下层的v的)
  ) u是满足条件 )的最上层的节点。 、如果回溯到源点而且无法继续往下走了,DFS结束。因此,一次DFS过程中,可以找到多条增广路径。
、DFS结束后,对残余网络再次进行分层,然后再进行DFS当残余网络的分层操作无法算出汇点的层次(即BFS到达不了汇点)时,
算法结束,最大流求出。 #include <cstdio>
#include <cstdlib>
#include <vector>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 1e5;
struct edge
{
int u, v, c, f;
edge(int u, int v, int c, int f) :u(u), v(v), c(c), f(f) {}
};
vector<edge>e;
vector<int>G[maxn];
int level[maxn];//BFS分层,表示每个点的层数
int iter[maxn];//当前弧优化
int m;
void init(int n)
{
for (int i = ; i <= n; i++)G[i].clear();
e.clear();
}
void addedge(int u, int v, int c)
{
e.push_back(edge(u, v, c, ));
e.push_back(edge(v, u, , ));
m = e.size();
G[u].push_back(m - );
G[v].push_back(m - );
}
void BFS(int s)//预处理出level数组
//直接BFS到每个点
{
memset(level, -, sizeof(level));
queue<int>q;
level[s] = ;
q.push(s);
while (!q.empty())
{
int u = q.front();
q.pop();
for (int v = ; v < G[u].size(); v++)
{
edge& now = e[G[u][v]];
if (now.c > now.f && level[now.v] < )
{
level[now.v] = level[u] + ;
q.push(now.v);
}
}
}
}
int dfs(int u, int t, int f)//DFS寻找增广路
{
if (u == t)return f;//已经到达源点,返回流量f
for (int &v = iter[u]; v < G[u].size(); v++)
//这里用iter数组表示每个点目前的弧,这是为了防止在一次寻找增广路的时候,对一些边多次遍历
//在每次找增广路的时候,数组要清空
{
edge &now = e[G[u][v]];
if (now.c - now.f > && level[u] < level[now.v])
//now.c - now.f > 0表示这条路还未满
//level[u] < level[now.v]表示这条路是最短路,一定到达下一层,这就是Dinic算法的思想
{
int d = dfs(now.v, t, min(f, now.c - now.f));
if (d > )
{
now.f += d;//正向边流量加d
e[G[u][v] ^ ].f -= d;
//反向边减d,此处在存储边的时候两条反向边可以通过^操作直接找到
return d;
}
}
}
return ;
}
int Maxflow(int s, int t)
{
int flow = ;
for (;;)
{
BFS(s);
if (level[t] < )return flow;//残余网络中到达不了t,增广路不存在
memset(iter, , sizeof(iter));//清空当前弧数组
int f;//记录增广路的可增加的流量
while ((f = dfs(s, t, INF)) > )
{
flow += f;
}
}
return flow;
}

接下来贴一个模板题,但是这个题目的图有点难建

C - Dining

POJ - 3281

Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others.

Farmer John has cooked fabulous meals for his cows, but he forgot to check his menu against their preferences. Although he might not be able to stuff everybody, he wants to give a complete meal of both food and drink to as many cows as possible.

Farmer John has cooked F (1 ≤ F ≤ 100) types of foods and prepared D (1 ≤ D ≤ 100) types of drinks. Each of his N (1 ≤ N ≤ 100) cows has decided whether she is willing to eat a particular food or drink a particular drink. Farmer John must assign a food type and a drink type to each cow to maximize the number of cows who get both.

Each dish or drink can only be consumed by one cow (i.e., once food type 2 is assigned to a cow, no other cow can be assigned food type 2).

Input

Line 1: Three space-separated integers: NF, and D 
Lines 2.. N+1: Each line i starts with a two integers Fi and Di, the number of dishes that cow i likes and the number of drinks that cow i likes. The next Fiintegers denote the dishes that cow i will eat, and the Di integers following that denote the drinks that cow i will drink.

Output

Line 1: A single integer that is the maximum number of cows that can be fed both food and drink that conform to their wishes

Sample Input

4 3 3
2 2 1 2 3 1
2 2 2 3 1 2
2 2 1 3 1 2
2 1 1 3 3

Sample Output

3

Hint

One way to satisfy three cows is: 
Cow 1: no meal 
Cow 2: Food #2, Drink #2 
Cow 3: Food #1, Drink #1 
Cow 4: Food #3, Drink #3 
The pigeon-hole principle tells us we can do no better since there are only three kinds of food or drink. Other test data sets are more challenging, of course.
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cstring>
#include <vector>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 1e5 + ;
int n, f, d;
struct node
{
int from, to, cap, flow;
node(int from = , int to = , int cap = , int flow = ) :from(from), to(to), cap(cap), flow(flow) {}
};
vector<node>e;
vector<int>G[maxn];
int level[maxn], iter[maxn];
void add(int u, int v, int w)
{
e.push_back(node(u, v, w, ));
e.push_back(node(v, u, , ));
int m = e.size();
G[u].push_back(m - );
G[v].push_back(m - );
} void bfs(int s)//这个是为了构建层次网络,也就是level的构建
{
memset(level, -, sizeof(level));
queue<int>que;
que.push(s);
level[s] = ;
while (!que.empty())
{
int u = que.front(); que.pop();
for (int i = ; i < G[u].size(); i++)
{
node &now = e[G[u][i]];
if (now.cap > now.flow&&level[now.to] < )//只有这个没有满并且没有被访问过才可以被访问
{
level[now.to] = level[u] + ;
que.push(now.to);
}
}
}
} int dfs(int u, int v, int f)
{
if (u == v) return f;
for (int &i = iter[u]; i < G[u].size(); i++)
{
node &now = e[G[u][i]];
if (now.cap > now.flow&&level[now.to] > level[u])
{
int d = dfs(now.to, v, min(f, now.cap - now.flow));
if (d > )
{
now.flow += d;
e[G[u][i] ^ ].flow -= d;
return d;
}
}
}
return ;
} int Maxflow(int s, int t)
{
int flow = ;
while ()
{
bfs(s);
if (level[t] < ) return flow;
memset(iter, , sizeof(iter));
int f;
while ((f = dfs(s, t, inf) > )) flow += f;
}
}
void init()
{
for (int i = ; i <= n + ; i++) G[i].clear();
e.clear();
} int main()
{
while (cin >> n >> f >> d)
{
init();
int s = , t = f + * n + d + ;
for (int i = ; i <= f; i++) add(s, i, );
for (int i = ; i <= n; i++)
{
int a, b;
cin >> a >> b;
while (a--)//与牛i相连
{
int x;
cin >> x;
add(x, f + i, );
}
add(f + i, f + n + i, );
while (b--)
{
int x;
cin >> x;
add(f + n + i, f + * n + x, );
}
}
for (int i = ; i <= d; i++) add(f + * n + i, t, );
int ans = Maxflow(s, t);
cout << ans << endl;
}
return ;
}

网络流 之 dinic算法的更多相关文章

  1. [知识点]网络流之Dinic算法

    // 此博文为迁移而来,写于2015年2月6日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102vrg4.html      ...

  2. [无效]网络流之Dinic算法

    // 此博文为迁移而来,写于2015年2月6日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102vrg4.html UPDA ...

  3. 网络流之Dinic算法

    初学网络流.存一下Dinic板子. 复杂度O(n^2*m) UVA - 1515 Pool construction 把每个草地与 S 相连,花费为dig,每个洞与 T 相连,花费为 然后对于每个两个 ...

  4. 网络流 之 dinic 算法

    网络流指的是:网络流(network-flows)是一种类比水流的解决问题方法.(类似于水管群,有一个源点(水无限多),和一个汇点,最大流就代表这个点水管群(边集)每秒最大能送道汇点的水量) 这个怎么 ...

  5. Secret Milking Machine POJ - 2455 网络流(Dinic算法---广搜判断+深搜增广)+时间优化+二分

    题意: 第一行输入N M C ,表示从1到N有M条无向边,现在要从1走到N 走C次完全不同的路径,求最长边的最小值.下面M行是从a点到b点的距离. 建图: 题上说从两点之间可以有多条边,问的是从1~N ...

  6. 网络流(dinic算法)

    洛谷p3376 https://www.luogu.com.cn/problem/P3376 #include <iostream> #include <cstdio> #in ...

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

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

  8. 网络流入门—用于最大流的Dinic算法

    "网络流博大精深"-sideman语 一个基本的网络流问题 最早知道网络流的内容便是最大流问题,最大流问题很好理解: 解释一定要通俗! 如右图所示,有一个管道系统,节点{1,2,3 ...

  9. Dinic算法(研究总结,网络流)

    Dinic算法(研究总结,网络流) 网络流是信息学竞赛中的常见类型,笔者刚学习了最大流Dinic算法,简单记录一下 网络流基本概念 什么是网络流 在一个有向图上选择一个源点,一个汇点,每一条边上都有一 ...

随机推荐

  1. ASP.NET中共有哪几种类型的控件?其中,HTML控件、HTML服务器控件和WEB服务器控件之间有什么区别

    ASP.NET的控件包括WEB服务器控件.WEB用户控件.WEB自定义控件.HTML服务器控件和HTML控件.HTML控件.HTML服务器控件和WEB服务器控件之间的区别如下所示.q      HTM ...

  2. C#线程安全使用(一)

    关于Task的使用,一直都是半知半解,最近终于有时间详细的看了一遍MSDN,作为备忘录,将心得也记录下来和大家分享. 首先,根据MSDN的描述,Task是FrameWork4引进的新功能,他和ConC ...

  3. 痞子衡嵌入式:第一本Git命令教程(1)- 准备(init/config/.gitignore)

    今天是Git系列课程第一课,痞子衡给大家要讲的是创建仓库的准备工作. 1.建仓库git init 第一步是创建一个空仓库,这是一切操作的前提. // 打开git bash命令行,切换到指定目录下 ja ...

  4. python基础3--函数

    1.函数定义 你可以定义一个由自己想要功能的函数,以下是简单的规则: 函数代码块以def关键词开头,后接函数标识符名称和圆括号(). 任何传入参数和自变量必须放在圆括号中间.圆括号之间可以用于定义参数 ...

  5. 【CentOS7】服务环境搭建

    用了两天时间,完成了服务环境的搭建.记录下了搭建的过程,搭建细节并没有记录. 1.OpenSSH. (1)yum search ssh (2)yum install openssh-server (3 ...

  6. 2-SAT速成

    本文只做总结性说明 2-SAT 2-SAT是k-SAT问题的一种,k-SAT问题在\(k>=3\)时已经被证明是NP完全问题 2-SAT问题定义比较简单 有n个布尔变量\(x_1-x_n\).给 ...

  7. .net mvc session失效问题

    最近解决基于.net mvc项目的session失效问题,这个跟大家聊聊. 1.问题分析 .net mvc中,Session失效需要考虑几种情况: 基于权限认证的Action,使用非Ajax请求: 基 ...

  8. (办公)TOKEN

    token就是HTTP认证,输入正确的token,在放在Authorization header中发送给服务器,认证成功.,就可以正确的拿到接口数据. 举个例子: 第一步:  客户端发送http re ...

  9. JavaScript中的高阶函数

    之前写的<JavaScript学习手册>,客户跟我说有些内容不适合初学者,让我删了,感觉挺可惜的,拿到这里和大家分享. JavaScript中的一切都是对象,这句话同样适用于函数.函数对象 ...

  10. Linux中逻辑卷的快照与还原

    有关逻辑卷的其他操作,请看: Linux中对逻辑卷的建立 Linux中对逻辑卷进行扩容与缩小 Linux中对逻辑卷的移除 LVM还有快照的功能,类似windows的系统还原点.其特点: 1.快照卷的容 ...