hdu_3449(有依赖背包)

https://blog.csdn.net/jibancanyang/article/details/44259105

超时代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int fw[52];
int fnum[52];
int cw[52][12];
int cv[52][12];
int main()
{
    int n,w;
    while(~scanf("%d%d",&n,&w)){
        int h[w+11];
        int dp[w+11];
        for(int i = 0;i < n;i++){
            scanf("%d%d",&fw[i],&fnum[i]);
            for(int j = 0;j < fnum[i]; j++){
                scanf("%d%d",&cw[i][j],&cv[i][j]);
            }
        }
        memset(dp,0,sizeof(dp));
        for(int i = 0; i < n; i++){
            memset(h,0,sizeof(h));
            for(int k = 0; k < fnum[i]; k++){
                for(int l =  w-fw[i]; l >=cw[i][k]; l--){
                    h[l] = max(h[l],h[l-cw[i][k]]+cv[i][k]);
                }
            }
            for(int j = w; j >=1; j--){
                for(int k = 1; k <=w-fw[i]; k++){
                    if(j-fw[i]-k>=0)
                        dp[j] = max(dp[j],dp[j-fw[i]-k]+h[k]);
                }
            }
        }
        printf("%d\n",dp[w]);
    }
    return 0;
}
上一篇:0-1knapsack


下一篇:Quartus15.1复制一个现有IP