Alice's chance
Time limit:1000 ms |
|
Memory limit:10000 K |
Total submissions:5280 |
|
Accepted:2171 |
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,
- It will be made only on some fixed days in a week, I. e., Alice can only work for the film on these days;
- Alice shoshould work for it at least for specified number of days;
- 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
220 1 0 1 0 1 0 9 30 1 1 1 0 0 0 6 420 1 0 1 0 1 0 9 40 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 Satweek1 film1 film2 film1 film1week2 film1 film2 film1 film1week3 film1 film2 film1 film1week4 film2 film2 film2
Idea: 1. Create a super source point. The traffic from this point to each movie (taking the movie as a point) is the number of days required;
2. Each day is regarded as a point. If a movie can be played on this day, the video will be connected to an edge and the edge weight will be 1. At the same time, connect this point to the sink point, and the edge right is also 1.
3. Find the maximum stream from the Super source point to the sink point. If the maximum stream is equal to the total number of days, the output is yes !!
# Include "stdio. H "# include" string. H "# include" queue "using namespace STD; # define n 505 const int INF = 10000000; int G [N] [N]; int pre [N], mark [N]; int min (int A, int B) {return a <B? A: B;} int EK (int n) {int I, U, ANS = 0; while (1) {queue <int> q; q. push (0); memset (mark, 0, sizeof (Mark); memset (PRE,-1, sizeof (pre); Mark [0] = 1; while (! Q. Empty () {u = Q. Front (); q. Pop (); for (I = 0; I <= N; I ++) {If (! Mark [I] & G [u] [I]) {mark [I] = 1; Pre [I] = u; q. push (I) ;}}if (pre [N] =-1) break; int d = inf; for (I = N; I! = 0; I = pre [I]) {d = min (d, g [pre [I] [I]);} for (I = N; I! = 0; I = pre [I]) {G [pre [I] [I]-= D; G [I] [pre [I] + = D ;} ans + = D;} // printf ("% d \ n", ANS); Return ans;} int main () {int I, j, k, n, T, u, sum, T; int A [22] [8], d [22], W [22]; scanf ("% d", & T ); while (t --) {scanf ("% d", & N); memset (G, 0, sizeof (g); sum = T = 0; // sink point for (I = 1; I <= N; I ++) {for (j = 0; j <7; j ++) {scanf ("% d", & A [I] [J]);} scanf ("% d", & D [I], & W [I]); If (W [I]> T) t = W [I];} t = T * 7 + n + 1; // subscript of the sink point for (I = 1; I <= N; I ++) {G [0] [I] = d [I]; // edge weight from super source to general source (days required for a movie) for (k = 0; k <W [I]; k ++) {for (j = 0; j <7; j ++) {if (a [I] [J]) {u = K * 7 + J + n + 1; G [I] [u] = G [u] [T] = 1 ;}} sum + = d [I] ;}if (sum = EK (t )) printf ("Yes \ n"); else printf ("NO \ n");} return 0 ;}