ACM 之 M - 基础DP

Description

Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave … The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously , different bone has different value and different volume, now given the each bone’s value along his trip , can you calculate out the maximum of the total value the bone collector can get ?

Input

The first line contain a integer T , the number of cases. Followed by T cases , each case three lines , the first line contain two integer N , V, (N <= 1000 , V <= 1000 )representing the number of bones and the volume of his bag. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone.

Output

One integer per line representing the maximum of the total value (this number will be less than 2 ^31).

Sample Input

1
5 10
1 2 3 4 5
5 4 3 2 1

Sample Output

14

理解:

第一道DP题,也就是01背包问题,我的做法就是多看多写多理解。
另外,个人感觉用一位数组更简洁明了。

代码部分

#include <cstdio>
#include <cstring>
using namespace std;
int va[101],vo[101],dp[100002],t,n,v;
inline int Max(int x,int y)
{return x>y?x:y;}
int main()
{
    while(scanf("%d",&t)!=EOF)
    {
        getchar();
        for(int i=0;i<t;i++)
        {
            scanf("%d%d",&va[i],&vo[i]);
        }getchar();
        scanf("%d",&v);getchar();
        memset(dp,0,sizeof(dp));
        for(int i=0;i<t;i++)
            for(int j=vo[i];j<=v;j++)
                dp[j]=Max(dp[j],dp[j-vo[i]]+va[i]);
        printf("%d\n",dp[v]);
    }
    return 0;
}

意见反馈 || 任何建议

联系我(新浪)
邮箱:qianlizhihao@gmail.com

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 最近这些日子,现实成功地打消了本科毕业时对未来的种种幻想。 到了找工作的阶段,对自己的感觉在所不能和一无是处之间不...
    冯昱霖阅读 5,126评论 10 12
  • 我的文字可能不是很美,语言组织可能也不是很严谨,内容可能也不够精彩,可是我想慢慢写点远离朋友圈远离生活圈的事情。 ...
    开心逗不开心阅读 1,810评论 2 2
  • 第一课,真诚的课题 分享心得:真诚,一直以来都被教育说要真诚待人,真诚待人—这一说,是要真诚的对待别人,真诚对待顾...
    郭晓萱阅读 4,483评论 0 2
  • 偶然看到淘宝上在卖一种名为“昨天”的酒。其实也不过是普通的梅子酒,只是包装好看,名字特色而已。 “昨天”...
    李青茴阅读 2,600评论 0 1

友情链接更多精彩内容