Alice's Chance
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 7875 | Accepted: 3213 |
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,
- it will be made ONLY on some fixed days in a week, i.e., Alice can only work for the film on these days;
- Alice should work for it at least for specified number of days;
- 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
题目大意 : 一个演员要参加N个电影的拍摄, 每个电影规定每周哪几天可以拍摄, 并规定最少需要在w周内拍摄完毕, 问你是否可以将所有电影全部拍摄完
思路 : 这是个二分图多重匹配的问题, 之所以是多重匹配, 因为他一个点可能连着多条边, 所以用网络流来做, 其实匹配问题都可以转化成网络流, 每条边的权值为1就可以了, 剩下就是建边, 源点为0, 电影数量为1 到 N, 天数为N + 最后期限 * 7, 汇点为N + 最后期限 * 7 + 1, 然后把每个电影和对应的天数进行连边, 每个源点和电影之间连边, 每个可以拍摄电影的天和汇点连边, 流量都为1, 判断是否满流即可
Accepted code
#include<iostream>
#include<algorithm>
#include<functional>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & (-x))
#define P2(x) ((x) * (x))
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 1e6 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }
struct Edge
{
int v, w, next;
}e[MAXN << 1];
int head[MAXN], f[55][10], n, m, cnt, stot, max_, T, X;
int dep[MAXN], cur[MAXN], w[55], d, sp, tp, tot;
bool vis[MAXN];
void init() {
MEM(head, -1); MEM(vis, 0);
cnt = tot = max_ = 0;
}
void add(int from, int to, int w) {
e[cnt].v = to; e[cnt].w = w;
e[cnt].next = head[from]; head[from] = cnt++;
e[cnt].v = from; e[cnt].w = 0;
e[cnt].next = head[to]; head[to] = cnt++;
}
bool bfs() {
queue <int> q; MEM(dep, 0);
dep[sp] = 1, q.push(sp);
while (!q.empty()) {
int now = q.front();
q.pop();
for (int i = head[now]; i != -1; i = e[i].next) {
int vi = e[i].v;
if (!dep[vi] && e[i].w) {
dep[vi] = dep[now] + 1;
q.push(vi);
}
}
}
if (dep[tp]) return true;
else return false;
}
int dfs(int x, int dist) {
if (x == tp || dist == 0) return dist;
int res = 0, r;
for (int &i = cur[x]; i != -1; i = e[i].next) {
int vi = e[i].v;
if (dep[vi] == dep[x] + 1 && e[i].w) {
r = dfs(vi, min(e[i].w, dist - res));
if (r > 0) {
e[i].w -= r;
e[i ^ 1].w += r;
res += r;
if (res == dist) return dist;
}
}
}
if (!res) dep[x] = 0;
return res;
}
int dinic() {
int flow = 0, ans;
while (bfs()) {
for (int i = sp; i <= tp; i++) cur[i] = head[i];
flow += dfs(sp, INF);
}
return flow;
}
int main()
{
cin >> T;
while (T--) {
sc("%d", &n); init(); sp = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= 7; j++) sc("%d", &f[i][j]);
sc("%d %d", &d, &w[i]); tot += d;
add(sp, i, d);
Max(max_, w[i]); // 最后期限, 便于给点编号
}
tp = n + 7 * max_ + 1;
for (int i = n + 1; i <= n + 7 * max_; i++) add(i, tp, 1);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= w[i]; j++) {
for (int k = 1; k <= 7; k++) {
if (f[i][k]) add(i, n + 7 * (j - 1) + k, 1);
}
}
}
int ans = dinic();
if (ans == tot) cout << "Yes" << endl;
else cout << "No" << endl;
}
return 0;
}