(Max stream) poj 1698 Alice's chance

Source: Internet
Author: User
Alice's chance
Time limit:1000 ms   Memory limit:10000 K
Total submissions:1613   Accepted:740

Description

Alice, a charming girl, have been dreaming of being a movie star for long. her chances will come now, for several filmmaking companies invite her to play the chief role in their new films. unfortunately, all these companies will start making the films at the same time, and the greedy Alice doesn't want to miss any of them !! You are asked to tell her whether she can act in all the films.

As for a film,

  1. It will be made only on some fixed days in a week, I. e., Alice can only work for the film on these days;
  2. Alice shoshould work for it at least for specified number of days;
  3. The film must be finished before a prearranged deadline.

For example, assuming a film can be made only on Monday, Wednesday and Saturday; Alice shocould work for the film at least for 4 days; and it must be finished within 3 weeks. in this case she can work for the film on Monday of the first week, on Monday and Saturday of the second week, and on Monday of the third week.

Notice that on a single day Alice can work on at most one film.

Input

The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. then T cases follow. each test case begins with a single line containing an integer N (1 <= n <= 20), the number of films. each of the following n lines is in the form of "F1 F2 F3 F4 F5 F6 F7 d w ". FI (1 <= I <= 7) is 1 or 0, representing whether the film can be made on the I-th day in a week (a week starts on Sunday ): 1 means that the film can be made on this day, while 0 means the opposite. both D (1 <= d <= 50) and W (1 <= W <= 50) are integers, and Alice shoshould go to the film for D days and the film must be finished in W weeks.

Output

For each test case print a single line, 'yes' if Alice can attend all the films, otherwise 'no '.

Sample Input

2
2
0 1 0 1 0 1 0 9 3
0 1 1 1 0 0 0 6 4
2
0 1 0 1 0 1 0 9 4
0 1 1 1 0 0 0 6 2

Sample output

YesNo

Hint

A proper schedule for the first test case:

date Sun Mon Tue Wed Thu Fri Sat
week1 film1 film2 film1 film1
week2 film1 film2 film1 film1
week3 film1 film2 film1 film1
week4 film2 film2 film2
/* Question: Someone is looking for Alice to make a movie. In order to become a star, Alice can seize all the opportunities and finish shooting all the movies at the Director's request. Can she ask?: A movie can only be shot within a specific day of its shooting cycle. You only need to construct a plot and then use the maximum stream to solve the problem. create origin s node 0, film node 1-max_film,: Then day node max_film-max_film * 7, sink t node max_week-max_film * 7 + 1.: S-> film [I] = film [I]. days, film [J]-> day [I] = 1, day [I]-> T = 1; the graph is constructed completely. code tips: Use BFs to find a gain path, and use next [I] to store those days. there is no need to repeat the judgment. source codeproblem: 1698 User: wawadimu memory: 1316 K time: 782 Ms language: C ++ result: accepted source code */# include <iostream> # include <queue> using namespace STD; # define maxn 22 # define Maxl 380 # define INF int_maxstruct node {int next [7]; // next [I] The next day that can work: int D, W; // days and weeks} f [maxn]; int CAP [maxl] [maxl], flow [maxl] [maxl]; int A [maxl], pre [maxl]; int maxflow (int s, int t) {int max = 0; while (1) {memset (A, 0, sizeof (a); A [s] = inf; queue <int> q; q. push (s); While (! Q. empty () // find the gain path {int u = Q. front (); q. pop (); For (int v = 0; v <= T; V ++) {If (! A [v] & Cap [u] [v]> flow [u] [v]) {pre [v] = u; A [v] = A [u]> CAP [u] [v]-flow [u] [v]? Cap [u] [v]-flow [u] [v]: A [u]; q. push (v) ;}}if (A [T] = 0) break; // No gain path for (INT I = T; I! = S; I = pre [I]) // reverse increase traffic {flow [pre [I] [I] + = A [T]; flow [I] [pre [I]-= A [T];} MAX + = A [T];} return Max;} int main () {// freopen ("1698.txt"," r ", stdin); int case, FN; int I, J, K, Max, sum; int S, T; int P, TMP; scanf ("% d", & case); While (case --) {memset (Cap, 0, sizeof (CAP); memset (flow, 0, sizeof (flow); max = sum = 0; scanf ("% d", & FN); for (I = 1; I <= FN; I ++) {k = 0; For (j = 1; j <= 7; j ++) {scanf ("% d", & TMP); If (TMP = 1) f [I]. next [k ++] = J; // record dates that can work} scanf ("% d", & F [I]. d, & F [I]. w); sum + = f [I]. d; CAP [0] [I] = f [I]. d; If (max <F [I]. w) max = f [I]. w; // record the maximum daily TMP = f [I]. w; For (P = 0; P <TMP; P ++) // set the capacity of each day that can work in the first W weeks to 1 {int KK = 7 * P + FN; For (j = 0; j <K; j ++) {int xx = KK + F [I]. next [J]; CAP [I] [XX] = 1 ;}}s = 0; t = 7 * MAX + FN + 1; for (j = FN + 1; j <t; j ++) // The capacity from date to sink is set to 1 {CAP [J] [T] = 1;} int ans = maxflow (0, T ); // find the maximum stream // cout <ans <Endl; If (ANS = sum) // you can complete printf ("yes") for all movies "); elseprintf ("no"); printf ("/N");} return 1 ;}

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.