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 ;
}
上一篇:js 日期天数相加减,格式化yyyy-MM-dd


下一篇:ORM框架--GreenDao 3.0基本使用指南