Alice's chance

Source: Internet
Author: User

Question Link

    • Question:
      Alice wants to make a movie. There are n movies. It is required that Alice's I-th movie can only be made for a fixed number of days every week, and can only be shot in the first wi Week, and it takes di to finish filming. Ask Alice if she can finish all the movies.
      The first row represents the number of groups of data. For each group of data, the first row represents n movies. The next two to n + 1 rows represent one movie, and each row contains 9 numbers, the first 7 digits, 1 representing the beat, 0 representing the no beat, 8th representing a few days, and 9th representing a few weeks
    • Analysis:
      For each movie, di-X [0]-X [1]-... -X [T] = 0. Di indicates the number of days required for the movie. X [] indicates whether the day corresponding to the movie is selected (with a value of zero or one)
      For each day, Y [0]-y [1]-... -Y [T] <= indicates that at most one movie is made on the day, and Y [] indicates whether the corresponding movie is shot on the day (with a value of 0 or 1)
Struct edge {int from, to, Cap, flow; bool operator <(const edge & RHs) const {return from <RHS. from | (from = RHS. from & to <RHS. to) ;}}; const int maxv = 500; struct ISAP {int n, m, S, T; vector <edge> edges; vector <int> G [maxv]; // In the adjacent table, G [I] [J] indicates the number of the J edge of node I in the E array bool vis [maxv]; // use int d [maxv] For BFS; // The distance from the start point to I: int cur [maxv]; // The Current Arc pointer int P [maxv]; // The Last arc int num [maxv] On the augmented path; // void add Edge (int from, int to, int cap) {edges. push_back (edge) {from, to, Cap, 0}); edges. push_back (edge) {to, from, 0, 0}); M = edges. size (); G [from]. push_back (m-2); G [to]. push_back m-1);} bool BFS () {memset (VIS, 0, sizeof (VIS); queue <int> q; q. push (t); vis [T] = 1; d [T] = 0; while (! Q. empty () {int x = Q. front (); q. pop (); rep (I, G [X]. size () {edge & E = edges [G [x] [I] ^ 1]; If (! Vis [E. from] & E. cap> E. flow) {vis [E. from] = 1; d [E. from] = d [x] + 1; q. push (E. from) ;}}return vis [s];} void clearall (int n) {This-> N = N; rep (I, n) g [I]. clear (); edges. clear ();} void clearflow () {rep (I, edges. size () edges [I]. flow = 0;} int augment () {int x = T, A = inf; while (X! = S) {edge & E = edges [p [x]; A = min (A, E. cap-e.flow); X = edges [p [X]. from;} X = T; while (X! = S) {edges [p [x]. flow + = A; edges [p [x] ^ 1]. flow-= A; X = edges [p [x]. from;} return a;} int maxflow (int s, int T, int need) {This-> S = s; this-> T = T; int flow = 0; BFS (); memset (Num, 0, sizeof (Num); rep (I, n) num [d [I] ++; int x = s; memset (cur, 0, sizeof (cur); While (d [s] <n) {If (x = T) {flow ++ = augment (); if (flow> = need) return flow; X = s;} int OK = 0; FF (I, cur [X], G [X]. size () {EDG E & E = edges [G [x] [I]; If (E. cap> E. flow & D [x] = d [E. to] + 1) // advance {OK = 1; p [E. to] = G [x] [I]; cur [x] = I; // note that X = E. to; break ;}} if (! OK) // retreat {int M = n-1; // The initial value must be Rep (I, G [X]. size () {edge & E = edges [G [x] [I]; If (E. cap> E. flow) M = min (M, d [E. to]);} If (-- num [d [x] = 0) break; num [d [x] = m + 1] ++; cur [x] = 0; // note that if (X! = S) x = edges [p [x]. from ;}} return flow;} vector <int> mincut () // call this after maxflow {BFS (); vector <int> ans; rep (I, edges. size () {edge & E = edges [I]; If (! Vis [E. from] & Vis [E. to] & E. cap> 0) ans. push_back (I);} return ans;} void reduce () {rep (I, edges. size () edges [I]. cap-= edges [I]. flow;} void print () {printf ("Graph: \ n"); rep (I, edges. size () printf ("% d-> % d, % d, % d \ n", edges [I]. from, edges [I]. to, edges [I]. cap, edges [I]. flow) ;}} MF; int day [18]; int main () {int t, n, D, W; RI (t); Fe (Kase, 1, t) {int sum = 0; RI (n); Mf. clearall (n + 7*50 + 2 ); Int S = 0, T = N + 7*50 + 1, max =-1; Fe (I, 1, n) {Fe (J, 1, 7) RI (day [J]); RII (D, W); sum + = D; max = max (max, W); Mf. addedge (S, I, d); Fe (J, 1, 7) if (day [J]) {rep (K, W) MF. addedge (I, n + K * 7 + J, 1) ;}} Fe (I, 1, 7) rep (J, max) MF. addedge (n + J * 7 + I, T, 1); printf ("% s \ n", MF. maxflow (S, T, INF) = sum? "Yes": "no");} 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.