zoukankan      html  css  js  c++  java
  • HDU2844_Coins【多重背包】【二进制优化】

    Coins


    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 7497    Accepted Submission(s): 3055

    Problem Description
    Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One day Hibix opened purse and found there were some coins. He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he known the price would not more than m.But he didn't know the exact price of the watch.

    You are to write a program which reads n,m,A1,A2,A3...An and C1,C2,C3...Cn corresponding to the number of Tony's coins of value A1,A2,A3...An then calculate how many prices(form 1 to m) Tony can pay use these coins.
     
    Input
    The input contains several test cases. The first line of each test case contains two integers n(1 ≤ n ≤ 100),m(m ≤ 100000).The second line contains 2n integers, denoting A1,A2,A3...An,C1,C2,C3...Cn (1 ≤ Ai ≤ 100000,1 ≤ Ci ≤ 1000). The last test case is followed by two zeros.
     
    Output
    For each test case output the answer on a single line.
     
    Sample Input
    3 10
    1 2 4 2 1 1
    2 5
    1 4 2 1
    0 0
     
    Sample Output
    8

    4


    题目大意:给你几种硬币的价值和数量,再给你一个最大钱数M,问你这些硬币能

    组成价值1到M的值有多少种

    思路:简单的多重背包,假设总容量比这个物品的容量要小,那么这个物品能够直

    接取完,相当于全然背包。否则的话就转成01背包来求解。

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    
    int v[110],c[110];
    int dp[100010],V;
    //v数组存价值,c数组存数量,V是总容量
    void ZeroOne(int cost,int weight)//01背包
    {
        for(int i = V; i >= cost; i--)
            dp[i] = max(dp[i],dp[i-cost]+weight);
    }
    
    void Complete(int cost,int weight)//全然背包
    {
        for(int i = cost; i <= V; i++)
            dp[i] = max(dp[i],dp[i-cost]+weight);
    }
    
    void Multiple(int cost,int weight,int cnt)//多重背包
    {
        //假设总容量比这个物品的容量要小,那么这个物品能够直接取完,相当于全然背包
        if(V <= cnt*cost)
        {
            Complete(cost,weight);
            return;
        }
        else//否则就将多重背包转化为01背包
        {
            int k = 1;
            while(k <= cnt)
            {
                ZeroOne(k*cost,k*weight);
                cnt -= k;
                k <<= 1;
            }
            ZeroOne(cnt*cost,cnt*weight);
        }
    }
    int main()
    {
        int N;
        while(~scanf("%d%d",&N,&V)&&(N!=0||V!=0))
        {
            for(int i = 0; i < N; i++)
                scanf("%d",&v[i]);
            for(int i = 0; i < N; i++)
                scanf("%d",&c[i]);
            for(int i = 0; i <= V; i++)//初始化:是否恰好装满背包
                dp[i] = -0xffffff0;
            dp[0] = 0;
            for(int i = 0; i < N; i++)
                Multiple(v[i],v[i],c[i]);
            int ans = 0;
            for(int i = 1; i <= V; i++)
                if(dp[i] >= 0)
                    ans++;
            printf("%d
    ",ans);
        }
        return 0;
    }
    


  • 相关阅读:
    串字符串下沙的沙粒
    格式返回jquery js 获取获得时间差,时间格式为
    类型数组perl6学习
    安装文件win7,ubuntu双系统的安装——准备工作
    下标注意【算法】冒泡排序与选择排序的递归实现
    容器结构Thrift的数据类型系统
    linux初次入门学习小结
    sata拷贝文件时候framebuffer 闪烁问题
    switch_root 到nfs根文件
    Linux内核中64 bit division
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4329010.html
Copyright © 2011-2022 走看看