Link to this code: https://cses.fi/paste/dea6d6ff974f33bcbb647a/
// Download Speed ( https://cses.fi/problemset/task/1694/ )
// implementation of Edmonds-Karp algorithm
#include "bits/stdc++.h"
using namespace std;
using LL = long long;

vector<vector<int>> adj;
vector<vector<LL>> c;

LL bfs(int src, int snk, vector<int>& par) {
	fill(begin(par), end(par), -1);
	queue<array<LL, 2>> Q;
	Q.push({src, (LL)1e15});
	while (!Q.empty()) {
		auto [v, flow] = Q.front();
		Q.pop();

		for (int u : adj[v]) {
			if (par[u] != -1 || c[v][u] == 0) {
				continue;
			}
			par[u] = v;
			LL new_flow = min(flow, c[v][u]);
			if (u == snk) {
				return new_flow;
			}
			Q.push({u, new_flow});
		}
	}
	return 0;
}

LL maxflow(int src, int snk) {
	LL flow = 0;
	LL new_flow;
	vector<int> par(adj.size());
	while ((new_flow = bfs(src, snk, par))) {
		flow += new_flow;
		int cur = snk;
		while (cur != src) {
			int prv = par[cur];
			c[prv][cur] -= new_flow;
			c[cur][prv] += new_flow;
			cur = prv;
		}
	}

	return flow;
}

int main() {
	int n; cin >> n;
	int m; cin >> m;
	adj.resize(n);
	c.assign(n, vector<LL>(n));
	for (int i = 0, a, b, w; i < m; ++i) {
		cin >> a >> b >> w;
		--a, --b;
		adj[a].push_back(b);
		adj[b].push_back(a);
		c[a][b] += w;
	}

	// collapse multi-edges (may be optional)
	for (auto& l : adj) {
		sort(begin(l), end(l));
		l.erase(unique(begin(l), end(l)), end(l));
	}

	cout << maxflow(0, n - 1) << "\n";
}