EK
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<math.h>
#include<set>
#define N 100010
#define LL long long
using namespace std;
const int inf = 0x3f3f3f3f;
int head[210];
int vis[210];
LL f[210][210];
LL amin[210];
int pre[210];
struct node
{
int v, net;
LL s;
}e[N];
int n, s, t, cnt = 1;//从2开始统计
LL ans = 0;
void add(int u, int v, LL s)
{
e[++cnt].v = v;
e[cnt].s = s;
e[cnt].net = head[u];
head[u] = cnt;
}
int bfs()
{
memset(vis, 0, sizeof(vis));
queue<int>q;
q.push(s);
vis[s] = 1;
amin[s] = inf;
while (!q.empty())
{
int now = q.front();
q.pop();
for (int i = head[now]; i; i = e[i].net)
{
if (e[i].s == 0 || vis[e[i].v])continue;
amin[e[i].v] = min(e[i].s, amin[now]);
pre[e[i].v] = i;
q.push(e[i].v);
vis[e[i].v] = 1;
if (e[i].v == t)return 1;
}
}
return 0;
}
void upde()
{
int x = t;
while (x != s)
{
int i = pre[x];
e[i].s -= amin[t];
e[i ^ 1].s += amin[t];//反向边
x = e[i ^ 1].v;
}
ans += amin[t];
}
int main()
{
int m;
scanf("%d%d%d%d", &n, &m, &s, &t);
int u, v, s;
while (m--)
{
scanf("%d%d%lld", &u, &v, &s);
f[u][v] += s;
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (f[i][j])
{
add(i, j, f[i][j]);
add(j, i, 0);//反向边
}
}
}
while (bfs())
{
upde();
}
printf("%lld", ans);
return 0;
}
dinic
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<math.h>
#include<set>
#define N 210
#define LL long long
using namespace std;
const int inf = 0x3f3f3f3f;
const long long inf1 = 2005020600;
int head[N];
struct node
{
int v, nex;
LL w;
}e[10010];
int cnt = 1, n, s, t;
LL f[N][N];
int cut[N];
int dep[N];
void add(int u, int v, LL w)
{
e[++cnt].v = v;
e[cnt].w = w;
e[cnt].nex = head[u];
head[u] = cnt;
}
int bfs()
{
for (int i = 1; i <= n; i++)dep[i] = inf;
queue<int>q;
q.push(s), dep[s] = 0;
while (!q.empty())
{
int u = q.front(); q.pop();
for (int i = head[u]; i; i = e[i].nex)
{
int v = e[i].v;
LL w = e[i].w;
if (w > 0 && dep[u] + 1 < dep[v])
{
dep[v] = dep[u] + 1;
if (v == t)return 1;//优化
q.push(v);
}
}
}
return dep[t] != inf;
}
LL dfs(int x, LL mw)
{
if (x == t)return mw;
for (int i = cut[x]; i; i = e[i].nex)
{
cut[x] = i;
int v = e[i].v;
LL w = e[i].w;
if (w <= 0 || dep[v] != dep[x] + 1)continue;
LL cw = dfs(v, min(mw, w));
if (cw <= 0)continue;
e[i].w -= cw;
e[i ^ 1].w += cw;
return cw;
}
return 0;
}
LL dinic()
{
LL ans = 0;
while (bfs())
{
for (int i = 1; i <= n; i++)
{
cut[i] = head[i];//弧优化
}
while (LL c = dfs(s, inf1))
{
ans += c;
}
}
return ans;
}
int main()
{
int m;
scanf("%d%d%d%d", &n, &m, &s, &t);
int u, v, w;
while (m--)
{
scanf("%d%d%lld", &u, &v, &w);
f[u][v] += w;
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (f[i][j])
{
add(i, j, f[i][j]);
add(j, i, 0);
}
}
}
cout << dinic();
}