超大背包问题

有一类背包问题中背包的体积很大但是价值很小,这种背包中如果按照正常的思维来写的话很容易超内存,可以换种思维,dp[I]表示价值为I的价值用的最小空间;
例题:



Knapsack problem
Crawling in process... Crawling failed  

Description

Given a set of n items, each with a weight w[i] and a value v[i], determine a way to choose the items into a knapsack so that the total weight is less than or equal to a given limit B and the total value is as large as possible. Find the maximum total value. (Note that each item can be only chosen once).

Input

The first line contains the integer T indicating to the number of test cases.

For each test case, the first line contains the integers n and B.

Following n lines provide the information of each item.

The i-th line contains the weight w[i] and the value v[i] of the i-th item respectively.

1 <= number of test cases <= 100

1 <= n <= 500

1 <= B, w[i] <= 1000000000

1 <= v[1]+v[2]+...+v[n] <= 5000

All the inputs are integers.

Output

For each test case, output the maximum value.

Sample Input

  1
  5 15
12 4
  2 2
1 1
4 10
1 2

Sample Output

  15
解体思路:首先这个很清楚看出来是个01背包。但是这个背包容量特别大,同时物品的体积很大,总的价值却很少。寻常意义上dp[i]表示背包容量为i时的最大价值,
那么我们把这个dp[]数组的含义改变一下,dp[i]表示装价值为i时所需的最小容量。
感悟:唉,压线过得真持基;
#include
#include
#include
using namespace std;
const int maxn = 550;
const int INF = 0x3f3f3f3f;
int w[maxn], v[maxn];
int dp[5500];
int main(){
    freopen("in.txt","r",stdin);
    int T, n, B;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&B);
        int V = 0;
        for(int i = 1; i <= n; i++){
            scanf("%d%d",&w[i],&v[i]);
            V += v[i];
        }
        memset(dp,INF,sizeof(dp));
        dp[0] = 0;
        for(int i = 1; i <= n; i++){
            for(int j = V; j >= v[i]; j--){
                dp[j] = min(dp[j],dp[j-v[i]]+w[i]);
             //   printf("%d ",dp[j]);
            }
        }
        int ans = 0;
        for(int i = V; i >= 0; i--){
            if(dp[i] <= B){
                ans = i; break;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

转载于:https://www.cnblogs.com/wuwangchuxin0924/p/5781535.html

### 大规模01背包问题的解决方案 针对大规模01背包问题,动态规划提供了一种有效的解决方法。然而,在处理非常大的数据集时,标准的二维数组实现可能会遇到内存不足的问题。因此,采用一维滚动数组的空间优化策略成为一种常见的改进措施[^1]。 #### 动态规划的一维数组优化 在一维数组优化的方法中,仅需维护一个大小为容量加一的一维数组`dp[]`。该数组用于记录不同重量下的最大价值。更新此数组的方式是从后向前遍历物品列表,从而避免重复覆盖未处理的数据: ```java public class Knapsack { public static int knapSack(int W, int wt[], int val[], int n) { int i, w; int K[] = new int[W + 1]; for (i = 0; i <= n; i++) { for (w = W; w >= wt[i]; w--) { if(wt[i]<=W){ K[w] = Math.max(K[w], K[w - wt[i]] + val[i]); } } } return K[W]; } } ``` 这种方法不仅减少了所需的额外空间,还保持了时间复杂度不变,即O(n*W),其中n表示物品数量而W代表背包的最大承重能力。 当面对更大规模的问题实例时,除了上述提到的技术外,还可以考虑其他几种可能有助于进一步提升性能的选择: - **近似算法**:如果允许一定的误差范围,则可以通过舍弃部分精度换取更高的执行速度; - **启发式搜索**:利用诸如遗传算法或者模拟退火这样的随机化技术探索解空间,虽然无法保证找到全局最优解,但在很多情况下能快速给出满意的结果; - **分布式计算框架**:借助MapReduce等工具将任务分解成多个子任务并发运行,适用于拥有海量输入数据的情形; 尽管贪心算法简单易懂,但对于0-1背包这类存在相互依赖性的组合最优化难题来说并不适用,因为这种局部最优选择往往不能带来全局最佳效果[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值