Zoj problem set-3229 shoot the bullet [Upper and Lower Bound network flow + traffic output]

Source: Internet
Author: User

Title: zoj problem set-3229 shoot the bullet


Category: source, sink, and upstream/downstream network streams


Assume that you have n days and M girls. Then, you can take a part of girls every day. Each girls has a lower limit, that is, you must take at least so many pictures. Then K Girls take pictures every day, A photographer can take up to num photos. Then, K female can take up to a lower limit of the number of photos each day. Then, they can ask if you have the largest solution to take photos of girls with such conditions, then, output the number of photos taken by the girl every day according to the input.


We recommend that you do this first: here

Analysis: First, it allows you to determine whether conditions can be met.

It is easy to create a graph based on the conditions given by the question:

S ---> days,

Days ----> girls

Girls ----> T

(All traffic is the current upper bound minus the lower bound)


In this case, the graph is created successfully, but this cannot be determined. We are adding an edge t -----> S, and the traffic is infinite.

In this case, the source image is programmed with a cyclic diagram, that is, the previously recommended question. Follow these steps to create a diagram, and then find the maximum stream to see if the stream is full.

Then, if the stream is full, you can consider the second problem. What is the maximum number of pictures taken by girls every day?

In this way, we can delete the T ----> S edge and find the maximum stream from S to t once.

Then output the traffic according to the question requirement.



PS: it hurt my English, I didn't understand my questions, and I made countless mistakes. I finally understood my questions.


# Include <cstdio> # include <cstring> # include <string> # include <iostream> # include <algorithm> # include <vector> # include <map> # include <queue> # define del (, b) memset (a, B, sizeof (A) using namespace STD; const int INF = 0x3f3f3f3f; const int n = 1550; struct node {int from, to, cap, flow ;}; vector <int> V [N]; vector <node> E; int vis [N]; // build a layered graph int cur [N]; void add_node (int from, int to, int cap) {e. push_back (node) {fr Om, to, Cap, 0}); E. push_back (node) {to, from, 0, 0}); int TMP = E. size (); V [from]. push_back (tmp-2); V [to]. push_back (tmp-1);} bool BFS (int s, int t) {del (VIS,-1); queue <int> q; q. push (s); vis [s] = 0; while (! Q. empty () {int x = Q. front (); q. pop (); For (INT I = 0; I <V [X]. size (); I ++) {node TMP = E [V [x] [I]; If (vis [TMP. to] <0 & TMP. cap> TMP. flow) // The second condition ensures {vis [TMP. to] = vis [x] + 1; q. push (TMP. to) ;}}} if (vis [T]> 0) return true; return false;} int DFS (int o, int F, int T) {If (O = T | f = 0) // returns F; int A = 0, ANS = 0; For (Int & I = cur [O]; I <V [O]. size (); I ++) // note the preceding '&', which is an important optimization {node & TMP = E [V [O] [I]; if (vis [TMP. to] = (Vis [O] + 1) & (A = DFS (TMP. to, min (F, TMP. cap-tmp.flow), t)> 0) {TMP. flow + = A; E [V [O] [I] ^ 1]. flow-= A; // save graph mode ans + = A; F-= A; If (F = 0) // note break optimization;} return ans; // optimization} int dinci (int s, int t) {int ans = 0; while (BFS (S, T) {del (cur, 0 ); int TM = DFS (S, INF, T); ans + = TM;} return ans;} void mp_clear (int n) {for (INT I = 0; I <= N; I ++) V [I]. clear (); E. clear ();} int come [N], to [N]; int flow [400] [N]; vector <pair <int, int>> PP; int main () {// freopen ("input.txt", "r", stdin); int n, m; while (~ Scanf ("% d", & N, & M) {del (come, 0); del (to, 0); del (flow, 0 ); int S = N + M, t = S + 1; for (INT I = 0; I <m; I ++) {int X; scanf ("% d ", & X); come [n + I] + = x; To [T] + = x; add_node (n + I, T, INF );} for (INT I = 0; I <n; I ++) {int cas, num; scanf ("% d", & CAS, & num ); add_node (S, I, num); For (Int J = 0; j <CAS; j ++) {int x, y, z; scanf ("% d", & X, & Y, & Z); pair <int, int> TS (I, x); pp. push_back (TS); flow [I] [x] + = y; come [I] + = y; To [n + x] + = y; add_nod E (I, n + X, Z-y); // Sb} add_node (t, s, INF); // int Ss = t + 1, tt = SS + 1; int COUNT = 0; For (INT I = 0; I <= T; I ++) {int CHA = come [I]-To [I]; If (CHA <0) {count + =-cha; add_node (SS, I,-cha );} if (CHA> 0) add_node (I, TT, CHA) ;}int ans = dinci (SS, TT); If (ANS! = Count) puts ("-1"); else {// add_node (t, s, 0); printf ("% d \ n", dinci (S, t); // sb for (INT I = 0; I <E. size (); I ++) {node F = E [I]; if (I % 2 = 0 & F. from <n) {flow [F. from] [F. to-N] + = f. flow; // printf ("% d \ n", F. from, F. to, F. cap, F. flow) ;}}for (INT I = 0; I <pp. size (); I ++) {printf ("% d \ n", flow [PP [I]. first] [PP [I]. second]) ;}} printf ("\ n"); mp_clear (TT); pp. clear () ;}return 0 ;}



Zoj problem set-3229 shoot the bullet [Upper and Lower Bound network flow + traffic output]

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.