HDU - 6006 Engineer Assignment (状压dp)

In Google, there are many experts of different areas. For example, MapReduce experts, Bigtable experts, SQL experts, etc. Directors need to properly assign experts to various projects in order to make the projects going smoothly. 
There are N projects owned by a director. For the ithith project, it needs CiCi different areas of experts, ai,0,ai,1,⋅⋅⋅,ai,Ci−1ai,0,ai,1,···,ai,Ci−1 respective. There are M engineers reporting to the director. For the ithith engineer, he is an expert of DiDi different areas, bi,0,bi,1,...,bi,Di−1.bi,0,bi,1,...,bi,Di−1. 
Each engineer can only be assigned to one project and the director can assign several engineers to a project. A project can only be finished successfully if the engineers expert areas covers the project areas, which means, for each necessary area of the project, there is at least one engineer 
masters it. 
The director wants to know how many projects can be successfully finished. 

Input

The first line of the input gives the number of test cases, T. T test cases follow. Each test case starts with a line consisting of 2 integers, N the number of projects and M the number of engineers. Then N lines follow. The ithith line containing the information of the ithith project starts 
with an integer CiCi then CiCi integers follow, ai,0,ai,1,...,ai,Ci−1ai,0,ai,1,...,ai,Ci−1 representing the expert areas needed for the ithith project. Then another M lines follow. The ithith line containing the information of the ithith engineer starts with an integer DiDi then DiDiintegers follow, bi,0,bi,1,...,bi,Di−1bi,0,bi,1,...,bi,Di−1 representing the expert areas mastered by ithithengineer. 

Output

For each test case, output one line containing “Case #x: y”, where x is the test case number (starting from 1) and y is the maximum number of projects can be successfully finished. 

limits


∙1≤T≤100.∙1≤T≤100. 
∙1≤N,M≤10.∙1≤N,M≤10. 
∙1≤Ci≤3.∙1≤Ci≤3. 
∙1≤Di≤2.∙1≤Di≤2. 
∙1≤ai,j,bi,j≤100.∙1≤ai,j,bi,j≤100. 

Sample Input

1
3 4
3 40 77 64
3 10 40 20
3 40 20 77
2 40 77
2 77 64
2 40 10
2 20 77

Sample Output

Case #1: 2

        
  

Hint

For the first test case, there are 3 projects and 4 engineers. One of the optimal solution is to assign the first(40 77) and second engineer(77 64) to project 1, which could cover the necessary areas 40, 77, 64. Assign the third(40 10) and forth(20 77) engineer to project 2, which could cover the necessary areas 10, 40, 20. There are other solutions, but none of them can finish all 3 projects.
So the answer is 2.

 

      有n个项目m个人,n个项目有c个方面需要砖家坐镇~~m个专家分别能坐镇d个方面~~求样的分配能够获得最大的项目完成数量。

       我们用dp[i][j]表示做完到第i个项目为止,人员使用状态为j的情况下的最大项目完成数量是多少就好了。

      其中need[i]来表示完成第i个项目的各种人员分配方式有什么。我们可以发现,一个项目最多需要3个专家就够了(分别负责三个方面)否则一定会出现重复或者完全没用的人的存在。这样的话我们可以先进预处理,用sum[i]来表示i状态下有多少个人被使用了(二进制有几个1),多于三个的跳过就好了。

#include <bits/stdc++.h>
using namespace std;
vector<int>need[20];
int a[20][10], b[20][10], vis[105];
int dp[20][1 << 11], n, m;
int sum[1 << 11];
void init() {
	sum[0] = 0;
	for (int i = 1; i < (1 << 11); i++) {
		sum[i] = sum[i&(i - 1)] + 1;
	}
}
int main() {
	int te;
	cin >> te;
	init();
	for (int t = 1; t <= te; t++) {
		cin >> n >> m;
		memset(dp, 0, sizeof dp);
		for (int i = 1; i <= n; i++)
			need[i].clear();
		for (int i = 1; i <= n; i++) {
			scanf("%d", &a[i][0]);
			for (int j = 1; j <= a[i][0]; j++)
				scanf("%d", &a[i][j]);
		}
		for (int i = 0; i<m; i++)
		{
			scanf("%d", &b[i][0]);
			for (int j = 1; j <= b[i][0]; j++)
				scanf("%d", &b[i][j]);
		}
		for (int i = 1; i <= n; i++) {
			for (int j = 0; j < (1 << m); j++) {
				if (sum[j] > 3)continue;//因为一个项目最多就需要三个人(分别负责三个方面),
										//多于3个的话对于这个项目而言肯定会出现他所必须的方面的重复
										//或者出现有人的能力不是这个项目所必需的。
				memset(vis, 0, sizeof(vis));
				for (int k = 0; k < m; k++) {
					if (j&(1 << k)) {
						for (int p = 1; p <= b[k][0]; p++)
							vis[b[k][p]] = 1;
					}
				}
				bool flag = 1;
				for (int k = 1; k <= a[i][0]; k++)
					if (!vis[a[i][k]]) flag = 0;
				if (flag) need[i].push_back(j);
			}
		}
		for (int i = 1; i <= n; i++)
		{
			for (int j = 0; j<(1 << m); j++)
			{
				int sz = need[i].size();
				for (int k = 0; k < sz; k++)
					if ((j | need[i][k]) == j) dp[i][j] = max(dp[i - 1][j - need[i][k]] + 1, dp[i][j]);
				dp[i][j] = max(dp[i][j], dp[i - 1][j]);
			}
		}
		printf("Case #%d: %d\n", t, dp[n][(1 << m) - 1]);
	}
}

 

全部评论

相关推荐

寿命齿轮:实习就一段还拉了,项目一看就不是手搓,学历也拉了,技术栈看着倒是挺好,就是不知道面试表现能咋样。 不过现在才大三,争取搞两端大厂实习,或者一个纯个人项目+一段大厂,感觉秋招还是未来可期。
投递美团等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务