C - Alice's Chance POJ - 1698(最大流模板+构图)

C - Alice's Chance

 POJ - 1698 

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
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
const int N=373;
const int mmax = 1e6+ 7;
int str[10],head[N],cur[N],dep[N];
struct node
{
    int v,w,next;
} gra[15000];
/*
这个gra结构体数组的长度的定义出错让我烦扰了2天,血的教训啊,
我还是忽略了数组定义的问题,一直估算着数组长度比2*371
大一些就行了,忽略了gra保存的是邻接表的边,边的数量的
边界值为(20+20*350+350)*2,这里定义数组长度为15000
是足够了构造的原点到电影结点最多建立20条正向边,电影
结点到日期结点最多有20*350条正向边,日期结点到构造的
汇点最多有350条正向边,有因为有反向边,所以在乘以2
*/
int num,sum,maxl;  
void ad(int u,int v,int w)
{
    gra[num].v=v;
    gra[num].next=head[u];
    gra[num].w=w;
    head[u]=num++;
    gra[num].next=head[v];
    gra[num].v=u;
    gra[num].w=0;
    head[v]=num++;
}
int bfs()
{
    queueq;
    memset(dep,0,sizeof(dep));
    q.push(0);
    dep[0]=1;
    while(!q.empty())
    {
        int t=q.front();
        q.pop();
        if(t==371)
            return 1;
        for(int i=head[t]; i!=-1; i=gra[i].next)
        {
            if(!dep[gra[i].v]&&gra[i].w>0)
            {
                dep[gra[i].v]=dep[t]+1;
                q.push(gra[i].v);
            }
        }
    }
    return 0;
}
int dfs(int now,int minl)
{
    if(now==371)
        return minl;
    for(int &i=cur[now]; i!=-1; i=gra[i].next)
    {
        if(dep[gra[i].v]==dep[now]+1&&gra[i].w>0)
        {
            int tminl=dfs(gra[i].v,min(gra[i].w,minl));
            if(tminl>0)
            {
                gra[i].w-=tminl;
                gra[i^1].w+=tminl;
                return tminl;
            }
        }
    }
    return 0;
}
int Dinic()   //最大流模板
{
    while(bfs())
    {
        for(int i=0; i<=371; i++)
            cur[i]=head[i];
        while(int flow=dfs(0,inf))
            maxl+=flow;
    }
    return maxl;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        maxl=0;
        sum=0;
        num=0;
        memset(head,-1,sizeof(head));
        int n;
        scanf("%d",&n);
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=7; j++)
                scanf("%d",&str[j]);
            int d,w;
            scanf("%d%d",&d,&w);
            sum+=d;
            ad(0,i,d);
            for(int j=1; j<=7; j++)
            {
                for(int k=0; k

 

你可能感兴趣的:(网络流,算法小结)