POJ 1698 Alice's Chance

来源:互联网 发布:雅漾在淘宝有旗舰店吗 编辑:程序博客网 时间:2024/05/22 04:30

链接:http://poj.org/problem?id=1698

题目:

Alice's Chance
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 5070 Accepted: 2080

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

220 1 0 1 0 1 0 9 30 1 1 1 0 0 0 6 420 1 0 1 0 1 0 9 40 1 1 1 0 0 0 6 2

Sample Output

YesNo

解题思路:

这题是一个匹配的问题,可以用最大流来做。关键问题就是如何构图。我们可以这样来进行构图,以0作为源点,将电影的序号(1~20)作为节点,连接源点和电影点,容量为拍完这部电影需要的天数,我们还需要将可以拍电影的天(在截止周之前的,值为1的那些天)作为节点,天的标号(21~370),连接电影点和日期点,容量为1(表示每一天只能拍一部电影),将371这个点作为汇点,连接日期点和汇点,容量为1。这样的话,一个网络流的模型就构建好了,用EK算法求出最大流即可。从源点到汇点的最大流即为:每天拍一部电影(在可拍日拍),在截止日期之前,最多可以拍电影的天数。如果这个值,与需要拍的电影的总天数相等,就可以吧所有的电影都拍完了。


代码:

#include <iostream>#include <cstdio>#include <queue>#include <limits.h>#include <cstring>using namespace std;#define min(x,y) x < y ? x : yconst int MAXN = 375;int n, p[MAXN], flow[MAXN][MAXN];int bfs(int s, int t){queue<int> q; q.push(s);memset(p, -1, sizeof(p));while(!q.empty()){int u = q.front(); q.pop();for(int v = 0; v <= 371; v++){if(-1 == p[v] && flow[u][v] > 0){p[v] = u; q.push(v);if(v == t) return 1;}}}return 0;}int ek(int s, int t){int f = 0;while(bfs(s,t)){int inc = INT_MAX;for(int u = t; u != s; u = p[u]){inc = min(inc, flow[p[u]][u]);}if(0 == inc) break;for(int u = t; u != s; u = p[u]){flow[p[u]][u] -= inc;flow[u][p[u]] += inc;}f += inc;}return f;}int main(){int t;scanf("%d", &t);while(t--){memset(flow, 0, sizeof(flow));scanf("%d", &n);int sum = 0;/*构图*/for(int i = 1; i <= n; i++){int weekday[7], day, week;for(int j = 0; j < 7; j++){scanf("%d", &weekday[j]);}scanf("%d%d", &day, &week);flow[0][i] = day;sum += day;for(int j = 0; j < week; j++){for(int k = 0; k < 7; k++){if(weekday[k]){int v = 21 + 7 * j + k;flow[i][v] = 1;flow[v][371] = 1;}}}}if(sum == ek(0, 371)){printf("Yes\n");}else{printf("No\n");}}return 0;} 


0 0
原创粉丝点击