• HDU-2844 Coins (多重背包)


    题目:

    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


    • 多重背包问题,要求每个背包恰好装满,因此把dp[0]赋为0,其他赋值负无穷。
    • 一开始忘记二进制优化最后要把剩余的数量再做一次01背包,因此老是漏掉2*1的解。
    • 觉得自己把所有东西都写在一块太难看了,因此借鉴了kuangbin大神的模板,瞬间清爽多了。

    This is Code:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define M(a, b) memset(a, b, sizeof(a))
     4 #define INF -0x3f3f3f3f
     5 int val[100], num[100], dp[100005], n, m;
     6 
     7 void ZeroOnePack(int cost, int weight){
     8     for (int i = m; i >= cost; i--)
     9         dp[i] = max(dp[i-cost]+weight, dp[i]);
    10 }
    11 
    12 void CompletePack(int cost, int weight){
    13     for (int i = cost; i <= m; i++)
    14         dp[i] = max(dp[i-cost]+weight, dp[i]);
    15 }
    16 
    17 void MultiplePack(int cost, int weight, int amount){
    18     if (cost*amount >= m) CompletePack(cost, weight);
    19     else{
    20         int k = 1;
    21         while(k < amount){
    22             ZeroOnePack(k*cost, k*weight);
    23             amount -= k;
    24             k <<= 1;
    25         }
    26         ZeroOnePack(amount*cost, amount*weight);//不要漏解
    27     }
    28 }
    29 
    30 int main()
    31 {
    32     while(scanf("%d%d", &n, &m), n || m){
    33         M(val, 0);
    34         M(num, 0);
    35         for (int i = 1; i <= m; i++) dp[i] = INF;
    36         dp[0] = 0;
    37         int maxn = 0;
    38         for (int i = 0; i < n; i++) scanf("%d", &val[i]);
    39         for (int i = 0; i < n; i++){
    40             scanf("%d", &num[i]);
    41             maxn += val[i] * num[i];
    42         }
    43         m = min(maxn, m);//背包容量取预计最大金额和所有硬币最大金额的最小值
    44         for (int i = 0; i < n; i++){
    45             MultiplePack(val[i], val[i], num[i]);
    46         }
    47         int ans = 0;
    48         for (int i = 0; i <= m ; i++)
    49             if(dp[i] > 0) ++ans;
    50 
    51         printf("%d
    ", ans);
    52     }
    53 
    54     return 0;
    55 }
  • 相关阅读:
    【Flutter学习】页面布局之基础布局组件
    【Flutter学习】基本组件之AppBar顶部导航栏
    【Flutter学习】基本组件之BottomNavigationBar底部导航栏
    开发日记:JsonCSharpHelp
    会议抢订
    C# WinForm 技巧十: winfrom 全屏自适应屏幕分辨率
    阿里云 轻量应用服务器(LAMP) 使用日志记录
    常见模块设计--权限管理(auth)
    PHP获取项目所有控制器方法名称
    想要开发自己的PHP框架需要那些知识储备?
  • 原文地址:https://www.cnblogs.com/robin1998/p/6359134.html
Copyright © 2020-2023  润新知