HDU 4501 小明系列故事——买年货(多维01背包)



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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值