将多个物品化为一个物品的01背包,注意下组合即可,还有Hand武器如果只有一个也要加入。
进行背包要按每种物品的个数从大到小
#include <bits/stdc++.h>
using namespace std;
#define sci(num) scanf("%d",&num)
#define mem(a,b) memset(a,b,sizeof a)
const int MAX_N = 50010;
struct P {
int first,second;
};
vector<P> ctions[20];
int dp[15][MAX_N];
char
str[50][50] = {"","Head","Shoulder","Neck","
Torso
","Hand","Wrist","Waist","Legs",
"Feet","Two-Finger","Two-Handed","Shield","Weapon","Finger"};
bool cmp(vector<P>& p1,vector<P>& p2) {
return p1.size() > p2.size();
}
char
s[100];
int main() {
int cases;
sci(cases);
while (cases--) {
int N,M;
//cin >> N >> M;
sci(N); sci(M);
for (int i = 0;i < 15;i++) {
ctions[i].clear();
}
int f,d;
for (int i =0;i < N;i++) {
//cin >> a >> f >>d ;
P p;
scanf("%s%d%d",s,&p.first,&p.second);
int id;
for (int j = 1;j <= 14;j++) {
if (strcmp(str[j],s) == 0) {
id = j;
break;
}
}
ctions[id].push_back(p);
}
for (int i = 0;i < ctions[14].size();i++) {
for (int j = i + 1;j < ctions[14].size();j++) {
P p;
p.first = ctions[14][i].first + ctions[14][j].first;
p.second = ctions[14][i].second + ctions[14][j].second;
ctions[10].push_back(p);
}
}
if (ctions[14].size() == 1) {
ctions[10].push_back(ctions[14][0]);
}
for (int i =0;i < ctions[12].size();i++) {
for (int j = 0;j < ctions[13].size();j++) {
P p;
p.first = ctions[12][i].first + ctions[13][j].first;
p.second = ctions[12][i].second + ctions[13][j].second;
ctions[11].push_back(p);
}
}
if (ctions[13].empty() ) {
for (int i = 0;i < ctions[12].size();i++) {
ctions[11].push_back(ctions[12][i]);
}
}
if (ctions[12].empty()) {
for (int i = 0;i < ctions[13].size();i++) {
ctions[11].push_back(ctions[13][i]);
}
}
sort(ctions+1,ctions+12,cmp);
mem(dp,-1);
dp[0][0] = 0;
for (int i = 1;i < 12;i++) {
for (int V= 0;V <= M;V++) {
dp[i][V] = max(dp[i][V],dp[i - 1][V]);
if(dp[i - 1][V] == -1) continue;
//cout << "OK" << endl;
for (int k = 0;k < ctions[i].size();k++) {
P& p = ctions[i][k];
int tmp = min(M,ctions[i][k].second + V);
dp[i][tmp] = max(dp[i][tmp],dp[i - 1][V] + p.first);
}
}
}
printf("%d\n", dp[11][M]);
}
return 0;
}