POJ-1698 Alice's Chance (网络流)

POJ-1698 Alice’s Chance (网络流)

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个电影,每个电影都有一个拍摄天数。还有他必须在前k周内拍完,并且必须在每周固定的周几来拍。
问:能不能把这些电影都拍完。
先输入 n,然后有行,每行9个数,前7个数代表周几能不能拍(比如前7个数是 1111111 表示周一到周日都能拍),接下来两个数就是这个电影需要拍多少天,和 必须在前几周内拍完。

解题思路:

关键在网络流建图:
我们观察数据范围 这个w只有50,所以最多有50*7 = 350天,把这350天每天当作一个点。这样就能解决前k周的问题。
源点连n个电影**,流量是这个电影所需要拍的天数**,每个电影连其对应的天,把前k周的周i转变成第 (k-1)+i天,连这个点,流量是1,最后把每个"天"连超级汇点,流量是1(因为每天只能拍一个电影),这样在源点和汇点之间跑最大流得出答案为ans。然后将这个ans和所有电影的拍摄天数之和作比较,如果ans>=sum 说明能全拍完,反之则不能全拍完。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm> 
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 11000;
int n,m,sp,tp;
int N,F,D;
int head[maxn*10],cnt = 0;
int deep[maxn*10];
int cur[maxn*10];
struct node{
	int to;
	int w;
	int next;
}side[maxn*10];
void init(){
	memset(head,-1,sizeof(head));
	cnt = 0;
}
void add(int x,int y,int w){
	side[cnt].to = y;
	side[cnt].w = w;
	side[cnt].next = head[x];
	head[x] = cnt++;
}
bool bfs(){
	memset(deep,-1,sizeof(deep));
	queue<int> q;
	q.push(sp);
	deep[sp] = 0;
	while(q.size()){
		int now = q.front();
		q.pop();
		for(int i=head[now];i!=-1;i=side[i].next){
			int to = side[i].to;
			if(deep[to]==-1&&side[i].w>0){
				deep[to] = deep[now]+1;
				q.push(to);
				if(to == tp) break;
			}
		}
	}
	return deep[tp]!=-1;
}
int dfs(int u,int cap){
	if(u==tp||cap==0) return cap;
	int res = 0;
	int f;
	for(int &i=cur[u];i!=-1;i=side[i].next){
		int to = side[i].to;
		if(deep[to]==deep[u]+1&&(f=dfs(to,min(side[i].w,cap-res)))>0){
			side[i].w-=f;
			side[i^1].w+=f;
			res+=f;
			if(res==cap) return cap;
		}
	}
	if(!res) deep[u] = -1;
	return res;
}
int Dinic(){
	int ans = 0;
	while(bfs()){
		for(int i = 1;i<=tp;i++) cur[i] = head[i];
		ans +=dfs(sp,inf);
	}
	return ans ;	
} 
int week[10];
int need[maxn];
int main(){
	int t;
	cin>>t;
	while(t--){
		scanf("%d",&n);
		init();
		sp = 1,tp =n+350+2;
		int sum = 0;
		int u,v;
		for(int i = 1;i<=n;i++){
			for(int j=1;j<=7;j++){
				scanf("%d",&week[j]);
			}
			int tmp,w;
			scanf("%d%d",&tmp,&w);
			need[i] = tmp;
			sum += tmp;
			for(int j=1;j<=7;j++){
				if(week[j] == 1){
					for(int k = 0;k<w;k++){
						u = n+k*7+j+1;
						add(i+1,u,1);
						add(u,i+1,0);
					}
				}
			}
		}
		for(int i=1;i<=n;i++){
			add(1,i+1,need[i]);
			add(i+1,1,0);
		}
		for(int i=1;i<=350;i++){
			u = i+1+n;
			add(u,tp,1);
			add(tp,u,0);
		}
		int ans = Dinic();
		if(ans>=sum){
			puts("Yes");
		}else{
			puts("No");
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值