http://acm.hdu.edu.cn/showproblem.php?pid=4501
很明显的背包,只是有三个取舍方法,所以开三维的dp。这里最坑爹的是尽然有0元物品与0积分物品,所有要用一个temp来记录最大值,最后再赋值给dp;
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
const int N=110;
using namespace std;
int dp[N][N][6],n,v1,v2,k;
int a[N],b[N],c[N];
int main(){
// freopen("in.txt", "r", stdin);
while(scanf("%d%d%d%d",&n,&v1,&v2,&k) == 4){
for(int i=1; i<=n; i++)
scanf("%d%d%d",&a[i],&b[i],&c[i]);
memset(dp, 0, sizeof(dp));
for(int i=1; i<=n; i++){
for(int j=v1; j>=0; j--){
for(int p=v2; p>=0; p--){
for(int l=k; l>=0; l--){
int tmp = 0;
if(j >= a[i])
tmp = max(tmp, dp[j-a[i]][p][l] + c[i]);
if(p >= b[i])
tmp = max(tmp, dp[j][p-b[i]][l] + c[i]);
if(l >= 1)
tmp = max(tmp, dp[j][p][l-1] + c[i]);
dp[j][p][l] = max(dp[j][p][l],tmp);
}
}
}
}
printf("%d\n",dp[v1][v2][k]);
}
return 0;
}