This is a classic model in the introduction to algorithms. The power station is regarded as the source and the user as the sink. In this way, a multi-source Multi-Sink Network stream model is formed, and a total source sink is directly added.
The capacity from the source to the power station is the capacity of the power station, and the capacity from the user to the sink is the user capacity, directly run the maximum flow.
My code:
# Include <cstdio> <br/> # include <cstdlib> <br/> # include <cstring> <br/> # include <algorithm> <br/> using namespace STD; <br/> const int max = 120; <br/> const int oo = 0x3f3f3f; <br/> struct edge {<br/> int num, NE, cap; <br/>} e [2 * max]; <br/> int pre [Max], Pree [Max], low [Max], gap [Max], cur [Max], p [Max]; <br/> int Dist [Max]; <br/> int N, St, Ed, K; <br/> int get () {<br/> int ret = 0; <br/> bool flag = false; <br/> char Ch; <br/> while (CH = getchar () <'0' | ch> '9') if (CH = '-') break; <br/> If (CH = '-') Flag = true; else ret = CH-'0'; <br/> while (CH = getchar ()> = '0' & Ch <= '9') ret = RET * 10 + CH-'0'; <br/> return flag? -RET: ret; <br/>}< br/> void add (const Int & U, const Int & V, const Int & Cap) {<br/> E [K]. num = V; E [K]. CAP = CAP; E [K]. ne = P [u]; P [u] = K ++; <br/> E [K]. num = u; E [K]. CAP = 0; E [K]. ne = P [v]; P [v] = K ++; <br/>}< br/> int SAP () {<br/> int ret = 0; <br/> bool done; <br/> memset (Dist, 0, sizeof (DIST); <br/> memset (GAP, 0, sizeof (GAP )); <br/> memset (low, 0, sizeof (low); <br/> for (INT I = 0; I <n; I ++) cur [I] = P [I]; <br/> low [st] = Oo; <br/> in T u = sT; <br/> while (Dist [u] <n) {<br/> done = true; <br/> for (INT I = cur [u]; ~ I; I = E [I]. ne) {<br/> int v = E [I]. num; <br/> cur [u] = I; <br/> If (E [I]. cap & Dist [u] = DIST [v] + 1) {<br/> pre [v] = u; Pree [v] = I; <br/> low [v] = min (low [u], E [I]. CAP); <br/> U = V; <br/> If (u = ed) {<br/> do {<br/> E [Pree [u]. cap-= low [ed]; <br/> E [Pree [u] ^ 1]. cap + = low [ed]; <br/> U = pre [u]; <br/>}while (u! = ST); <br/> RET + = low [ed]; <br/>}< br/> done = false; <br/> break; <br/>}< br/> If (done) {<br/> GaP [Dist [u] --; <br/> If (! Gap [Dist [u]) return ret; <br/> Dist [u] = N; <br/> cur [u] = P [u]; <br/> for (INT I = P [u]; ~ I; I = E [I]. ne) {<br/> If (E [I]. CAP) Dist [u] = min (Dist [u], DIST [E [I]. num] + 1); <br/>}< br/> GaP [Dist [u] ++; <br/> If (u! = ST) u = pre [u]; <br/>}< br/> return ret; <br/>}< br/> int main () {<br/> int NN, NP, NC, M; <br/> char ch; <br/> while (~ Scanf ("% d", & NN, & NP, & NC, & M) {<br/> N = nn + 2; <br/> ST = nn; <br/> ED = nn + 1; <br/> for (INT I = 0; I <n; I ++) P [I] =-1; k = 0; <br/> while (M --) {<br/> while (CH = getchar ())! = '('); <Br/> int u = get (); <br/> int v = get (); <br/> int W = get (); <br/> Add (U, V, W); <br/>}< br/> while (NP --) {<br/> int u = get (); <br/> int W = get (); <br/> Add (St, U, W); <br/>}< br/> while (NC --) {<br/> int u = get (); <br/> int W = get (); <br/> Add (u, Ed, W ); <br/>}</P> <p> printf ("% d/N", SAP (); <br/>}< br/> return 0; <br/>}< br/>