POJ 1698--Alice's Chance【最大流 && 经典】

Alice's Chance
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 6384 Accepted: 2607

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

题意:

有一个小女孩从小就梦想成为一名影星,现在机会来了,有很多电影公司找她拍电影,但是这些拍电影的日程安排之间可能还有冲突,但是有女孩不想错过任何机会,想把每个公司的电影都接下来,但是她不知道能不能接下所有的电影,所有找到了你优秀的ACMER来帮忙解决问题。
数据的输入如下:有T组数据每组先有一个N表示要找她拍的电影的个数,其中N<=20;
每个电影有9个数据,前7个数据表示一个星期的7天 ,这7个数不是0,就是1,1表示能在这天拍电影,0表示不能在这天拍电影 还有2个数据D,W。D表示这部电影需要她拍D天才能完成,W表示这D天必须在前W周内。


解析:

标准的最大流问题,就是建图稍微麻烦了一点,由题意可知,最多20部电影, 50周。50周按天算,一共350天。先把节点划分一下,节点1~20表示电影,节点21~370表示50周的每一天日期。0为源点, 371为汇点。

(1)源点到每部电影建边,权值为每部电影花费的时间。

(2)每部电影到可以拍摄的日期建边,权值为1。(这里要注意,要把前w个周可以拍摄的日期都会对应的电影连起来)

(3)所有日期到汇点建边,权值为1

跑一遍最大流,看看是否满流,满流的的话就输出Yes,否则就输出No。

</pre><pre name="code" class="cpp">#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define maxn 1000
#define maxm 20000
#define INF 0x3f3f3f3f
using namespace std;
int n;//n个电影
struct node{
    int u, v, cap, flow, next;
};

node edge[maxm];
int head[maxn], cnt, cur[maxn];
int vis[maxn], dist[maxn];
int str[25][10];//记录每部电影可以在哪一天拍摄
int sum;//总共的拍摄时间

void init(){
    cnt = 0;
    memset(head, -1, sizeof(head));
}

void add(int u, int v, int w){
    node E;
    edge[cnt] = {u, v, w, 0, head[u]};
    head[u] = cnt++;
    edge[cnt] = {v, u, 0, 0 ,head[v]};
    head[v] = cnt++;
}

void getmap(){
    //0为源点,371为汇点
    sum = 0;
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= 7; ++j)
            scanf("%d", &str[i][j]);
        int d, w;
        scanf("%d%d", &d, &w);
        sum += d;
        add(0, i, d);
        for(int j = 1; j <= 7; ++j)
            for(int k = 0; k < w; ++k){//这里要注意
                if(str[i][j])
                    add(i, 20 + k * 7 + j, 1);
            }
    }
    for(int i = 21; i <= 370; ++i)
        add(i, 371, 1);
}

bool BFS(int st ,int ed){
    queue<int>q;
    memset(vis, 0 ,sizeof(vis));
    memset(dist, -1, sizeof(dist));
    vis[st] = 1;
    dist[st] = 0;
    q.push(st);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        for(int i = head[u]; i != -1; i = edge[i].next){
            node E = edge[i];
            if(!vis[E.v] && E.cap > E.flow){
                vis[E.v] = 1;
                dist[E.v] = dist[u] + 1;
                if(E.v == ed)
                    return true;
                q.push(E.v);
            }
        }
    }
    return false;
}

int DFS(int x, int ed, int a){
    if(x == ed || a == 0)
        return a;
    int flow = 0, f;
    for(int &i = cur[x]; i != -1; i = edge[i].next){
        node &E = edge[i];
        if(dist[E.v] == dist[x] + 1 && (f = DFS(E.v, ed, min(a, E.cap - E.flow))) > 0){
            E.flow += f;
            edge[i ^ 1].flow -= f;
            a -= f;
            flow += f;
            if(a == 0)
                break;
        }
    }
    return flow;
}

int maxflow(int st, int ed){
    int flowsum = 0;
    while(BFS(st,ed)){
        memcpy(cur, head, sizeof(head));
        flowsum += DFS(st, ed, INF);
    }
    return flowsum;
}

int main (){
    int T;
    scanf("%d", &T);
    while(T--){
        init();
        getmap();
        int ans = maxflow(0, 371);
        if(ans == sum)
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值