HackerRank "Training the army" - Max Flow
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的更多相关文章
- BZOJ 4390: [Usaco2015 dec]Max Flow
4390: [Usaco2015 dec]Max Flow Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 177 Solved: 113[Submi ...
- 洛谷P3128 [USACO15DEC]最大流Max Flow [树链剖分]
题目描述 Farmer John has installed a new system of pipes to transport milk between the stalls in his b ...
- Max Flow
Max Flow 题目描述 Farmer John has installed a new system of N−1 pipes to transport milk between the N st ...
- min cost max flow算法示例
问题描述 给定g个group,n个id,n<=g.我们将为每个group分配一个id(各个group的id不同).但是每个group分配id需要付出不同的代价cost,需要求解最优的id分配方案 ...
- [Luogu 3128] USACO15DEC Max Flow
[Luogu 3128] USACO15DEC Max Flow 最近跟 LCA 干上了- 树剖好啊,我再也不想写倍增了. 以及似乎成功转成了空格选手 qwq. 对于每两个点 S and T,求一下 ...
- [Usaco2015 dec]Max Flow 树上差分
[Usaco2015 dec]Max Flow Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 353 Solved: 236[Submit][Sta ...
- 洛谷P3128 [USACO15DEC]最大流Max Flow
P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of N-1N−1 pipes to transpo ...
- BZOJ4390: [Usaco2015 dec]Max Flow
BZOJ4390: [Usaco2015 dec]Max Flow Description Farmer John has installed a new system of N−1 pipes to ...
- P3128 [USACO15DEC]最大流Max Flow(LCA+树上差分)
P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of pipes to transport mil ...
随机推荐
- cookie 和 session 机制
cookie机制 Cookie实际上是Web服务端与客户端(典型的是浏览器)交互时彼此传递的一部分内容,内容可以是任意的,但要在允许的长度范围之内.客户端会将它保存在本地机器上(如IE便会保存在本地的 ...
- 蓝牙(Profile)构成
蓝牙剖面(Profile)构成 蓝牙剖面(Profile)及其相互关系 蓝牙SIG定义13种蓝牙剖面及其相互关系如下图: 一个剖面若直接或间接采用了另一个剖面的部分或全部功能则称该剖面依赖于另一剖面. ...
- 获得项目的绝对地址 getRequestURI,getRequestURL的区别
java获得tomcat项目的绝对地址 String basePath = request.getScheme()+"://"+request.getServerName()+&q ...
- 111. Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- boot/setup.S
!! setup.S Copyright (C) 1991, 1992 Linus Torvalds!! setup.s is responsible for getting th ...
- CSS--滚动条设置;
CSS滚动条实现步骤及美化小技巧 1.overflow-y : 设置当对象的内容超过其指定高度时如何管理内容:overflow-x : 设置当对象的内容超过其指定宽度时如何管理内容. 参数:visib ...
- JavaScript学习记录总结(九)——移动添加效果
<!DOCTYPE html><html><head><title>moveOption.html</title> <meta nam ...
- VMware虚拟机打开不了操作系统的解决方案
1. 当你的VMware虚拟机出现下面这种情况的时候 解决方案:找到此VM安装的文件夹.在此文件夹下,将名字以“.lck ”结尾的文件夹全部重命名,重命名的名字随意好了.然后再到VMWARE里启动你的 ...
- (转) Deep Learning in a Nutshell: Reinforcement Learning
Deep Learning in a Nutshell: Reinforcement Learning Share: Posted on September 8, 2016by Tim Dettm ...
- data pump (数据抽取)测试
背景介绍>利用db_link直接pump抽取,减少转储文件集. 前提: 授权> grant create public database link,create database l ...