poj 3624 01背包

来源:互联网 发布:知聊能不能提现 编辑:程序博客网 时间:2024/06/06 01:11

http://poj.org/problem?id=3624

主要注意的是内存的优化

以下是我的代码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;


const int sizen=14000;
int dp[sizen];
struct ele
{
    int w;
    int d;
}p[4000];


int main()
{
    int n,m;
    int i,j;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(i=1;i<=n;i++)
            scanf("%d%d",&p[i].w,&p[i].d);
        memset(dp,0,sizeof(dp));
        for(i=1;i<=n;i++)
        for(j=m;j>=1;j--)
            if(j>=p[i].w)
                dp[j]=max(dp[j],dp[j-p[i].w]+p[i].d);
            else
                dp[j]=dp[j];
        printf("%d\n",dp[m]);
    }
    return 0;
}

0 0