写代码是一种艺术,甚于蒙娜丽莎的微笑!

不拼搏,枉少年!

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

对于背包问题在前面动态规划 - 0-1背包问题的算法优化已经讲到了关于0-1背包问题的解法,0-1背包问题是最基本的背包问题,它的特点是:每一件物品之多只能选择一件,即在背包中该物品数量只有0和1两种情况。

现在扩展一下,有一个容积为V的背包,同时有n种物品,每种物品均有无数多个,并且每种物品的都有自己的体积和价值。求使用该背包最多能够装的物品价值总和。

这就是完全背包问题。

如果按照0-1背包的思路求解该问题,可设当前物品的体积为w,价值为v,考虑到背包中最多存放V/w件该物品,那么该物品的可选数量就为V/w件。依次可以对所有的物品进行拆分,最后对拆分的所有物品做0-1背包即可得到答案。但是,这样拆分会使物品数量大大增加,其时间复杂度为:O(V*∑ni=1(V/wi))。

可见,当V较大时每个物品的体积较小时,其时间复杂度会显著增大。所以将完全背包问题转化为0-1背包问题去解决的方法不可靠。

在0-1背包的解决算法中,其中一段代码是该算法的核心算法,如下:

struct Good{
    int w;
    int v;
}goods[101];
int dp[101][1001];
int n,S;//n表示有n个物品,S表示背包的最大容积
for (i = 1; i <= n; i++)
{
    for (j = S; j >= goods[i].w; j--)
        dp[j] = max(dp[j], dp[j - goods[i].w] + goods[i].v);
}

在这段代码中,之所将j初始化为S,逆序循环更新状态是为了保证在更新dp[j]时,dp[j-goods[i].w]的状态尚未因为本次更新而发生改变,即等价于由

dp[i-1][j-goods[i].w]转移得到dp[i][j]。保证了更新dp[j]时,dp[j-goods[i].w]是没有放入物品i时的数据dp[i-1][j-goods[i].w]。

在解决完全背包问题时,可以借鉴这个思路。在完全背包中,每个物品可以被无限次选择,那么状态dp[i][j]恰好可以由可能已经放入物品i的状态dp[i][j-goods[i].w]转移而来。可以将上面的代码改写如下:

for (i = 1; i <= n; i++)
{
    for (j = goods[i].w; j <= S; j++)
        dp[j] = max(dp[j], dp[j - goods[i].w] + goods[i].v);
}

九度教程102题:Piggy-Bank

Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from Irreversibly The idea behind is simple. Whenever some ACM member has any small money, he takes all the coins and throws them into a piggy-bank. You know
that this process is irreversible, the coins cannot be removed without breaking the pig. After a sufficiently long time, there should be enough cash in the piggy-bank to pay everything that needs to be paid.
But there is a big problem with piggy-banks. It is not possible to determine how much money is inside. So we might break the pig into pieces only to find out that there is not enough money. Clearly, we want to avoid this unpleasant situation. The only possibility is to weigh the piggy-bank and try to guess how many coins are inside. Assume that we are able to determine the weight of the pig exactly and that we know
the weights of all coins of a given currency. Then there is some minimum amount of money in the piggy-bank that we can guarantee. Your task is to find out this worst case and determine the minimum amount of cash inside the piggy-bank. We need your help. No more prematurely broken pigs!
输入:
The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing two integers E and F. They indicate the weight of an empty pig and of the pig filled with coins. Both
weights are given in grams. No pig will weigh more than 10 kg, that means 1 <= E <= F <= 10000. On the second line of each test case, there is an integer number N (1 <= N <= 500) that gives the number of various coins used in the given currency.
Following this are exactly N lines, each specifying one coin type. These lines contain two integers each, Pand W (1 <= P <= 50000, 1 <= W <=10000). P is the value of the coin in monetary units, W is it's weight in grams.
输出:
Print exactly one line of output for each test case. The line must contain the sentence "The minimum amount of money in the piggy-bank is X." where X is the minimum amount of money that can be achieved using coins with the given total weight. If the weight cannot be reached exactly, print a line "This is impossible.".

编码实现:

#include <stdio.h>
#define INF (1e8)
int dp1[10001];
int v[501], w[501];
int min(int a, int b)
{
    return a < b ? a : b;
}
int main()
{
    int Ncase;
    scanf("%d", &Ncase);
    while (Ncase--)
    {
        int wEmptyPig, wFullPig;
        scanf("%d%d", &wEmptyPig, &wFullPig);
        int wMoney = wFullPig - wEmptyPig;
        int countOfMoneySp;
        scanf("%d", &countOfMoneySp);
 
        for (int i = 1; i <= countOfMoneySp; i++)
        {
            scanf("%d%d", &v[i], &w[i]);
        }
 
        for (int i = 0; i <= wMoney; i++)
            dp1[i] = INF;
        dp1[0] = 0;
        for (int i = 1; i <= countOfMoneySp; i++)
        {
            for (int j = w[i]; j <= wMoney; j++)
            {
                if (dp1[j - w[i]] != INF)
                    dp1[j] = min(dp1[j], dp1[j - w[i]] + v[i]);
            }
        }
        if (dp1[wMoney] == INF)
            printf("This is impossible.\n");
        else
            printf("The minimum amount of money in the piggy-bank is %d.\n", dp1[wMoney]);
    }
    return 0;
}
 
/**************************************************************
    Problem: 1454
    User: 凌月明心
    Language: C++
    Result: Accepted
    Time:70 ms
    Memory:1064 kb
****************************************************************/

完全背包问题,其特点为每种物品可选的数量为无数个,其解法与0-1背包整体保持一致,不同点在于状态更新时的遍历顺序。

posted on 2016-03-28 14:54  RunningSnail  阅读(14203)  评论(1编辑  收藏  举报