#include
#include
#include
using namespace std;
const int INF = 1e9;
class Graph {
private:
vector> adj_list;
public:
Graph() {}
Graph(int n) {
adj_list.resize(n);
}
void add_edge(int a, int b) {
adj_list[a].push_back(b);
}
vector get_neighbours(int x) {
return adj_list[x];
}
};
class FordFulkerson {
private:
int n;
vector> capacity;
Graph adj;
public:
FordFulkerson(int n, vector> capacity, Graph adj) {
this->n = n;
this->capacity = capacity;
this->adj = adj;
}
bool dfs(int S, int E, vector & visited, vector & path) {
visited[S] = true;
path.push_back(S);
if(S == E) {
return true;
}
for(int neighbour : adj.get_neighbours(S)) {
if(!visited[neighbour] and capacity[S][neighbour] > 0) {
if(dfs(neighbour, E, visited, path)) {
return true;
}
}
}
path.pop_back();
return false;
}
int ford_fulkerson(int S, int E) {
int flow = 0;
vector visited(n, false);
vector path;
while(true) {
int path_flow = INF;
fill(visited.begin(), visited.end(), false);
path.clear();
if(!dfs(S, E, visited, path)) {
break;
}
for(int i = 0; i < path.size() - 1; i++) {
int a = path[i];
int b = path[i + 1];
path_flow = min(path_flow, capacity[a][b]);
}
for(int i = 0; i < path.size() - 1; i++) {
int a = path[i];
int b = path[i + 1];
capacity[a][b] -= path_flow;
capacity[b][a] += path_flow;
}
flow += path_flow;
}
return flow;
}
};
int main() {
int V = 6;
int graph[6][6] = { {0, 16, 13, 0, 0, 0},
{0, 0, 10, 12, 0, 0},
{0, 4, 0, 0, 14, 0},
{0, 0, 9, 0, 0, 20},
{0, 0, 0, 7, 0, 4},
{0, 0, 0, 0, 0, 0}
};
Graph adj(6);
vector> capacity(6, vector(6, 0));
for(int i = 0; i < 6; i++) {
for(int j = 0; j < 6; j++) {
capacity[i][j] = graph[i][j];
if(graph[i][j] != 0) {
adj.add_edge(i, j);
}
}
}
FordFulkerson ff(6, capacity, adj);
cout << ff.ford_fulkerson(0, 5) << endl;
return 0;
}