Alice's Chance
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 4521 | Accepted: 1907 |
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,
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.
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
题意:Alice要参加n部电影的拍摄,每部电影都要在一个星期里指定的日子Fi拍摄,要在Wi个星期之内拍完,Alice必须在每部电影中拍摄Di天。Alice一天只能拍一部电影。问Alice是否能拍完这n部电影。
思路:最大流。源点连向每部电影,容量为Di;每部电影连向该电影能拍摄的日子,容量为1;然后每天看成一个结点,连向汇点,容量为1。如果最大流==所有电影拍摄的天数总和,则输出Yes。
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <queue> #include <stack> #include <vector> #include <map> #include <cmath> #include <cstdlib> #define L(rt) (rt<<1) #define R(rt) (rt<<1|1) #define ll long long #define eps 1e-6 using namespace std; const int INF=1000000000; const int maxn=500; struct Edge{ int u,v,cap,flow,next; }et[maxn*maxn]; int cnt[maxn],low[maxn],dis[maxn],cur[maxn],pre[maxn],eh[maxn]; int fix[22][10],w[22]; int s,t,n,num; void init(){ memset(eh,-1,sizeof(eh)); s=num=0; } void add(int u,int v,int cap,int flow){ Edge e={u,v,cap,flow,eh[u]}; et[num]=e; eh[u]=num++; } void addedge(int u,int v,int cap){ add(u,v,cap,0); add(v,u,0,0); } int isap(int s,int t,int n){ int u,v,now,flow=0; memset(cnt,0,sizeof(cnt)); memset(low,0,sizeof(low)); memset(dis,0,sizeof(dis)); for(u=0;u<=n;u++) cur[u]=eh[u]; low[s]=INF,cnt[0]=n,u=s; while(dis[s]<n) { for(now=cur[u];now!=-1;now=et[now].next) if(et[now].cap-et[now].flow&&dis[u]==dis[v=et[now].v]+1) break; if(now!=-1) { cur[u]=pre[v]=now; low[v]=min(et[now].cap-et[now].flow,low[u]); u=v; if(u==t) { for(;u!=s;u=et[pre[u]].u) { et[pre[u]].flow+=low[t]; et[pre[u]^1].flow-=low[t]; } flow+=low[t]; low[s]=INF; } } else { if(--cnt[dis[u]]==0) break; dis[u]=n,cur[u]=eh[u]; for(now=eh[u];now!=-1;now=et[now].next) if(et[now].cap-et[now].flow&&dis[u]>dis[et[now].v]+1) dis[u]=dis[et[now].v]+1; cnt[dis[u]]++; if(u!=s) u=et[pre[u]].u; } } return flow; } int main() { int ca,d; scanf("%d",&ca); while(ca--) { scanf("%d",&n); init(); int mw=0,sum=0; for(int i=1;i<=n;i++) { for(int j=1;j<=7;j++) scanf("%d",&fix[i][j]); scanf("%d%d",&d,&w[i]); w[i]*=7; mw=max(mw,w[i]); sum+=d; addedge(s,i,d); } t=mw+n+1; for(int i=1;i<=mw;i++) addedge(i+n,t,1); for(int i=1;i<=n;i++) { for(int j=1;j<=7;j++) if(fix[i][j]) { int k=j; while(k<=w[i]) { addedge(i,k+n,1); k+=7; } } } if(sum==isap(s,t,t+1)) printf("Yes\n"); else printf("No\n"); } return 0; }