zoukankan      html  css  js  c++  java
  • 【luogu1776】 宝物筛选_NOI导刊2010提高(02) [动规 多重背包]

    1776 宝物筛选_NOI导刊2010提高(02) 

    我...比较弱 暂时只能打出二进制优化 过段时间再来刚

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    #define ll long long
    #define rg register
    #define Max(x,y) (x)>(y)?(x):(y)
    #define Min(x,y) (x)>(y)?(y):(x)
    const int N=200+5,M=200000+5,inf=0x3f3f3f3f,P=19650827;
    int n,m,ans=0,cnt=0,f[M],w[M],v[M]; 
    template <class t>void rd(t &x){
        x=0;int w=0;char ch=0;
        while(!isdigit(ch)) w|=ch=='-',ch=getchar();
        while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
        x=w?-x:x;
    }
    
    int main(){
    //    freopen("in.txt","r",stdin);
        rd(n),rd(m);
        for(int i=1,a,b,c;i<=n;++i){
            rd(a),rd(b),rd(c);
            for(int j=1;j<=c;j<<=1)    w[++cnt]=j*a,v[cnt]=j*b,c-=j;
            if(c) w[++cnt]=c*a,v[cnt]=c*b;
        }
        for(int i=1;i<=cnt;++i){
            for(int j=m;j>=v[i];--j) f[j]=Max(f[j],f[j-v[i]]+w[i]);
        }
        printf("%d",f[m]);
        return 0;
    }
    二进制优化
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    #define ll long long
    #define rg register
    #define Max(x,y) (x)>(y)?(x):(y)
    #define Min(x,y) (x)>(y)?(y):(x)
    const int N=200+5,M=200000+5,inf=0x3f3f3f3f,P=19650827;
    int n,w,ans=0,a[N],b[N],c[N],f[M]; 
    template <class t>void rd(t &x){
        x=0;int w=0;char ch=0;
        while(!isdigit(ch)) w|=ch=='-',ch=getchar();
        while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
        x=w?-x:x;
    }
     
    int main(){
    //  freopen("in.txt","r",stdin);
        rd(n),rd(w);
        for(int i=1;i<=n;++i){
            rd(a[i]),rd(b[i]),rd(c[i]);
            for(int v=w;v>=b[i];--v){
                for(int j=1;j<=c[i];++j)
                if(v>=b[i]*j) f[v]=max(f[v],f[v-b[i]*j]+a[i]*j);
                ans=Max(ans,f[v]);
            }
        }
        printf("%d",ans);
        return 0;
    }
    30昏 裸的完全背包
  • 相关阅读:
    1295: [SCOI2009]最长距离
    [vijos p1028] 魔族密码
    HJ浇花
    1060: [ZJOI2007]时态同步
    1816: [Cqoi2010]扑克牌
    1800: [Ahoi2009]fly 飞行棋
    4300: 绝世好题
    1237: [SCOI2008]配对
    1801: [Ahoi2009]chess 中国象棋
    1189: [HNOI2007]紧急疏散evacuate
  • 原文地址:https://www.cnblogs.com/lxyyyy/p/11177003.html
Copyright © 2011-2022 走看看