您的位置:首页 > 其它

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

2014-11-09 13:04 260 查看

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

[6],n,v1,v2,k;
int a
,b
,c
;
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: