luogu#P4722. 【模板】最大流 加强版 / 预流推进

【模板】最大流 加强版 / 预流推进

Problem Description

Given nn vertices and mm directed edges, with a capacity on each edge, find the maximum flow from vertex ss to vertex tt.

Input Format

The first line contains four positive integers nn, mm, ss, tt, separated by spaces, representing the number of vertices, the number of directed edges, the source vertex index, and the sink vertex index.

The next mm lines each contain three positive integers uiu_i, viv_i, cic_i, separated by spaces, meaning that the ii-th directed edge starts from uiu_i, ends at viv_i, and has capacity cic_i.

Output Format

Output one integer, the maximum flow from ss to tt.

7 14 1 7
1 2 5
1 3 6
1 4 5
2 3 2
2 5 3
3 2 2
3 4 3
3 5 3
3 6 7
4 6 5
5 6 1
6 5 1
5 7 8
6 7 7

14
10 16 1 2
1 3 2
1 4 2
5 2 2
6 2 2
3 5 1
3 6 1
4 5 1
4 6 1
1 7 2147483647
9 2 2147483647
7 8 2147483647
10 9 2147483647
8 5 2
8 6 2
3 10 2
4 10 2

8

Hint

Constraints: 1n12001\leqslant n \leqslant 1200, 1m1200001\leqslant m \leqslant 120000, 1c23111\leqslant c \leqslant 2^{31}-1.

It is guaranteed that the answer does not exceed 23112^{31}-1.

The time complexity of common network flow algorithms is O(n2m)O(n^2 m), so please optimize your algorithm as much as possible.

testdata provider: @negiizhao.

(If anyone passes this problem using Dinic’s algorithm, please send a private message to the uploader.)

Translated by ChatGPT 5