POJ 1698 Alice's Chance

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,

  1. it will be made ONLY on some fixed days in a week, i.e., Alice can only work for the film on these days;
  2. Alice should work for it at least for specified number of days;
  3. 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想要演尽量多的电影,她每天只能演一场电影,每场电影要演d天,要在w周内演完,每周只有给定的天能演

思路:源点和每天各连一条边,边权为1,每天和能匹配的电影连边,边权为1,每个电影和汇点连边,边权为d,然后求最大流,如果最大流和演的电影总天数相同,输出Yes,否则输出No

#include<stdio.h>
#include<queue>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
#define inf 1e9
#define maxn 1005
#define maxm 105000
struct Edge
{
    int to,next,flow;
} e[maxm];
int cur[maxn],head[maxn],cnt,d[maxn],n,d1,w,s,t;
void add(int u,int v,int flow)
{
    e[cnt].to=v;
    e[cnt].flow=flow;
    e[cnt].next=head[u];
    head[u]=cnt++;
}
void init()
{
    s=0;
    t=350+20+1;
    cnt=0;
    memset(head,-1,sizeof(head));
}
int dfs(int now,int t,int lim)
{
    if(!lim||now==t)
        return lim;
    int flow=0,f=0;
    for(int i=cur[now];i!=-1;i=e[i].next)
    {
        int v=e[i].to;
        if(d[v]==d[now]+1&&(f=dfs(v,t,min(lim,e[i].flow))))
        {
            flow+=f;
            lim-=f;
            e[i].flow-=f;
            e[i^1].flow+=f;
            if(!lim) break;
        }
    }
    return flow;
}
int bfs(int s,int t)
{
    for(int i=0;i<=t;i++)
    {
        cur[i]=head[i];
        d[i]=0x7f7f7f7f;
    }
    queue<int>q;
    d[s]=0;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
       //if(head[u]!=-1) printf("u=%d\n",u);
        for(int i=head[u];i!=-1;i=e[i].next)
        {
            int v=e[i].to;

            if(d[v]>inf&&e[i].flow)
            {//printf(" v=%d w=%d\n\n",v,e[i].flow);
                d[v]=d[u]+1;
                q.push(v);
            }
        }
    }
    if(d[t]<inf) return 1;
    else return 0;
}
int slove()
{
    int ans=0;
    while(bfs(s,t))
    {
        ans+=dfs(s,t,inf);
    }
    return ans;
}
int main()
{
    int t1;
    scanf("%d",&t1);
    while(t1--)
    {
        init();
        for(int i=1; i<=350; i++)
        {
            add(s,i,1);
            add(i,s,0);
        }
        int a[10]= {0},s1=0;
        scanf("%d",&n);
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=7; j++)
            {
                scanf("%d",&a[j]);
            }
            scanf("%d %d",&d1,&w);
            s1+=d1;
            add(350+i,t,d1);
            add(t,350+i,0);
            for(int j=1; j<=w; j++)
            {
                for(int k=1;k<=7;k++)
                {
                    if(a[k]==1)
                    {
                        add((j-1)*7+k,350+i,1);add(350+i,(j-1)*7+k,0);
                    }
                }
            }
        }
        //printf("s=%d t=%d\n",s,t);
        /*for(int i=s;i<=t;i++)
        {
            printf("head[%d]=%d\n",i,head[i]);
            for(int j=head[i];j!=-1;j=e[j].next)
            {
                int v=e[j].to;
                printf("%d %d %d\n",i,v,e[j].flow);
            }
        }*/
        int ans=slove();
        //printf("ans=%d\n",ans);
        if(ans==s1) printf("Yes\n");
        else printf("No\n");
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值