天天看點

poj1698 Alice's Chance

​​蒟蒻的blog​​​

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 should 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 should 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 should 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

Yes

No

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

#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 440
#define inf 0x3f3f3f3f
using namespace std;
inline char gc(){
    static char now[1<<16],*S,*T;
    if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0;int f=1;char ch=gc();
    while(ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=gc();}
    while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=gc();}
    return x*f;
}
struct node{
    int y,z,next;
}data[N*N];
int num=1,T,n,TT,h[N],level[N],f[10];
inline void insert1(int x,int y,int z){
    data[++num].y=y;data[num].z=z;data[num].next=h[x];h[x]=num;
    data[++num].y=x;data[num].z=0;data[num].next=h[y];h[y]=num;
}
inline bool bfs(){
    memset(level,0,sizeof(level));level[0]=1;queue<int>q;q.push(0);
    while(!q.empty()){
        int x=q.front();q.pop();
        for (int i=h[x];i;i=data[i].next){
            int y=data[i].y,z=data[i].z;
            if (level[y]||!z) continue;level[y]=level[x]+1;q.push(y);if (y==T) return 1;
        }
    } return 0;
}
inline int dfs(int x,int s){
    if (x==T) return s;int ss=s;
    for (int i=h[x];i;i=data[i].next){
        int y=data[i].y,z=data[i].z;
        if (level[x]+1==level[y]&&z){
            int xx=dfs(y,min(s,z));if (!xx) level[y]=0;
            s-=xx;data[i].z-=xx;data[i^1].z+=xx;if (!s) return ss;
        }
    }return ss-s;
}
int main(){
    freopen("poj1698.in","r",stdin);
    TT=read();
    while(TT--){
        n=read();memset(h,0,sizeof(h));num=1;T=371;int sum=0,ans=0;
        for (int i=1;i<=n;++i){
            for (int j=1;j<=7;++j) f[j]=read();int d=read(),w=read();sum+=d;
            for (int k=0;k<w;++k)
                for (int j=1;j<=7;++j)
                    if (f[j]) insert1(350+i,k*7+j,1);
            insert1(0,350+i,d);
        }
        for (int i=1;i<=350;++i) insert1(i,T,1);
        while(bfs()) ans+=dfs(0,inf);if (ans==sum) printf("Yes\n");else printf("No\n");
    }
    return 0;
}