1.dinic
const int MAXN = 2e3 + 5;
const int INF = 0x3f3f3f3f;
struct Edge{
int to,val,nxt;
}e[MAXN];
int head[MAXN], deep[MAXN];
int n, m, s, t, cnt;
ll res;
inline void init() {
for (int i = 1; i <= n; i++) head[i] = -1;
cnt = 0;
}
inline void add(int from, int to, int val) {
e[cnt] = Edge{to, val, head[from]};
head[from] = cnt++;
}
inline int bfs() {
for (int i = 2; i <= t; i++) deep[i] = -1;
queue<int>q;
deep[s] = 1;
q.push(s);
while (!q.empty()) {
int front = q.front();
q.pop();
for (int i = head[front]; i != -1; i = e[i].nxt) {
if (deep[e[i].to] == -1 && e[i].val) {
deep[e[i].to] = deep[front] + 1;
q.push(e[i].to);
}
}
}
return (deep[t] != -1);
}
inline int dfs(int x, int totFlow) {
if (x == t || totFlow ==0) return totFlow;
int ans = totFlow;
for (int i = head[x]; i != -1; i = e[i].nxt) {
if (deep[e[i].to] == deep[x] + 1 && e[i].val) {
int flow = dfs(e[i].to, min(totFlow,e[i].val));
e[i].val -= flow;
e[i ^ 1].val += flow;
totFlow -= flow;
if (totFlow == 0) return ans;
}
}
return ans - totFlow;
}
inline void dinic() {
while (bfs()) {
res += dfs(s, INF);
}
}