对于背包问题在前面动态规划 - 0-1背包问题的算法优化已经讲到了关于0-1背包问题的解法,0-1背包问题是最基本的背包问题,它的特色是:每一件物品之多只能选择一件,即在背包中该物品数量只有0和1两种状况。php
如今扩展一下,有一个容积为V的背包,同时有n种物品,每种物品均有无数多个,而且每种物品的都有本身的体积和价值。求使用该背包最多可以装的物品价值总和。html
这就是彻底背包问题。算法
若是按照0-1背包的思路求解该问题,可设当前物品的体积为w,价值为v,考虑到背包中最多存放V/w件该物品,那么该物品的可选数量就为V/w件。依次能够对全部的物品进行拆分,最后对拆分的全部物品作0-1背包便可获得答案。可是,这样拆分会使物品数量大大增长,其时间复杂度为:O(V*∑ni=1(V/wi))。ide
可见,当V较大时每一个物品的体积较小时,其时间复杂度会显著增大。因此将彻底背包问题转化为0-1背包问题去解决的方法不可靠。post
在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]的状态还没有由于本次更新而发生改变,即等价于由this
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]转移而来。能够将上面的代码改写以下:url
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-Bankidea
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背包总体保持一致,不一样点在于状态更新时的遍历顺序。