其他分享
首页 > 其他分享> > PACM Team (多种重量的01背包+记录路径) 牛客

PACM Team (多种重量的01背包+记录路径) 牛客

作者:互联网

题面:
Since then on, Eddy found that physics is actually the most important thing in the contest. Thus, he wants to form a team to guide the following contestants to conquer the PACM contests(PACM is short for Physics, Algorithm, Coding, Math).

There are N candidate groups each composed of pi physics experts, ai algorithm experts, ci coding experts, mi math experts. For each group, Eddy can either invite all of them or none of them. If i-th team is invited, they will bring gi knowledge points which is calculated by Eddy’s magic formula. Eddy believes that the higher the total knowledge points is, the better a team could place in a contest. But, Eddy doesn’t want too many experts in the same area in the invited groups. Thus, the number of invited physics experts should not exceed P, and A for algorithm experts, C for coding experts, M for math experts.

Eddy is still busy in studying Physics. You come to help him to figure out which groups should be invited such that they doesn’t exceed the constraint and will bring the most knowledge points in total.
1 ≤ N ≤ 36
0 ≤ pi,ai,ci,mi,gi ≤ 36
0 ≤ P, A, C, M ≤ 36
题意:
Eddy现在想招募多支队伍辅助他,每一个队伍有4种人,人数分别为p,a,c,m,另有1个权值g。
现在Eddy想在招募队伍各个职务的人数各不超过P,A,C,M的条件下使得g的和最大。
输出选择的队伍的编号(编号从0开始),如果有相同结果时任意输出。
思路: 01背包的多维质量,跟一维类似,多了个记录路径

#include<bits/stdc++.h>
using namespace std;
const int N=37;
int p[N],a[N],c[N],m[N],g[N];
bool path[N][N][N][N][N];
int dp[N][N][N][N];
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%d%d%d%d%d",&p[i],&a[i],&c[i],&m[i],&g[i]);
	}
	int pp,aa,cc,mm;
	scanf("%d%d%d%d",&pp,&aa,&cc,&mm);
	for(int i=0;i<n;i++)
	{
		for(int j=pp;j>=p[i];j--)
		{
			for(int k=aa;k>=a[i];k--)
			{
				for(int l=cc;l>=c[i];l--)
				{
					for(int r=mm;r>=m[i];r--)
					{
						if(dp[j][k][l][r]<dp[j-p[i]][k-a[i]][l-c[i]][r-m[i]]+g[i])
						{
							dp[j][k][l][r]=dp[j-p[i]][k-a[i]][l-c[i]][r-m[i]]+g[i];
							path[i][j][k][l][r]=true;
						}
					}
				}
			}
		}
	}
	vector<int>ans;
	for(int i=n-1;i>=0;i--)
	{
		if(path[i][pp][aa][cc][mm])
		{
			ans.push_back(i);
			pp-=p[i],aa-=a[i];
			cc-=c[i],mm-=m[i];
		}
	}
	printf("%d\n",ans.size());
	reverse(ans.begin(),ans.end());
	for(auto it: ans)
	{
		printf("%d ",it);
	}
	puts("");
	return 0;
}

标签:01,knowledge,int,invited,ans,牛客,Eddy,experts,Team
来源: https://blog.csdn.net/qq_42819598/article/details/97152596