zoukankan      html  css  js  c++  java
  • 02bag模板

    cost->体积          weight->价值

    hdu2844

    可达/不可达

    #include <stdio.h>
    #include <algorithm>
    #include <string.h>
    using namespace std;
    
    const int MAX=100000;
    int dp[MAX];
    int c[MAX],w[MAX];
    int v;
    
    void ZeroOnePack(int cost,int wei)//01
    {
        int i;
        for(i = v;i>=cost;i--)
        {
            dp[i] = max(dp[i],dp[i-cost]+wei);
        }
    }
    
    void CompletePack(int cost,int wei)//完全
    {
        int i;
        for(i = cost;i<=v;i++)
        {
            dp[i] = max(dp[i],dp[i-cost]+wei);
        }
    }
    
    void MultiplePack(int cost,int wei,int cnt)//多重
    {
        if(v<=cnt*cost)//如果总容量比这个物品的容量要小,那么这个物品可以直到取完,相当于完全背包
        {
            CompletePack(cost,wei);
            return ;
        }
        else//否则就将多重背包转化为01背包
        {
            int k = 1;
            while(k<=cnt)
            {
                ZeroOnePack(k*cost,k*wei);
                cnt = cnt-k;
                k = 2*k;
            }
            ZeroOnePack(cnt*cost,cnt*wei);
        }
    }
    
    int main()
    {
        int n;
        while(~scanf("%d%d",&n,&v),n+v)
        {
            int i;
            for(i = 0;i<n;i++)
            scanf("%d",&c[i]);
            for(i = 0;i<n;i++)
            scanf("%d",&w[i]);
            memset(dp,0,sizeof(dp));
            for(i = 0;i<n;i++)
            {
                MultiplePack(c[i],c[i],w[i]);
            }
            int sum = 0;
            for(i = 1;i<=v;i++)
            {
                if(dp[i]==i)
                {
                    sum++;
                }
            }
            printf("%d
    ",sum);
        }
        return 0;
    }

    普通模板  hdu2191

    #include<iostream>  
    #include<stdio.h>  
    #include<algorithm>  
    #include<cstring>  
    #include<string.h>  
    #include<string>  
    using namespace std;  
      
    int c[105],w[105],num[105];  
    int dp[105];  
    int v,V,V1;  
      
    void ZeroOnePack(int c, int w)  
    {  
        for(int v = V; v >=c; v--)  
        {  
            dp[v] = max(dp[v],dp[v-c]+w);  
        }  
    }  
      
    void CompletePack(int c, int w)  
    {  
        for(int v = c; v <= V; v++)  
        {  
            dp[v] = max(dp[v],dp[v-c]+w);  
        }  
    }  
      
    void MultiplePack(int c, int w, int num)  
    {  
        if(c * num >= V)  
        {  
            CompletePack(c,w);  
        }  
        else  
        {  
            int k = 1;  
            while(k < num)  
            {  
                ZeroOnePack(k*c, k*w);  
                num -= k;  
                k <<= 1;  
            }  
            ZeroOnePack(num*c, num*w);  
        }  
    }  
    int main()  
    {  
        int t;  
        scanf("%d",&t);  
        int n;  
        int i;  
        while(t--)  
        {  
            scanf("%d%d",&V,&n);  
            for(i = 1; i <=n; i++)  
            {  
                scanf("%d%d%d",&c[i], &w[i], &num[i]);  
            }  
            memset(dp,0,sizeof(dp));  
            for(i = 1; i <= n; i++)  
            {  
                MultiplePack(c[i],w[i],num[i]);  
            }  
            printf("%d
    ",dp[V]);  
        }  
        return 0;  
    }  
  • 相关阅读:
    三次请求(读-改-读)引出nibernate 一级缓存
    算法竞赛入门经典第一、二章摘记
    uva 10905 Children's Game
    uva 11205 The broken pedometer
    uva 10160 Servicing stations
    uva 208 Firetruck
    uva 167 The Sultan's Successors
    zoj 1016 Parencodings
    uva 307 Sticks
    uva 216 Getting in Line
  • 原文地址:https://www.cnblogs.com/Aragaki/p/7499539.html
Copyright © 2011-2022 走看看