zoukankan      html  css  js  c++  java
  • 5709 01背包

    5709 01背包

     

    时间限制: 1 s
    空间限制: 128000 KB
    题目等级 : 黄金 Gold
     
     
     
     
    题目描述 Description

    cjw很奇怪,他喜欢吃巧克力,tr送给他一个包,包容量是S,cjw可以自己去巧克力店里取巧克力,已知有n块巧克力,每块有一个重量W[i]和一个使cjw快乐的价值K[i],请写一个程序求出cjw能得到的最大总快乐价值。

    输入描述 Input Description

       

    输入文件第一行包含一个正整数S,表示包重S。( 0<S<=5000 )
    第二行包含N个正整数,表示S中元素个数。(0<n<=1000)
    第3~3+N-1行包含N个正整数W[i],K[i]。( 0<W[i],K[i]<=100000)

    输出描述 Output Description

       

    输出文件一行只包含一个正整数X,表示最大快乐价值。
    样例输入 Sample Input

    5

    3

    1 5

    2 3

    3 4

    样例输出 Sample Output

    9

    数据范围及提示 Data Size & Hint

     1 #include<iostream>
     2 #include<cstdio>
     3 
     4 using namespace std;
     5 const int N=1001;
     6 
     7 int w[N];
     8 int c[N];
     9 int f[N][N];
    10 
    11 int main()
    12 {
    13     int n,m;
    14     cin>>n>>m;
    15     for(int i=1;i<=m;i++)
    16     {
    17         cin>>w[i]>>c[i];
    18     }
    19     
    20     for(int i=1;i<=m;i++)
    21     {
    22         for(int j=n;j>=1;j--)
    23         {
    24             if(w[i]<=j)
    25             {
    26                 f[i][j]=max(f[i-1][j],f[i-1][j-w[i]]+c[i]);
    27             }
    28             else f[i][j]=f[i-1][j];
    29         }
    30     }
    31     
    32     cout<<f[m][n];
    33 } 
  • 相关阅读:
    friend ---- public and private
    c++中const使用详解
    In c++ access control works on per-class basis not on per-object basis.
    realloc 用法
    enum don't allocate any memory
    (转)C++ STL中的vector的内存分配与释放
    计算机网络面试总结
    Redis安装与测试
    Hbase的安装与测试
    使用ActiveMQ实现简易聊天功能
  • 原文地址:https://www.cnblogs.com/lyqlyq/p/6821054.html
Copyright © 2011-2022 走看看