解题报告 之 HDU1698 Alice's Chance

解题报告 之 HDU1698 Alice's Chance


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
1 0 1 0 1 0 9 3 0
0
1 1 1 0 0 0 6 4
2
  
  
1 1 1 0 0 0 6 2
0 1 0 1 0 1 0 9 4
0

Sample Output

  
  
Yes
No

题目大意:有n部电影同时开拍,爱丽丝每天只能参与拍摄一部电影,且电影 i 只可以在一周的某些日子拍摄 (如周一、周三、周五),但不一定个日子都去。第 i 个电影需要work天的拍摄时间,且必须在第week周结束之前拍完。问你爱丽丝是不是能拍完所有电影。

分析:典型的二分匹配嘛。首先更正一下错误的理解,首先week指的是从开拍起,到第几周结束之前拍完;而不是需要在week周之内拍摄完。也就是说不能在中间截取week周,在此内拍完。我一开始理解错了, 于是建图的时候直接抽象到以星期几来建图,那么就错了。于是乎应该以每一天建图。

首先,超级源点和汇点分别为0和371(其实可以自己指定,我比较习惯一前一后)。为什么是371呢,因为最多有7*50=350天再加上20部电影,所以是371。首先汇点将所有天数连接,边负载为1,然后如果电影在哪一天可以拍(遍历前week周与能拍的星期几),那么将这一天与该部电影连接,负载为1,然后将每部电影与超级汇点连接,边负载为work(需要的天数)。BTW有一种思路是源点与电影连接,而汇点与天数连接,我猜想可能是dfs或者bfs比较快,不过我觉得理解不直观。

最大流之后看看最大流==需要的天数之和?如果是则表示能够拍完。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
const int MAXN = 10000;
const int MAXM = 50000;
const int INF = 0x3f3f3f3f;
using namespace std;

struct Edge
{
	int to, next,cap;
};

Edge edge[MAXM];
int head[MAXN];
int level[MAXN];
int cnt,src,des;

void addedge(int from, int to, int cap)
{
	edge[cnt].to = to;
	edge[cnt].cap = cap;
	edge[cnt].next = head[from];
	head[from] = cnt++;

	edge[cnt].to = from;
	edge[cnt].cap = 0;
	edge[cnt].next = head[to];
	head[to] = cnt++;

}

int bfs()
{
	queue<int> q;
	while (!q.empty())
		q.pop();

	memset(level, -1, sizeof level);
	level[src] = 0;
	q.push(src);

	while (!q.empty())
	{
		int u = q.front();
		q.pop();

		for (int i = head[u]; i != -1; i = edge[i].next)
		{
			int v = edge[i].to;
			if (edge[i].cap > 0 && level[v] == -1)
			{
				level[v] = level[u] + 1;
				q.push(v);
			}
		}
	}
	return level[des] != -1;
}

int dfs(int u, int f)
{
	if (u == des) return f;
	int tem;
	for (int i = head[u]; i != -1; i = edge[i].next)
	{
		int v = edge[i].to;
		if (edge[i].cap > 0 && level[v] == level[u] + 1)
		{
			tem = dfs(v, min(f, edge[i].cap));
			if (tem > 0)
			{
				edge[i].cap -= tem;
				edge[i ^ 1].cap += tem;	
				return tem;
			}
		}
	}
	level[u] = -1;
	return 0;
}

int Dinic()
{
	int ans = 0, tem;
	while (bfs())
	{
		while (tem = dfs(src, INF))
		{
			ans += tem;
		}
	}
	return ans;
}


int main()
{
	int kase;
	cin >> kase;
	while (kase--)
	{
		int n, ok[8],  work, week, totalw = 0;
		cin >> n;
		cnt = 0;
		memset(head, -1, sizeof head);

		src = 0;
		des = 371;

		for (int i = 350+1; i <= 350+n; i++)
		{
			for (int d = 1; d <= 7; d++)
			{
				cin >> ok[d];
			}
			cin >> work>>week;
			
			totalw += work;

			for (int d = 1; d <= 7; d++)
			{
				if (ok[d])
				{
					for (int k = 0; k < week; k++)
						addedge(k*7+d,i,1);
				}
			} //日子与电影相连

			addedge(i, des, work);
		}
		for (int d = 1; d<= 350; d++)
		{
			addedge(0, d, 1);
		}
		cout << (Dinic()==totalw? "Yes":"No") << endl;
	}
	return 0;
}
再一次证明了该模板的吊炸天。敲得越来越熟练呢了!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值