• POJ 1276:Cash Machine 多重背包


    Cash Machine
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 30006   Accepted: 10811

    Description

    A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate @ bills for a requested cash amount. The machine uses exactly N distinct bill denominations, say Dk, k=1,N, and for each denomination Dk the machine has a supply of nk bills. For example, 

    N=3, n1=10, D1=100, n2=4, D2=50, n3=5, D3=10 

    means the machine has a supply of 10 bills of @100 each, 4 bills of @50 each, and 5 bills of @10 each. 

    Call cash the requested amount of cash the machine should deliver and write a program that computes the maximum amount of cash less than or equal to cash that can be effectively delivered according to the available bill supply of the machine.

    Notes: 
    @ is the symbol of the currency delivered by the machine. For instance, @ may stand for dollar, euro, pound etc. 

    Input

    The program input is from standard input. Each data set in the input stands for a particular transaction and has the format: 

    cash N n1 D1 n2 D2 ... nN DN 

    where 0 <= cash <= 100000 is the amount of cash requested, 0 <=N <= 10 is the number of bill denominations and 0 <= nk <= 1000 is the number of available bills for the Dk denomination, 1 <= Dk <= 1000, k=1,N. White spaces can occur freely between the numbers in the input. The input data are correct. 

    Output

    For each set of data the program prints the result to the standard output on a separate line as shown in the examples below. 

    Sample Input

    735 3  4 125  6 5  3 350
    633 4  500 30  6 100  1 5  0 1
    735 0
    0 3  10 100  10 50  10 10

    Sample Output

    735
    630
    0
    0

    Hint

    The first data set designates a transaction where the amount of cash requested is @735. The machine contains 3 bill denominations: 4 bills of @125, 6 bills of @5, and 3 bills of @350. The machine can deliver the exact amount of requested cash. 

    In the second case the bill supply of the machine does not fit the exact amount of cash requested. The maximum cash that can be delivered is @630. Notice that there can be several possibilities to combine the bills in the machine for matching the delivered cash. 

    In the third case the machine is empty and no cash is delivered. In the fourth case the amount of cash requested is @0 and, therefore, the machine delivers no cash.

    题意是给你几种金钱的面额,每种面额有多少张纸币,然后给一个target金额。问在给定的纸币条件下,能凑成的最大的小于等于target的面额是多少。

    关于多重背包,背包九讲里是这么解答处理一件多重背包物品的过程的:

    def MultiplePack(F,C,W,M)
    	if C * M >= V
    		CompletePack(F,C,W)
    		return
    	k := 1
    	while k < M
    		ZeroOnePack(kC,kW)
    		M := M - k
    		k := 2k
    	ZeroOnePack(C * M, W * M)

    我的理解:首先对于一件物品来说,如果这个物品的容量*数目已经大于背包的总容量了,那么这与完全背包问题没有区别了。因为完全背包问题就是可以任意的往背包里面放物品,而这时这个物品的容量*数目已经大于背包总容量了,所以,也就相当于在背包总容量的范围内,这个物品是可以支持 任意地往背包里面放物品。

    如果这个物品的容量*数目小于背包的总容量,那么正常情况下,需要一件一件往里面放找最大值的。但是它的方法是:将第i种物品分成若干件01背包中的物品,其中每件物品有一个系数。这件物品的费用和价值均是原来的费用和价值乘以这个系数。令这些系数分别为1,2, 2的2次方,2的三次方,。。。2的k-1次方,最后是Mi - 2的k次方 + 1,且k是满足Mi - 2的k次方 + 1 的最大整数。例如,如果Mi为13,则相应的k=3,这种最多取13件物品的应被分成系数分别为1 2 4 6 的四件物品。

    我觉得这里设计的很nice在于,正常情况下,举例来说这个物品有13件,我们需要一件一件放,放1件,2件,3件,。。。13件都放入,然后不断找最大值。但是上面的方法利用二进制,4个数,就做了13个数的事情。

    发现 1 2 4 6 这四个数相互之间的和 就已经可以表示1到13的所有数,原因就在于当我放入1件物品,再放入2件物品找最大值的时候,实际上相当于我一下子放入了该物品的3件,所以实际上3是不需要我们再去实验的了。


    代码:

    #include <iostream>
    #include <algorithm>
    #include <cmath>
    #include <vector>
    #include <string>
    #include <cstring>
    #pragma warning(disable:4996)
    using namespace std;
    
    int w, n;
    int num[500002];
    int val[500002];
    int c[500002];
    long long dp[500002];
    
    void Zero_Pack(int weight, int cost, int w)
    {
    	int i;
    	for (i = w; i >= cost; i--)
    	{
    		dp[i] = max(dp[i], dp[i - cost] + weight);
    	}
    }
    
    void Complete_Pack(int weight,int cost,int w)
    {
    	int i;
    	for (i = cost; i <= w; i++)
    	{
    		dp[i] = max(dp[i], dp[i - cost] + weight);
    	}
    }
    
    long long Multi_Pack(int c[],int val[],int num[],int n,int w)
    {
    	memset(dp, 0, sizeof(dp));
    	int i;
    	for (i = 1; i <= n; i++)
    	{
    		if (num[i] * c[i] > w)
    			Complete_Pack(val[i],c[i],w);
    		else
    		{
    			int k = 1;
    			while (k < num[i])
    			{
    				Zero_Pack(k*val[i],k*c[i],w);
    				num[i] = num[i] - k;
    				k = k << 1;
    			}
    			Zero_Pack(num[i]*val[i],num[i]*c[i],w);
    		}
    	}
    	return dp[w];
    }
    int main()
    {		
    	int i, j, v, max_v,re;
    	while (scanf("%d%d", &n,&w) != EOF)
    	{
    		memset(dp, 0, sizeof(dp));
    		for (i = 1; i <= n; i++)
    		{
    			scanf("%d%d%d", c + i, val + i, num + i);
    		}
    		re = Multi_Pack(c, val, num, n, w);
    		cout << re << endl;
    	}
    	return 0;
    }
    




    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    Linux中使用mysqldump对MySQL数据库进行定时备份
    Linux备份数据库,mysqldump命令实例详解
    laravel 怎么获取public路径
    laravel 返回值
    [Deep-Learning-with-Python]机器学习基础
    [Deep-Learning-with-Python]基于Keras的房价预测
    [Deep-Learning-with-Python]基于Kears的Reuters新闻分类
    基于Keras的imdb数据集电影评论情感二分类
    [Deep-Learning-with-Python]神经网络入手学习[上]
    [Deep-Learning-with-Python]神经网络的数学基础
  • 原文地址:https://www.cnblogs.com/lightspeedsmallson/p/4899575.html
Copyright © 2020-2023  润新知