First problem to learn Max Flow.

Ford-Fulkerson is a group of algorithms - Dinic is one of it.
It is an iterative process: we use BFS to check augament-ability, and use DFS to augment it.

Here is the code with my comments from its tutorial

#include <cmath>
#include <climits>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std; /*
* Graph Model
*/
const int MAXA = ;
const int MAXV = ; int A, V, source, dest;
// index based logging
int cap[MAXA], flow[MAXA], ady[MAXA], nexts[MAXA], last[MAXV];
int now[MAXA], level[MAXV]; void ADD(int u, int v, int c) // from u to v with cap c
{
// + edge
cap[A] = c; flow[A] = ;
ady[A] = v; nexts[A] = last[u]; last[u] = A++;
// - edge
cap[A] = ; flow[A] = ;
ady[A] = u; nexts[A] = last[v]; last[v] = A++;
} /*
* Dinic Algorithm
*/
bool BFS(int source, int dest)
{
memset(level, -, sizeof(level));
level[source] = ; queue<int> q;
q.push(source);
while (!q.empty() && level[dest] == -)
{
int u = q.front(); q.pop(); // from
for (int i = last[u]; i != -; i = nexts[i])
{
int v = ady[i]; // to
if (level[v] == - && flow[i] < cap[i])
{
level[v] = level[u] + ; // mark level
q.push(v);
}
} }
return level[dest] != -;
} int DFS(int u, int aux)
{
if (u == dest) return aux; for (int i = now[u]; i != -; now[u] = i = nexts[i])
{
int v = ady[i];
// next aux-able level node
if (level[v] > level[u] && flow[i] < cap[i])
{
int ret = DFS(v, min(aux, cap[i] - flow[i]));
if (ret > )
{
flow[i] += ret; // + edge
flow[i ^ ] -= ret;// - edge
return ret;
}
}
}
return ;
} long long Dinic()
{
long long flow = , aum;
while (BFS(source, dest))
{
for (int i = ; i <= V; i++) now[i] = last[i];
while ((aum = DFS(source, INT_MAX)) > ) flow += aum;
}
return flow;
} /*
*
*/
int main()
{
// [index]: first n is cluster, next m is wizard..
memset(last, -, sizeof(last)); int n, m, v, cc;
cin >> n >> m; source = ;
V = dest = n + m + ; // 1. Source -> Cluster with No. with people
// Cluster -> Dest with cap of 1 - means no transform
// no. of people of each skill
for (int i = ; i <= n; i++)
{
cin >> v;
if (v) ADD(source, i, v);
ADD(i, dest, ); // a non-transformed edge
}
// wizard info
for (int i = ; i <= m; i++) // m wizards
{
// array A - index of from-skill
cin >> cc;
for (int j = ; j < cc; j++)
{
cin >> v;
ADD(v, n + i, ); // skill[v](from) -> wizard[i]
}
// array B - index of to-skill
cin >> cc;
for (int j = ; j < cc; j++)
{
cin >> v;
ADD(n + i, v, ); // wizard[i] -> skill[v](to)
}
} cout << Dinic() << endl;
return ;
}

HackerRank "Training the army" - Max Flow的更多相关文章

  1. BZOJ 4390: [Usaco2015 dec]Max Flow

    4390: [Usaco2015 dec]Max Flow Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 177  Solved: 113[Submi ...

  2. 洛谷P3128 [USACO15DEC]最大流Max Flow [树链剖分]

    题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his b ...

  3. Max Flow

    Max Flow 题目描述 Farmer John has installed a new system of N−1 pipes to transport milk between the N st ...

  4. min cost max flow算法示例

    问题描述 给定g个group,n个id,n<=g.我们将为每个group分配一个id(各个group的id不同).但是每个group分配id需要付出不同的代价cost,需要求解最优的id分配方案 ...

  5. [Luogu 3128] USACO15DEC Max Flow

    [Luogu 3128] USACO15DEC Max Flow 最近跟 LCA 干上了- 树剖好啊,我再也不想写倍增了. 以及似乎成功转成了空格选手 qwq. 对于每两个点 S and T,求一下 ...

  6. [Usaco2015 dec]Max Flow 树上差分

    [Usaco2015 dec]Max Flow Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 353  Solved: 236[Submit][Sta ...

  7. 洛谷P3128 [USACO15DEC]最大流Max Flow

    P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of N-1N−1 pipes to transpo ...

  8. BZOJ4390: [Usaco2015 dec]Max Flow

    BZOJ4390: [Usaco2015 dec]Max Flow Description Farmer John has installed a new system of N−1 pipes to ...

  9. P3128 [USACO15DEC]最大流Max Flow(LCA+树上差分)

    P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of  pipes to transport mil ...

随机推荐

  1. 快速构建express项目

    构建node项目 github地址 https://github.com/haoyongliang/quickly-create-node-project.git 创建最基本的node项目 1.全局安 ...

  2. UVa 12558 - Egyptian Fractions (HARD version)

    题目大意: 给出一个真分数,把它分解成最少的埃及分数的和.同时给出了k个数,不能作为分母出现,要求解的最小的分数的分母尽量大. 分析: 迭代加深搜索,求埃及分数的基础上,加上禁用限制就可以了.具体可以 ...

  3. UI学习笔记---第六天

    UIControl及其子类 UISegmentedControl的用法 UISegmentedControl是iOS中得分段控件,每个segment都能被点击,相当于集成了若干个button.通常我们 ...

  4. ZOJ 1007 Numerical Summation of a Series

    原题链接 题目大意:x的取值从0.000到2.000,输出每个x对应的y(x)的值 解法:参考了这篇日志http://www.cnblogs.com/godhand/archive/2010/04/2 ...

  5. 选数 2002年NOIP全国联赛普及组

    题目描述 Description 已知 n 个整数 x1,x2,-,xn,以及一个整数 k(k<n).从 n 个整数中任选 k 个整数相加,可分别得到一系列的和.例如当 n=4,k=3,4 个整 ...

  6. ntpdate:no server suitable for synchronization found

    Question: 在使用ntpdate同步时间时,出现了no server suitable for synchronization found的报错. 通过ntpdate -d s2m.time. ...

  7. knockoutJs基础1 - 简单的knockoutjs实现

    简单的knockoutjs实现 1.knockoutJs是在MVVM的机制下实现的,所以要有view(HTML页面中的DOM标签)和viewModel(JavaScript中的js代码). 2.在vi ...

  8. 【NOIP2007】矩阵取数

    因为傻逼写错高精度搞了一下午浪费好多时间,好想哭qaq 原题: 帅帅经常更同学玩一个矩阵取数游戏:对于一个给定的n*m的矩阵,矩阵中的每个元素aij据为非负整数.游戏规则如下: 1. 每次取数时须从每 ...

  9. php会话(session)生命周期概念介绍及设置更改和回收

    http://www.169it.com/article/8429580816135935852.html https://my.oschina.net/jiec/blog/227252  sessi ...

  10. Object-c 语言

    字符串操作: http://www.myexception.cn/mobile/455287.html 1,判断两字符串是否相同 NSString *str1 = @"hello pepe& ...