0
点赞
收藏
分享

微信扫一扫

poj 3624 01背包

kiliwalk 2023-08-23 阅读 32


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 条评论