Because a wrong min and transferred half an hour - "a tragic history of OIer"
Luo Gu P3376 Network maximum flow
Title Description
If that, given a network diagram, and the source and sink, the network obtains its maximum flow.
Input and output formats
Input formats:
The first row contains four positive integers N, M, S, T, respectively, the number of points, there are the number of edges, number source, sink point number.
Next M lines contains three positive integers ui, vi, wi, represents the i ui article there from edge to reach VI, the right side of wi (i.e., the side of the maximum flow rate wi)
Output formats:
Line, contains a positive integer, is the maximum flow of the network.
Sample input and output
Input Sample # 1:
4 5 4 3
4 2 30
4 3 20
2 3 20
2 1 30
1 3 40
Output Sample # 1:
50
Explanation
Time limit: 1000ms, 128M
Data Scale:
For 30% of the data: N <= 10, M <= 25
For 70% of the data: N <= 200, M <= 1000
To 100% of the data: N <= 10000, M <= 100000
Code
#include<cstdio>
#include<cstring>
using namespace std;
const int inf = 0x3f3f3f;
int n, m, s, t, tot = -1, ans = 0;
int st[200001], dep[10001];
int min(int a, int b) { return a < b ? a : b; }
struct node
{
int to, val, last;
}c[200001];
void add(int u, int v, int w)
{
c[++tot].to = v;
c[tot].val = w;
c[tot].last = st[u];
st[u] = tot;
}
bool bfs()
{
int q[10001], l = 0, r = 1;
memset(dep, 0, sizeof dep);
dep[s] = 1;
q[r] = s;
while (l != r)
{
int head = q[++l];
for (int i = st[head]; i != -1; i = c[i].last)
{
if (c[i].val > 0 && !dep[c[i].to])
{
dep[c[i].to] = dep[head] + 1;
q[++r] = c[i].to;
}
}
}
if (!dep[t])
return false;
return true;
}
int dfs(int u, int k)
{
if (u == t) return k;
for (int i = st[u]; i != -1; i = c[i].last)
{
if ((dep[c[i].to] == dep[u] + 1) && c[i].val)
{
int p = dfs(c[i].to, min(c[i].val, k));
if (p > 0)
{
c[i].val -= p;
c[i ^ 1].val += p;
return p;
}
}
}
return 0;
}
void dinic()
{
while (bfs())
while (int p = dfs(s, inf))
ans += p;
return;
}
int main()
{
memset(st, -1, sizeof st);
scanf("%d%d%d%d", &n, &m, &s, &t);
for (int i = 0; i < m; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
add(u, v, w);
add(v, u, 0);
}
dinic();
printf("%d\n", ans);
return 0;
}