#P3376. 【模板】网络最大流

【模板】网络最大流

Description

As stated, given a network graph and its source and sink, compute the maximum flow of this network.

Input Format

The first line contains four positive integers n,m,s,tn, m, s, t, denoting the number of vertices, the number of directed edges, the index of the source, and the index of the sink, respectively.

Each of the next mm lines contains three positive integers ui,vi,wiu_i, v_i, w_i, meaning that the ii-th directed edge goes from uiu_i to viv_i with capacity wiw_i (i.e., the maximum flow on this edge is wiw_i).

Output Format

One line containing a single positive integer, which is the maximum flow of the network.

4 5 4 3
4 2 30
4 3 20
2 3 20
2 1 30
1 3 30

50

Hint

Explanation for Sample Input/Output 1

There are 33 paths in the figure:

  • 4234 \to 2 \to 3, which can carry a flow of 2020.
  • 434 \to 3, which can carry a flow of 2020.
  • 42134 \to 2 \to 1 \to 3, which can carry a flow of 1010 (edge 424 \to 2 has already used 2020 units of flow beforehand).

Therefore, the total flow is 20+20+10=5020 + 20 + 10 = 50. Output 5050.


Constraints

  • For 30%30\% of the testdata, it is guaranteed that n10n \le 10, m25m \le 25.
  • For 100%100\% of the testdata, it is guaranteed that 1n2001 \le n \le 200, 1m50001 \le m \le 5000, 0w<2310 \le w < 2^{31}.

Translated by ChatGPT 5