#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 1e4 + 50;
const int M = 2e5 + 50;
const int INF = 1e9;
int n, m, S, T;
int head[N], tot = 1;
struct Edge {
int nex, to, w;
}e[M];
void addedge(int a, int b, int c) {
e[++tot] = (Edge){head[a], b, c};
head[a] = tot;
}
void add(int a, int b, int c) {
addedge(a, b, c);
addedge(b, a, 0);
}
struct DINIC {
queue<int> q;
int n, S, T;
int dep[N], cur[N];
void init(int _n, int _S, int _T) {
n = _n; S = _S; T = _T;
memset(head, 0, sizeof(head));
tot = 1;
}
int bfs() {
while (!q.empty()) q.pop();
memset(dep, 0, sizeof(dep)); dep[S] = 1;
q.push(S);
while (!q.empty()) {
int now = q.front(); q.pop();
for (int i = head[now]; i; i = e[i].nex) {
int to = e[i].to;
if (!dep[to] && e[i].w) {
dep[to] = dep[now] + 1;
q.push(to);
}
}
}
return dep[T] > 0;
}
int dfs(int rt, int flow) {
if (rt == T || !flow) return flow;
for (int i = cur[rt]; i; i = e[i].nex) {
cur[rt] = i;
int to = e[i].to;
if (dep[to] == dep[rt] + 1 && e[i].w) {
int f = dfs(to, min(flow, e[i].w));
if (f) {
e[i].w -= f;
e[i ^ 1].w += f;
return f;
}
}
}
return 0;
}
int Dinic() {
int ans = 0;
while (bfs()) {
for (int i = 1; i <= n; i++) cur[i] = head[i];
while (int tmp = dfs(S, INF)) {
ans += tmp;
}
}
return ans;
}
}dinic;
int main() {
scanf("%d%d%d%d", &n, &m, &S, &T);
dinic.init(n, S, T);
for (int i = 1, u, v, w; i <= m; i++) {
scanf("%d%d%d", &u, &v, &w);
add(u, v, w); add(v, u, 0);
}
printf("%d\n", dinic.Dinic());
return 0;
}
暂无评论