看了许多的题解,都有题目翻译,很不错,以后我也这样写。直接翻译样例:

      /*鞋子的数量N[1, 100]; 拥有的金钱M[1, 1w]; 品牌数目[1, 10]*/
/*以下四行是对于每双鞋的描述*/
/*品牌种类a; 标价b; 高兴程度增加量c*/ /*每一种品牌的鞋子最少买一双,求最大的高兴程度*/

很容易看出是分组背包的题型,trick是价格可能为0(居然有免费的),所以注意dp转移数组初始化-inf。

 #include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <set>
using namespace std; //const int = INT_MIN;
int s[][], v[][], dp[][]; int main () {
ios :: sync_with_stdio(false);
int N, M, K, a, b, c;
while (cin >> N >> M >> K) {
int cur[] = {};
for (int i = ; i <= N; ++ i) {
cin >> a >> b >> c;
s[a][++ cur[a]] = b;
v[a][cur[a]] = c;
}
/*测试种类*/
/*
for (int i = 1; i <= N; ++ i) {
cout << i << " : " << cur[i] << endl;
}
*/ /*dp数组初始化*/
for (int i = ; i <= K; ++ i) {
for (int j = ; j <= M; ++ j) {
dp[i][j] = INT_MIN;
}
} for (int i = ; i <= K; ++ i) {
for (int j = ; j <= cur[i]; ++ j) {
for (int k = M; k >= s[i][j]; -- k) {
dp[i][k] = max (dp[i][k], max (dp[i][k - s[i][j]] + v[i][j], dp[i - ][k - s[i][j]] + v[i][j]) );
}
}
} //cout << dp[K][M] << endl;
if (dp[K][M] < ) {
cout << "Impossible" << endl;
} else {
cout << dp[K][M] << endl;
}
}
return ;
}
04-19 20:18