Alice's Chance
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 4691 | Accepted: 1961 |
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
Source
题意是说Alice要拍n部电影,每部电影可以在一个星期的某几天拍,而且给你一部电影需要多长时间才能拍完以及有多少个星期去拍。
既可以是最大流算法,也可以是二分匹配算法。
最大流建图:
将n部电影与超级源点相连,流量为天数。
将每部电影与可以在哪些天拍连接起来,流量为inf.
将所有电影可以允许的拍摄天数与超级汇点连接起来,流量为1.
这是用EK算法求的最大流:
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
//#define min(a,b) a<b?a:b
#define M 377
#define inf 0x3f3f3f
using namespace std;
int g[M][M],flow[M][M],pre[M],a[M],work[25][10];
int EK(int s,int t)
{
int sum=0;
queue<int>q;
memset(flow,0,sizeof(flow));
while(true)
{
memset(a,0,sizeof(a));
a[s]=inf;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=0; i<=t; i++)
if(!a[i]&&g[u][i]>flow[u][i])
{
pre[i]=u;
q.push(i);
a[i]=min(a[u],g[u][i]-flow[u][i]);
}
}
if(!a[t])break;
for(int i=t; i!=s; i=pre[i])
{
flow[pre[i]][i]+=a[t];
flow[i][pre[i]]-=a[t];
}
sum+=a[t];
}
return sum;
}
int main()
{
int tt;
scanf("%d",&tt);
while(tt--)
{
int n,day,week,sum=0,s=0,e=371;
memset(g,0,sizeof(g));
memset(pre,0,sizeof(pre));
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
for(int j=1; j<=7; j++)
scanf("%d",&work[i][j]);
scanf("%d%d",&day,&week);
g[s][i]=day;
sum+=day;
for(int j=1; j<=7; j++)
if(work[i][j])
{
for(int k=0; k<week; k++)
{
int mm=k*7+n+j;
g[i][mm]=g[mm][e]=1;
}
}
}
int ans=EK(s,e);
if(ans==sum)printf("Yes\n");
else printf("No\n");
}
return 0;
}
这是用SAP算法求的最大流:
//SAP算法
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define inf 9999999
#define M 1007
#define MIN(a,b) a>b?b:a;
using namespace std;
struct E
{
int v,w,next;
}edg[500000];
int dis[2000],gap[2000],list[2000],nodes;
int sourse,sink,nn;
void addedge(int u,int v,int w)
{
edg[nodes].v=v;
edg[nodes].w=w;
edg[nodes].next=list[u];
list[u]=nodes++;
edg[nodes].v=u;
edg[nodes].w=0;
edg[nodes].next=list[v];
list[v]=nodes++;
}
int dfs(int src,int aug)
{
if(src==sink)return aug;
int left=aug,mindis=nn;
for(int j=list[src];j!=-1;j=edg[j].next)
{
int v=edg[j].v;
if(edg[j].w)
{
if(dis[v]+1==dis[src])
{
int minn=MIN(left,edg[j].w);
minn=dfs(v,minn);
edg[j].w-=minn;
edg[j^1].w+=minn;
left-=minn;
if(dis[sourse]>=nn)return aug-left;
if(left==0)break;
}
if(dis[v]<mindis)
mindis=dis[v];
}
}
if(left==aug)
{
if(!(--gap[dis[src]]))dis[sourse]=nn;
dis[src]=mindis+1;
gap[dis[src]]++;
}
return aug-left;
}
int sap(int s,int e)
{
int ans=0;
nn=e+1;
memset(dis,0,sizeof(dis));
memset(gap,0,sizeof(gap));
gap[0]=nn;
sourse=s;
sink=e;
while(dis[sourse]<nn)
ans+=dfs(sourse,inf);
return ans;
}
int main()
{
int t;
int ss[M];
scanf("%d",&t);
while(t--)
{
int n,s=0,sum=0,ansss=-1,day,week;
memset(list,-1,sizeof(list));
nodes=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
for(int j=0;j<7;j++)
scanf("%d",&ss[j]);
scanf("%d%d",&day,&week);
addedge(s,i,day);
for(int j=0;j<week;j++)
for(int k=0;k<7;k++)
if(ss[k])addedge(i,j*7+n+k+1,inf);
ansss=max(ansss,week);
sum+=day;
}
int t=n+ansss*7+1;
for(int i=0;i<ansss;i++)
for(int j=1;j<=7;j++)
addedge(n+i*7+j,t,1);
int anss=sap(s,t);
//printf("%d %d\n",sum,anss);
if(anss==sum)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
这是用二分匹配求的最大流:
#include<stdio.h>
#include<algorithm>
#include<string.h>
#define M 1007
#define MM 507
#define inf 0x3f3f3f
using namespace std;
int g[M][MM],work[10],link[MM];
bool vis[MM];
int nx,ny;
bool find(int i)
{
for(int j=1;j<=ny;j++)
if(g[i][j]&&!vis[j])
{
vis[j]=true;
if(link[j]==-1||find(link[j]))
{
link[j]=i;
return true;
}
}
return false;
}
int main()
{
int tt;
scanf("%d",&tt);
while(tt--)
{
int n;
nx=0,ny=0;
memset(g,0,sizeof(g));
scanf("%d",&n);
while(n--)
{
for(int i=0; i<9; i++)
scanf("%d",&work[i]);
ny=max(ny,work[8]);
for(int i=0; i<work[7]; i++)
{
++nx;
for(int j=0; j<7; j++)
if(work[j])
for(int k=0; k<work[8]; k++)
g[nx][k*7+j+1]=1;
}
}
ny*=7;
memset(link,-1,sizeof(link));
int flag=1;
for(int i=1; i<=nx; i++)
{
memset(vis,false,sizeof(vis));
if(!find(i)){flag=0;break;}
}
if(flag)printf("Yes\n");
else printf("No\n");
}
return 0;
}