zoukankan      html  css  js  c++  java
  • 逃亡的准备

    【问题描述】

    在《Harry Potter and the Deathly Hallows》中,Harry Potter他们一起逃亡,现在有许多的东西要放到赫敏的包里面,但是包的大小有限,所以我们只能够在里面放入非常重要的物品,现在给出该种物品的数量、体积、价值的数值,希望你能够算出怎样能使背包的价值最大的组合方式,并且输出这个数值,赫敏会非常地感谢你。

    【输入文件】(hallows.in)

    (1)第一行有2个整数,物品种数n和背包装载体积v。

    (2)2行到n+1行每行3个整数,为第i种物品的数量m、体积w、价值s。.

         

    【输出文件】(hallows.out)

          输出文件hallows.out仅包含一个整数,即为能拿到的最大的物品价值总和。

    【输入样例】                                    

    2 10                           

    3 4 3

    2 2 5

    【输出样例】

    13

    【注释】

    选第一种一个,第二种两个。

    结果为3*1+5*2=13

    【数据规模】

    对于30%的数据

    1<=v<=500

    1<=n<=2000

    1<=m<=10

    1<=w<=20

    1<=s<=100

     

    对于100%的数据

    1<=v<=500

    1<=n<=2000

    1<=m<=5000

    1<=w<=20

    1<=s<=100

    /*
      背包水题
    */
    #include<cstdio>
    #include<iostream>
    #define N 40010
    #define M 510
    using namespace std;
    int w[N],v[N],f[M],n,m,cnt;
    int main()
    {
        freopen("jh.in","r",stdin);
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            int num,ww,vv,p=0;
            scanf("%d%d%d",&num,&ww,&vv);
            while(num-(1<<p)>=0)
            {
                w[++cnt]=ww*(1<<p);
                v[cnt]=vv*(1<<p);
                num-=(1<<p);p++;
            }
            if(num)
            {
                w[++cnt]=ww*num;
                v[cnt]=vv*num;
            }
        }
        for(int i=1;i<=cnt;i++)
          for(int j=m;j>=w[i];j--)
            f[j]=max(f[j-w[i]]+v[i],f[j]);
        printf("%d",f[m]);
        return 0;
    }
    View Code
  • 相关阅读:
    1163
    1162
    1154
    Qt532_QWebView做成DLL供VC/Delphi使用_Bug
    Qt532__std::numeric_limits<qint64>::min();
    Qt532_WebKit_SSL问题
    Qt532_自定义QWebView_01
    HTML解析
    Qt5_pro_02
    Qt5需要的_libstdc++6_4.7.2-5_???.deb
  • 原文地址:https://www.cnblogs.com/harden/p/5933068.html
Copyright © 2011-2022 走看看