zoukankan      html  css  js  c++  java
  • hdu 1059 Dividing 多重背包

    http://acm.hdu.edu.cn/showproblem.php?pid=1059

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    using namespace std;
    int a[7];
    int dp[121111];
    int v,k;
    void ZeroOnePack(int cost,int weight) {
        for(int i=v;i>=cost;i--)
            dp[i] = max(dp[i] , dp[i-cost] + weight);    
    }
    void CompletePack(int cost,int weight) {
        for(int i=cost;i<=v;i++)
            dp[i] = max(dp[i] , dp[i-cost] + weight);    
    }
    void MultiplePack(int cost,int weight,int amount) {
        if(cost * amount >= v) CompletePack(cost,weight);
        else {
            for(int k=1;k<amount;) {
                ZeroOnePack(k*cost,k*weight);
                amount -= k;
                k <<= 1;    
            }    
            ZeroOnePack(amount*cost,amount*weight);
        }  
    }
    int main() {
        int cas = 1;
        while(1) {
            int tot = 0;
            for(int i=1;i<=6;i++) {
                scanf("%d",&a[i]);
                tot += a[i] * i;
            }    
            if(tot == 0) break;
            printf("Collection #%d:
    ",cas++);
            if(tot % 2) puts("Can't be divided.");
            else {
                v = tot / 2;
                memset(dp,0,sizeof(dp));
                for(int i=1;i<=6;i++) {
                    MultiplePack(i,i,a[i]);    
                }    
                if(dp[v] == v) puts("Can be divided.");
                else puts("Can't be divided.");
            }
            puts("");
        }
        return 0;    
    }
    

      

  • 相关阅读:
    此生对我影响最大的三位老师
    介绍自己
    介绍自己
    第三周作业
    第二周作业
    PTA编程总结3
    PTA编程总结2
    PTA编程总结1
    2019年春季学期第七周作业.
    2019年春季学期第六周作业.
  • 原文地址:https://www.cnblogs.com/tobec/p/3341904.html
Copyright © 2011-2022 走看看