POJ1698

52 篇文章 0 订阅
Alice's Chance
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 3704 Accepted: 1588

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

Source

解题思路:要确定能否完成所有的film,那么就需要完成所有的天数,将参加演出的天数看成流量。
1.从源点S向第i个film连接一天容量为完成这个film需要的天数,及预设定的最大流量。
2.从每个film向能工作的日期连接一条容量为1的边。
3.从每个日期向汇点T连接一条容量为1的边。
但事实上,刚开始我建好图后,总是TLE,改了N次,才发现是在上述2.的过程中细节没有优化的问题,蛋碎了.....
Code:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int mm=88888;
const int nn=444;
const int oo=1e9;
int head[nn],que[nn],lv[nn];
int to[mm],flow[mm],next[mm];
int start,end,node,edge;
int week[51][7],mw[51],md[51];
void initial(int n,int s,int t)
{
    node=n,start=s,end=t,edge=0;
    for(int i=0; i<node ; i++)head[i]=-1;
}
void addedge(int u,int v,int c)
{
    to[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++;
    to[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++;
}
bool bfs()
{
    int iq=0;
    for(int i=0; i<node; i++) lv[i]=0;
    que[iq++]=start;
    lv[start]=1;
    for(int i=0; i<iq; i++)
    {
        int top=que[i];
        for(int k=head[top]; k!=-1; k=next[k])
        {
            if(flow[k]&&!lv[to[k]])
            {
                que[iq++]=to[k];
                lv[to[k]]=lv[top]+1;
                if(to[k]==end) return 1;
            }
        }
    }
    return 0;
}
int dfs(int now,int maxf)
{
    if(now==end) return maxf;
    int ret=0,f;
    for(int k=head[now]; k!=-1; k=next[k])
    {
        if(flow[k]&&lv[to[k]]==lv[now]+1)
        {
            f=dfs(to[k],min(maxf-ret,flow[k]));
            flow[k]-=f;
            flow[k^1]+=f;
            ret+=f;
            if(ret==maxf) return ret;
        }
    }
    return ret;
}
int dinic()
{
    int ans=0;
    while(bfs()) ans+=dfs(start,oo);
    return ans;
}
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        int maxday=0,maxw=0;
        for(int i=1; i<=n; i++)
        {
            for(int j=0; j<7; j++)
                scanf("%d",&week[i][j]);
            scanf("%d%d",&md[i],&mw[i]);
            maxday+=md[i];
            maxw=max(mw[i],maxw);
        }
        initial(2+n+maxw*7,0,1+n+maxw*7);
        //cout<<"maxweek="<<maxw<<endl;
        //cout<<"maxday="<<maxday<<endl;
        for(int i=1; i<=n; i++)              //源点到film连接一条容量为出演天数的边
            addedge(start,i,md[i]);
        for(int i=1; i<=maxw*7; i++)         //每天都到汇点连接一条容量为1的边
            addedge(i+n,end,1);
        for(int i=1; i<=n; i++)                          //for(int i=1;i<=n;i++)
            for(int j=0; j<7; j++)                       // for(int k=0;k<mw[i];k++)
                if(week[i][j])                           //for(int j=0;j<7;j++)
                {                                        //if(week[i][j])
                    int k=0;                             //addedge(i,k*7+n+j+1,1);
                    while(k<mw[i])                       //这段代码使我TLE好几次.....
                    {
                        addedge(i,k*7+n+j+1,1);
                        k++;
                    }
                }
        //printf("%d\n",dinic());
        if(dinic()==maxday) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值