Poj1276 Cash Machine(多重背包转01背包 两种方法)

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.
 

题意

这是一道典型的多重背包的问题。

第一个数表示目标总金额,第二个数N表示后面有N对数,每对数的第一个表示个数,第二个表示面额价值。

然后组成最接近但不能大于目标总金额的数并输出。

 

思路

此题提供两种思路,复杂度分别为VΣlogMi,VN.

1.VΣlogMi复杂度的思路(适用所有的多重背包问题)

  Mi表示第i对数的个数,将Mi采用二进制编法转化为:Mi=1+2+4+8+...

  进而用01背包求解之

2.VN复杂度的思路(适用物品只有费用不考虑价值的多重背包问题)

  状态:F[i]=1/0表示能否组成金额i.

  状态方程:F[0]=1

       ( if F[i-j*per] ) F[i]=1 (1<=j<=k)

       每次加入一种面额的时候更新,k表示该种面额的个数,per表示面额价值。

代码

第一种VΣlogMi

//Memory:556K Time:563MS
#include <cstdio>
int main()
{
    int C, N, a[11], w[11], i, j, k, p;
    while (scanf("%d", &C) != EOF)
    {
        int f[100001] = { 0 };
        f[0] = 1;
        scanf("%d", &N);
        for (i = 1; i <= N; i++)
        {
            scanf("%d%d", &a[i], &w[i]);
            for (k = 100000; k; k--)
                if (k - w[i] >= 0 && f[k - w[i]])
                    for (p = k, j = 0; j < a[i]; j++)
                    {
                        if (p > 100000 || f[p] == i) break;
                        f[p] = i;
                        p += w[i];
                    }
        }
        for (i = C; !f[i]; i--);
        printf("%d\n", i);
    }
}

 

第二种VN

//Memory:556K Time:47MS
#include<iostream>
using namespace std;
int main()
{
    int amount, n;
    while (scanf("%d%d", &amount, &n)==2)
    {
        int F[100010] = { 0 };
        int k = 0, max = 0;        
        int m, per;
        for (int i = 0; i < n; i++)
        {
            scanf("%d%d", &m, &per);
            F[0] = 1;
            for (int j = amount; j >= 0; j--)
            {
                if (F[j])for (int k = 1; k <= m; k++)
                {
                    if (j + k*per > amount)break;
                    else F[j + k*per] = 1;
                }
            }

        }
        for (int i = amount; i >= 0; i--) if (F[i])
        {
            max = i; break;
        }
            printf("%d\n", max);
    }
}

 

 

总结:

这题弄了很久,有几点心得:

先说几个语法上简单的错误

  1. 刚开始一直TLE,最后发现是主循环写的是while(true)...(简直zz)
  2. 后面第二个VN的方法开始RE,发现是数组有元素超界了。顺便搜了一下RE和TLE的区别,TLE应该是算法复杂度不行,RE是运行错误(直接检查数组越界问题)...

再说算法上的问题

  1. 这个题搞得很懵,虽然一般来讲VN应该比VΣlogMi效率要高,但是可以看到运行时间恰恰相反。可能是因为poj测试数据的问题。
  2. 第二个算法之前写的很复杂,单独把i=0的情况拿出来讨论。记得把算法思路想清楚了再开始写。

 

加油~争取这学期把poj刷到50题以上。

posted @ 2017-05-02 23:32  Shifting  阅读(179)  评论(0编辑  收藏  举报