1 #include<bits/stdc++.h>
2 using namespace std;
3 typedef long long ll;
4 const int N = 1000;
5 ll dep[N], h[N], now[N],n, m, s, t, ee = 2;
6
7 struct edge{
8 ll v, next, flow;
9 }e[10010];
10 void addedge(int u, int v, int flow)
11 {
12 e[ee] = (edge){v, h[u], flow};
13 h[u] = ee++;
14 }
15 ll bfs()
16 {
17 memset(dep, -1, sizeof(dep));
18 queue<ll>q;
19 q.push(s);
20 dep[s] = 0;
21 now[s] = h[s];
22 while (q.size())
23 {
24 ll u = q.front();
25 q.pop();
26 for(int i = h[u]; i; i = e[i].next)
27 {
28 ll v = e[i].v;
29 if(dep[v] == -1 && e[i].flow > 0)
30 {
31 dep[v] = dep[u] + 1;
32 now[v] = h[v];
33 q.push(v);
34 if(v == t)return 1;
35 }
36 }
37 }
38 return 0;
39 }
40
41 ll dfs(ll u, ll path_misum)
42 {
43 if(u == t)return path_misum;
44 ll ans = 0;
45 for(int i = now[u]; i && path_misum; i = e[i].next)
46 {
47 now[u] = i;
48 ll v = e[i].v;
49 if(dep[v] == dep[u] + 1 && e[i].flow > 0)
50 {
51 ll other_flow = dfs(v, min(e[i].flow, path_misum));
52 e[i].flow -= other_flow;
53 e[i ^ 1].flow += other_flow;
54 ans += other_flow;
55 path_misum -= other_flow;
56
57 }
58 }
59 return ans;
60 }
61 ll dinic()
62 {
63 ll ans = 0;
64 while (bfs())
65 {
66 ans += dfs(s, LONG_MAX);
67 }
68 return ans;
69 }
70 int main()
71 {
72 ios::sync_with_stdio(false);cin.tie(0);
73 cin >> n >> m >> s >> t;
74 ll u, v, flow;
75 for(int i = 1; i <= m; i++)
76 {
77 cin >> u >> v >> flow;
78 addedge(u, v, flow);
79 addedge(v, u, 0);
80 }
81 cout << dinic() << "\n";
82 return 0;
83 }