Alice's Chance
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 7327 | Accepted: 2992 |
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
week4 film2 film2 film2
Source
题意:
有n部电影需要拍摄,每部电影在一个星期内只有固定的几天可以拍,一天只能拍一种电影,并被拍完这部电影需要d天,必须在w周之内拍完,问能否全部拍完这n部电影
输入t组数据
输入n
输入n行,每行9个数,前七个表示这部电影在一周之内那几点可以拍(1表示可以拍,0表示不能),后两个数d,w
代码:
//每部电影拆点,容量为拍摄需要的天数,每一天拆点容量为1(一天只能拍一个电影),然后把电影和天之间建边容量为1
//(w周共有7*w天),源点连向电影,天连向汇点,求最大流是否等于拍摄电影的总天数。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int maxn=;
const int inf=0x7fffffff;
struct Edge{
int from,to,cap,flow;
Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
struct Dinic{
int n,m,s,t;
vector<Edge>edges;
vector<int>g[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
void Init(int n){
this->n=n;
for(int i=;i<n;i++) g[i].clear();
edges.clear();
}
void Addedge(int from,int to,int cap){
edges.push_back(Edge(from,to,cap,));
edges.push_back(Edge(to,from,,));//反向弧
m=edges.size();
g[from].push_back(m-);
g[to].push_back(m-);
}
bool Bfs(){
memset(vis,,sizeof(vis));
queue<int>q;
q.push(s);
d[s]=;
vis[s]=;
while(!q.empty()){
int x=q.front();q.pop();
for(int i=;i<(int)g[x].size();i++){
Edge &e=edges[g[x][i]];
if(!vis[e.to]&&e.cap>e.flow){
vis[e.to]=;
d[e.to]=d[x]+;
q.push(e.to);
}
}
}
return vis[t];
}
int Dfs(int x,int a){
if(x==t||a==) return a;
int flow=,f;
for(int&i=cur[x];i<(int)g[x].size();i++){
Edge &e=edges[g[x][i]];
if(d[x]+==d[e.to]&&(f=Dfs(e.to,min(a,e.cap-e.flow)))>){
e.flow+=f;
edges[g[x][i]^].flow-=f;
flow+=f;
a-=f;
if(a==) break;
}
}
return flow;
}
int Maxflow(int s,int t){
this->s=s;this->t=t;
int flow=;
while(Bfs()){
memset(cur,,sizeof(cur));
flow+=Dfs(s,inf);
}
return flow;
}
}dc;
int main()
{
int t,n,a[];
scanf("%d",&t);
while(t--){
scanf("%d",&n);
dc.Init();
int sum=;
for(int i=;i<=n;i++){
for(int j=;j<=;j++)
scanf("%d",&a[j]);
sum+=a[];
dc.Addedge(i,i+n,a[]);
for(int j=;j<=;j++){
if(a[j]==) continue;
for(int k=;k<a[];k++)
dc.Addedge(i+n,j+k*+,);
}
dc.Addedge(,i,inf);
}
for(int i=;i<=;i++){
dc.Addedge(i,i+,);
dc.Addedge(i+,,inf);
}
if(dc.Maxflow(,)==sum) printf("Yes\n");
else printf("No\n");
}
return ;
}