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 ...
随机推荐
- Activity Lifecycle
Activity Lifecycle Activities in the system are managed as an activity stack(activity栈?). When a new ...
- Spring AOP配置文件
在<aop:config>...</aop:config>报错: Multiple annotations found at this line: - cvc-complex- ...
- node实现http上传文件进度条 -我们到底能走多远系列(37)
我们到底能走多远系列(37) 扯淡: 又到了一年一度的跳槽季,相信你一定准备好了,每每跳槽,总有好多的路让你选,我们的未来也正是这一个个选择机会组合起来的结果,所以尽可能的找出自己想要的是什么再做决定 ...
- linux安装sqlcmd登录sqlserver
首先从微软网站下载sqlncli安装文件,link. 因为是在内网安装,首先手工下载unixODBC2.3.0.tar.gz,下载后上传到服务器. 将下载的tar文件文件,放在同build_dm.sh ...
- Java 性能优化实战记录(2)---句柄泄漏和监控
前言: Java不存在内存泄漏, 但存在过期引用以及资源泄漏. (个人看法, 请大牛指正) 这边对文件句柄泄漏的场景进行下模拟, 并对此做下简单的分析.如下代码为模拟一个服务进程, 忽略了句柄关闭, ...
- Junit单元测试细节
1.中心思想: 单元测试不是证明你对,而是证明你没错 2.基本注解应用 注解 使用环境 @Test 标志这个方法需要单元测试 @BeforeClass 在所有单元测试方法前执行 ps:需要是stati ...
- ZOJ 1234 Chopsticks
原题链接 题目大意:有这么一个公式 A,B,C(A<=B<=C), (A-B)^2来衡量这对数字的好坏,值越小越好.现在给出一个数组,要求每三个配对,最后得到的每组值总和最小. 解法:我是 ...
- android中的空格及汉字的宽度
在Android布局中进行使用到空格,以便实现文字的对齐.那么在Android中如何表示一个空格呢? 空格: 窄空格: 一个汉字宽度的空格: [用两个空格( )占一个汉字的宽度时,两个空格比 ...
- mysql 5.5及以前版本的编码问题“Incorrect string value: '\xE6\x9B\xB9\xE5\x86\xAC...' for column 'realname' at row 1”
遇到这个问题,所有的编码都设为utf8了,还是没有用,各种乱码,后来发现这是mysql自己的问题,它在5.5及之前的版本只支持3字节的utf8编码,出现4字节的utf编码时出现错误,参考: http: ...
- 用360安全浏览器控制网速,调试loading
360安全浏览器 按f12 两个按钮的意思分别为禁止缓存,网络设置,这样就能控制网速了,调试loading了