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. 机器学习技法-神经网络(NNet)

    课程地址:https://class.coursera.org/ntumltwo-002 重要!重要!重要~ 一.神经网络(NNet)的动机 神经网络有很久的历史,由感知机(perceptron)模型 ...

  2. jq实现动态添加样式

    <script> $(function(){ $("#list_zlm > a").hover(function(){ $(this).addClass(&quo ...

  3. js实现分页列表添加样式

    <script> var dUrl=window.location.href; var cUrl=(dUrl.substring(0, dUrl.indexOf('list_'))); v ...

  4. C#使用FtpWebRequest上传文件

    System.Net命名空间下的FtpWebRequest类实现了ftp协议的.Net实现. FtpWebRequest.KeepAlive指定在请求完成后服务器是否要马上关闭连接 FtpWebReq ...

  5. xmind的第三天笔记

  6. ZOJ 1056 The Worm Turns

    原题链接 题目大意:贪吃蛇的简化版,给出一串操作命令,求蛇的最终状态是死是活. 解法:这条蛇一共20格的长度,所以用一个20个元素的队列表示,队列的每个元素是平面的坐标.每读入一条指令,判断其是否越界 ...

  7. POJ 3233 Matrix Power Series 矩阵快速幂

    设S[k] = A + A^2 +````+A^k. 设矩阵T = A[1] 0 E E 这里的E为n*n单位方阵,0为n*n方阵 令A[k] = A ^ k 矩阵B[k] = A[k+1] S[k] ...

  8. html部分---格式与布局;

    一:position:fixed(相对于浏览器窗口来对元素进行定位) <style type="text/css"> .aa { position:fixed; lef ...

  9. Objective-c——UI基础开发第六天(UITableView)

    一.UITableView的简单使用 显示要素: 1.显示多少给区组 2.显示多少行数据 3.每行显示什么内容 代理不会提醒你有什么方法没调用,但是UITableViewDataSource会 1)用 ...

  10. timus 1106 Two Teams(二部图)

    Two Teams Time limit: 1.0 secondMemory limit: 64 MB The group of people consists of N members. Every ...