poj1698Alice's Chance

poj1698Alice's Chance:http://poj.org/problem?id=1698


Alice's Chance
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 6519 Accepted: 2660

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

题意:MM有n场戏(种类)要参加,每场戏有固定的时间,并且必须在前w周完成d场。。。。。问她最终能否全部完成。。。

思路:(1、可以用最大流来做。创建超级源点和超级汇点,让源点和n场戏相连,流量为d,再将每周安排的戏和汇点相连,流量为1(每天只能参加一场戏)……若跑出来的流量和d的和相等,则输出“Yes”……

好久没写网络流了,搞了半天才搞出来……看来还是不大熟练阿……

代码:(用isap写的

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <map>
#include <queue>
#include <algorithm>

using namespace std;

const int inf = 0x3fffffff;
template <int N, int M>
struct Isap
{
    int top;
    int d[N], pre[N], cur[N], gap[N];
    struct Vertex
    {
        int head;
    } V[N];
    struct Edge
    {
        int v, next;
        int c, f;
    } E[M];
    void init()
    {
        memset(V, -1, sizeof(V));
        top = 0;
    }
    void add_edge(int u, int v, int c)
    {
        E[top].v = v;
        E[top].c = c;
        E[top].f = 0;
        E[top].next = V[u].head;
        V[u].head = top++;
    }
    void add(int u,int v, int c)
    {
        add_edge(u, v, c);
        add_edge(v, u, 0);
    }
    void set_d(int t)
    {
        queue<int> Q;
        memset(d, -1, sizeof(d));
        memset(gap, 0, sizeof(gap));
        d[t] = 0;
        Q.push(t);
        while(!Q.empty())
        {
            int v = Q.front();
            Q.pop();
            ++gap[d[v]];
            for(int i = V[v].head; ~i; i = E[i].next)
            {
                int u = E[i].v;
                if(d[u] == -1)
                {
                    d[u] = d[v] + 1;
                    Q.push(u);
                }
            }
        }
    }
    int sap(int s, int t, int num)
    {
        set_d(t);
        int ans = 0, u = s;
        int flow = inf;
        memcpy(cur, V, sizeof(V));
        while(d[s] < num)
        {
            int &i = cur[u];
            for(; ~i; i = E[i].next)
            {
                int v = E[i].v;
                if(E[i].c > E[i].f && d[u] == d[v] + 1)
                {
                    u = v;
                    pre[v] = i;
                    flow = min(flow, E[i].c - E[i].f);
                    if(u == t)
                    {
                        while(u != s)
                        {
                            int j = pre[u];
                            E[j].f += flow;
                            E[j^1].f -= flow;
                            u = E[j^1].v;
                        }
                        ans += flow;
                        flow = inf;
                    }
                    break;
                }
            }
            if(i == -1)
            {
                if(--gap[d[u]] == 0)
                    break;
                int dmin = num - 1;
                cur[u] = V[u].head;
                for(int j = V[u].head; ~j; j = E[j].next)
                    if(E[j].c > E[j].f)
                        dmin = min(dmin, d[E[j].v]);
                d[u] = dmin + 1;
                ++gap[d[u]];
                if(u != s)
                    u = E[pre[u] ^ 1].v;
            }
        }
        return ans;
    }
};
Isap<15000, 1000000> Sap;
int f[10],w,d;
int main()
{
//    freopen("in.txt","r",stdin);
    int t;
    scanf("%d",&t);
    while(t --)
    {
        int n,sum = 0,cnt = 0;
        scanf("%d",&n);
        Sap.init();
        int maxw = 0;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d%d%d%d%d%d%d%d",&f[1],&f[2],&f[3],&f[4],&f[5],&f[6],&f[7],&d,&w);
            Sap.add(0,i,d);
            cnt++;
            sum += d;
            maxw = max(w,maxw);
            for(int j = 0; j < w; j++)
                for(int l = 1; l <= 7; l++)
                    if(f[l])
                        Sap.add(i,n + l + j * 7,1),cnt++;
        }
        int T = n + maxw * 7 + 1;
        for(int j = 0; j < maxw; j++)
            for(int l = 1; l <= 7; l++)
                Sap.add(n+l+j*7,T,1),cnt++;
        int ans = Sap.sap(0,n+7*maxw+1,cnt);   // 源点\汇点\边数
        if(ans == sum)
            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、付费专栏及课程。

余额充值