Zoj 3229 shoot the bullet-The Source Sink has the upper and lower bound maximum stream recursion and non-recursive SAP

Source: Internet
Author: User
/* Question: someone takes photos of M beauty in N days. Each beauty has a minimum requirement. Each day, there is a maximum number of photos. n m is given first. Each beauty has a minimum of N. {the number of beautiful women taking photos on I day. the maximum number of photos is the upper limit of the number of beautiful women in turn.} the upper and lower limits of the source sink have the upper and lower limits. The maximum flow is obtained for the additional source sink ss tt. If there is a feasible flow, the maximum flow is obtained for s t. this is the second time I wrote this question, the first write was not smooth (it may be due to a problem in the diagram, not timeout or error. The maximum flow method is changed to three types: SAP recursion and SAP non-recursion, dinic I doubt whether my largest stream template has a vulnerability. The recursive sap below was originally used. At that time, I thought there was a problem. Actually, there was a problem, but it still times out. Today, it seems to be a problem with the diagram) the following non-recursive sap runs 330 Ms. Recursive sap runs 270 Ms. It seems that my template speed is good */# include <stdio. h> # include <string. h> # define INF 0x7fffffffstru CT edge // side {int U, V, F, next, B, c; // Number of the edge in the stream available to the front node and the back node of the edge} e [400000]; int head [1400], in [1400], out [1400], n, m, S, T, SS, TT, Yong, indexx [400000], IND, sum; /* the inbound lower bound and-outbound lower bound of the head linked list header at a certain point do not use Yong to point to the next unused edge. indexx stores the number index of the edge of a beauty day in string. h, so I changed this IND to indicate the next unused indexxsum incoming TT traffic and to determine whether there is a feasible solution. */void INI () // initialize {memset (Head,-1, sizeof (head); Yong = 0; memset (in, 0, sizeof (in); s = m + n, t = S + 1, SS = t + 1, tt = SS + 1; ind = 0; s Um = 0;} void Adde (int from, int to, int Xia, int Shang) // Add edge {// Add edge e [Yong]. U = from, E [Yong]. V = To, E [Yong]. F = Shang-xia, E [Yong]. B = Xia, E [Yong]. C = Shang; E [Yong]. next = head [from], head [from] = Yong ++; // Add its return edge e [Yong] at the same time. U = To, E [Yong]. V = from, E [Yong]. f = 0, E [Yong]. B = Xia, E [Yong]. C = Shang; E [Yong]. next = head [to], head [to] = Yong ++;} void build () // construct the necessary arc // I; for (I = 0; I <= T; ++ I) {If (in [I]> 0) Adde (SS, I, 0, in [I]); else {Adde (I, TT, 0,-in [I]); sum + = (-in [I]) ;}} int SAP (INT S, int T) // non-recursive sap {int I, j, FF = 0, V, cur [1400], d [1400], num [1400], low [1400], pre [1400]; memset (low, 0, sizeof (low); memset (D, 0, sizeof (d); memset (Num, 0, sizeof (Num); for (I = 0; I <= T; ++ I) cur [I] = head [I]; low [I = s] = inf; num [s] = t + 1; while (d [s] <t + 1) {for (j = cur [I]; J! =-1; j = E [J]. next) if (E [J]. f> 0 & D [I] = d [V = E [J]. v] + 1) break; cur [I] = J; If (j> = 0) {pre [v] = J; low [v] = E [J]. f; If (low [v]> low [I]) low [v] = low [I]; I = V; if (I = T) {(; i! = S; I = E [pre [I]. u) {e [pre [I]. f-= low [T]; E [pre [I] ^ 1]. F + = low [T];} FF + = low [T]; memset (low, 0, sizeof (low); low [s] = inf ;}} else {If (-- num [d [I] = 0) break; d [I] = t + 1; cur [I] = head [I]; for (j = head [I]; J! =-1; j = E [J]. next) if (E [J]. f> 0 & D [I]> d [E [J]. v] + 1) d [I] = d [E [J]. v] + 1; num [d [I] ++; if (I! = S) I = E [pre [I]. U ;}} return ff;} int d [1400], num [1400]; int min (int A, int B) {return a <B? A: B;} int sap_gap (int u, int F, int S, int t) // recursive sap {If (u = T) return F; int I, V, mind = T, last = F, cost; for (I = head [u]; I! =-1; I = E [I]. next) {v = E [I]. v; int flow = E [I]. f; If (flow> 0) // when writing a reference template, the flow is written as f {If (d [u] = d [v] + 1) {cost = sap_gap (v, min (last, flow), S, T); e [I]. f-= cost; E [I ^ 1]. F + = cost; last-= cost; If (d [s]> = t + 1) return F-last; If (last = 0) break ;} if (d [v] <mind) Mind = d [v] ;}} if (last = f) {-- num [d [u]; if (Num [d [u] = 0) d [s] = t + 1; d [u] = mind + 1; ++ num [d [u];} return F-last;} int max_f (int s, int t) // call recursive sap {int f = 0; memset (D, 0, sizeof (d); memset (Num, 0, size Of (Num); For (Num [s] = t + 1; d [s] <t + 1;) F + = sap_gap (S, INF, S, t); Return F;} int main () {int I, A, C, B, Ng, most, sun; while (scanf ("% d ", & N, & M )! = EOF) {INI (); for (I = 0; I <m; I ++) {scanf ("% d", & A); Adde (I, T, a, INF); in [T] + = A; in [I]-= A;} For (; I <m + n; ++ I) {scanf ("% d", & ng, & most); Adde (S, I, 0, most); While (NG --) {scanf ("% d", & A, & B, & C); indexx [ind ++] = Yong; Adde (I, A, B, c); in [a] + = B; in [I]-= B ;}} Adde (t, s, 0, INF); Build (); // sun = SAP (S, TT); // non-recursive sapsun = max_f (SS, TT); // recursive sapif (Sun! = Sum) {printf ("-1 \ n");} else {// SAP (S, T); // non-recursive sapmax_f (S, T ); // recursive sapb = 0; for (I = 0; I <ind; ++ I) {A = indexx [I]; B ++ = E [A]. c-e [A]. f;} printf ("% d \ n", B); for (I = 0; I <ind; ++ I) {A = indexx [I]; printf ("% d \ n", E [A]. c-e [A]. f) ;}} printf ("\ n") ;}return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.