#P3381. 【模板】最小费用最大流

【模板】最小费用最大流

Description

Given a directed graph (hereinafter referred to as a network) G=(V,E)G=(V,E) with nn vertices and mm edges. The vertices are numbered 1n1 \sim n, and the edges are numbered 1m1 \sim m. The source of the network is ss, and the sink is tt. Each edge (u,v)(u,v) has a capacity limit w(u,v)w(u,v) and a unit cost c(u,v)c(u,v).

You need to assign a flow f(u,v)f(u,v) to each edge (u,v)(u,v) such that:

  1. 0f(u,v)w(u,v)0 \leq f(u,v) \leq w(u,v) (the flow on each edge does not exceed its capacity limit);
  2. p{V{s,t}}\forall p \in \{V \setminus \{s,t\}\}, $\sum_{(i,p) \in E} f(i,p) = \sum_{(p,i) \in E} f(p,i)$ (for all vertices except the source and the sink, the total inflow equals the total outflow);
  3. $\sum_{(s,i) \in E} f(s,i) = \sum_{(i,t) \in E} f(i,t)$ (the total flow leaving the source equals the total flow entering the sink).

Define the network flow F(G)=(s,i)Ef(s,i)F(G)=\sum_{(s,i)\in E} f(s,i) and the network cost C(G)=(i,j)Ef(i,j)×c(i,j)C(G)=\sum_{(i,j)\in E} f(i,j) \times c(i,j).

You need to find the minimum-cost maximum flow of the network, that is, maximize F(G)F(G) and, subject to that, minimize C(G)C(G).

Input Format

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

Each of the next mm lines contains four integers ui,vi,wi,ciu_i,v_i,w_i,c_i, representing the start vertex, end vertex, capacity limit, and unit cost of the ii-th edge.

Output Format

Output two integers: the maximum flow F(G)F(G) and, subject to F(G)F(G) being maximum, the minimum cost C(G)C(G).

4 5 4 3
4 2 30 2
4 3 20 3
2 3 20 1
2 1 30 9
1 3 40 5
50 280

Hint

Constraints:

For 100%100\% of the testdata, 1n5×1031 \leq n \leq 5 \times 10^3, 1m5×1041 \leq m \leq 5 \times 10^4, 1s,tn1 \leq s,t \leq n, uiviu_i \neq v_i, 0wi,ci1030 \leq w_i,c_i \leq 10^3, and the network’s maximum flow and minimum cost 2311\leq 2^{31}-1.

The testdata is randomly generated.

Translated by ChatGPT 5