hdu6006(状压dp)

本文探讨了如何通过合理分配专家工程师来确保项目的顺利进行。利用状态压缩技术解决了小规模问题,实现项目所需技能与工程师专长的有效匹配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Engineer Assignment

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 781    Accepted Submission(s): 269


Problem Description
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 ith project, it needs Ci different areas of experts, ai,0,ai,1,,ai,Ci1 respective. There are M engineers reporting to the director. For the ith engineer, he is an expert of Di different areas, bi,0,bi,1,...,bi,Di1.
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 ith line containing the information of the ith project starts
with an integer Ci then Ci integers follow, ai,0,ai,1,...,ai,Ci1 representing the expert areas needed for the ith project. Then another M lines follow. The ithline containing the information of the ith engineer starts with an integer Di then Di integers follow, bi,0,bi,1,...,bi,Di1 representing the expert areas mastered by ith engineer.
 

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


1T100.
1N,M10.
1Ci3.
1Di2.
1ai,j,bi,j100.
 

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、m很小,选择状态压缩的方法,看了别人代码才敲出来的。


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
int proj[15][10];               //工程需要领域知识  proj[i][0]表示个数 
int eng[15][10];               //工程师掌握领域    eng[i][0]表示个数 
int dp[15][1<<11];             //dp[i][st]表示前i个工程在使用工程师的状态为st的情况下能完成的最多工程数 
vector<int>engstate[15];       //存储每个工程需要工程师方案的可能状态 
bool area[105];                //表示领域覆盖状况 
int main()
{
	int t;
	int n,m;
	scanf("%d",&t);
	for(int tt=1;tt<=t;tt++)
	{
		memset(proj,0,sizeof(proj));
		memset(eng,0,sizeof(eng));
		memset(dp,0,sizeof(dp));
		for(int i=0;i<15;i++)engstate[i].clear();
		scanf("%d%d",&n,&m);
		for(int i=0;i<n;i++)    //输入工程所需领域 
		{
			scanf("%d",&proj[i][0]);
			for(int j=1;j<=proj[i][0];j++)
			{
				scanf("%d",&proj[i][j]);
			}
		}
		for(int i=0;i<m;i++)      //输入工程师掌握领域 
		{
			scanf("%d",&eng[i][0]);
			for(int j=1;j<=eng[i][0];j++)
			{
				scanf("%d",&eng[i][j]);
			}
		}
		for(int i=0;i<n;i++)        //求每个工程需要工程师方案的可能状态 
		{
			for(int st=0;st<(1<<m);st++)
			{
				memset(area,0,sizeof(area));
				int cnt=0;
				for(int k=0;k<m;k++)
				{
					if(st&(1<<k))     //找出这种状态下的工程师,求出这种状态下掌握领域状态 
					{
						cnt++;
						for(int k1=1;k1<=eng[k][0];k1++)
						{
							area[eng[k][k1]]=1;
						}
					}
				}
				if(cnt>3)continue;      //多余,剪枝 
				bool flag=true;
				for(int k=1;k<=proj[i][0];k++)
				{
					if(!area[proj[i][k]])       //对于这个工程,有没有覆盖到的领域 
					{
						flag=false;
					}
				}
				if(flag)    //符合要求 
				{
					engstate[i].push_back(st);
				}
			}
		}
		for(int i=0;i<n;i++)
		{
			for(int st=0;st<(1<<m);st++)
			{
				for(int j=0;j<engstate[i].size();j++)
				{
					if((st|engstate[i][j])==st)   //可以完成工程i 
					{
						if(i>0)
							dp[i][st]=max(dp[i][st],dp[i-1][st-engstate[i][j]]+1);     //选择前i个能完成工程i时最多完成工程数 
						else dp[i][st]=1;
					}
				}
				if(i>0)   //比较选与不选择完成工程i 
					dp[i][st]=max(dp[i][st],dp[i-1][st]);
			}
		}
		printf("Case #%d: %d\n",tt,dp[n-1][(1<<m)-1]);
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值