summaryrefslogtreecommitdiff
path: root/graph/pushRelabel3.cpp
blob: 4f383f61db0bdb9f20e9dd10dfc98339fff9eef5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
struct edge {
	int from, to;
	ll f, c;
};

vector<edge> edges;
vector<vector<int>> adj, hs;
vector<ll> ec;
vector<int> cur, H;

void addEdge(int from, int to, ll c) {
	adj[from].push_back(sz(edges));
	edges.push_back({from, to, 0, c});
	adj[to].push_back(sz(edges));
	edges.push_back({to, from, 0, 0});
}

void addFlow(int id, ll f) {
	if (ec[edges[id].to] == 0 && f > 0)
		hs[H[edges[id].to]].push_back(edges[id].to);
	edges[id].f += f;
	edges[id^1].f -= f;
	ec[edges[id].to] += f;
	ec[edges[id].from] -= f;
}

ll maxFlow(int s, int t) {
	int n = sz(adj);
	hs.assign(2*n, {});
	ec.assign(n, 0);
	cur.assign(n, 0);
	H.assign(n, 0);
	H[s] = n;
	ec[t] = 1;//never set t to active...
	vector<int> co(2*n);
	co[0] = n - 1;
	for (int id : adj[s]) addFlow(id, edges[id].c);
	for (int hi = 0;;) {
		while (hs[hi].empty()) if (!hi--) return -ec[s];
		int v = hs[hi].back();
		hs[hi].pop_back();
		while (ec[v] > 0) {
			if (cur[v] == sz(adj[v])) {
				H[v] = 2*n;
				for (int i = 0; i < sz(adj[v]); i++) {
					int id = adj[v][i];
					if (edges[id].c - edges[id].f > 0 &&
					    H[v] > H[edges[id].to] + 1) {
						H[v] = H[edges[id].to] + 1;
						cur[v] = i;
				}}
				co[H[v]]++;
				if (!--co[hi] && hi < n) {
					for (int i = 0; i < n; i++) {
						if (hi < H[i] && H[i] < n) {
							co[H[i]]--;
							H[i] = n + 1;
				}}}
				hi = H[v];
			} else {
				auto e = edges[adj[v][cur[v]]];
				if (e.c - e.f > 0 && H[v] == H[e.to] + 1) {
					addFlow(adj[v][cur[v]], min(ec[v], e.c - e.f));
				} else {
					cur[v]++;
}}}}}