先看个未经优化的二维空间dp:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <memory.h>
using namespace std;
const int maxn1=;
const int maxn2=;
int dp[maxn2][maxn2];
//int dp[maxn2];
int w[maxn1],v[maxn2];
int m,n;
int max(int x,int y)
{
return x>y?x:y;
}
int main()
{
freopen("in.txt","r",stdin);
while(~scanf("%d%d",&n,&m))
{
for(int i=;i<=n;i++)
cin >> w[i] >> v[i];
}
memset(dp,,sizeof(dp));
int j;
for(int i=;i<=n;i++)
{
for(j=;j<=m;j++)
{
if(j>=w[i])
dp[i][j]=max(dp[i-][j],dp[i-][j-w[i]]+v[i]);
else
dp[i][j] = dp[i-][j];
}
}
cout << dp[n][m] << endl;
return ;
}

改进:

1。二维优化到一维

2。倒写

#include <iostream>
#include <cstdio>
#include <cmath>
#include <memory.h>
using namespace std;
const int maxn1=;
const int maxn2=;
int dp[maxn2],w[maxn1],v[maxn2];
int m,n;
int max(int a, int b)
{
if(a>b) return a ;
else return b ;
}
int main()
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
while(~scanf("%d%d",&n,&m))
{
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)
cin >> w[i] >> v[i];
int j;
for(int i=;i<=n;i++)
{
for(j=m;j>=w[i];j--)
{
dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
cout << j << " j "<< dp[j]<< endl;
}
cout <<endl;
}
cout << dp[m] << endl;
}
return ;
}

给一组数据便于理解:

6 j 4
5 j 4
4 j 4
3 j 4
2 j 4
1 j 4

6 j 10
5 j 10
4 j 10
3 j 10
2 j 6

6 j 22
5 j 18
4 j 16
3 j 12

6 j 23
5 j 19
4 j 16
3 j 12
2 j 7

23

最后给出模板:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <memory.h>
using namespace std; const int maxn1=;
const int maxn2=;
int dp[maxn2],w[maxn1],v[maxn1];
int m,n; int max(int a, int b)
{
if(a>b) return a ;
else return b ;
} int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d%d",&n,&m))
{
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)
cin >> w[i] >> v[i];
int j;
for(int i=;i<=n;i++)
for(j=m;j>=w[i];j--)
dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
cout << dp[m] << endl;
}
return ;
}

【0-1 背包模板】 poj 3624的更多相关文章

  1. 01背包模板、全然背包 and 多重背包(模板)

    转载请注明出处:http://blog.csdn.net/u012860063 贴一个自觉得解说不错的链接:http://www.cppblog.com/tanky-woo/archive/2010/ ...

  2. POJ 3624 Charm Bracelet (01背包)

    题目链接:http://poj.org/problem?id=3624 Bessie has gone to the mall's jewelry store and spies a charm br ...

  3. POJ.3624 Charm Bracelet(DP 01背包)

    POJ.3624 Charm Bracelet(DP 01背包) 题意分析 裸01背包 代码总览 #include <iostream> #include <cstdio> # ...

  4. POJ 1636 Prison rearrangement DFS+0/1背包

    题目链接: id=1636">POJ 1636 Prison rearrangement Prison rearrangement Time Limit: 3000MS   Memor ...

  5. POJ 1745 【0/1 背包】

    题目链接:http://poj.org/problem?id=1745 Divisibility Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

  6. 01背包问题:Charm Bracelet (POJ 3624)(外加一个常数的优化)

    Charm Bracelet    POJ 3624 就是一道典型的01背包问题: #include<iostream> #include<stdio.h> #include& ...

  7. poj 3624 Charm Bracelet 01背包问题

    题目链接:poj 3624 这是最基础的背包问题,特点是:每种物品仅有一件,可以选择放或不放.             用子问题定义状态:即F [i, v]表示前i件物品恰放入一个容量为v 的背包可以 ...

  8. 解题报告:hdu2602 Bone collector 01背包模板

    2017-09-03 15:42:20 writer:pprp 01背包裸题,直接用一维阵列的做法就可以了 /* @theme: 01 背包问题 - 一维阵列 hdu 2602 @writer:ppr ...

  9. 【洛谷P2722 USACO】 总分 01背包模板

    P2722 总分 Score Inflation 题目背景 学生在我们USACO的竞赛中的得分越多我们越高兴. 我们试着设计我们的竞赛以便人们能尽可能的多得分,这需要你的帮助 题目描述 我们可以从几个 ...

  10. P2722 总分 Score Inflation (完全背包模板)

    题目传送门:P2722 总分 Score Inflation 题目描述 我们可以从几个种类中选取竞赛的题目,这里的一个"种类"是指一个竞赛题目的集合,解决集合中的题目需要相同多的时 ...

随机推荐

  1. POJ 3041 Asteroids(匈牙利+邻接表)

    ( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cstring> #include<algo ...

  2. hdu 1022 Train Problem I(栈)

    #include<iostream> #include<vector> #include<cstring> #include<string> #incl ...

  3. wep密钥的长度

    理论上,WEP可以搭配任意长度的密钥,因为RC4并未要求非得使用特定长度的密钥. 不过,大多数产品均支持一种或两种长度的密钥.唯一出现在标准中的密钥长度时64位的WEP种子(seed),其中40位是两 ...

  4. java.lang.SecurityException:Invalid signature file digest forManifest main attributes

    今天在公司使用ant命令来从服务器中提取一个类的时候发现一个问题,每当我执行ant命令的时候就报如下异常: 想来想去猜测是Enovia提供的包没有做数字签名,但是如何解决这个问题呢?想不到解决方案只好 ...

  5. ERP流程图

  6. python 列表、字典的方法

    # 列表最后新增元素 li = [11, 22, 33] print(li) li.append(44) # 对原列表最后增加一个元素 print(li) 执行如下: [11, 22, 33][11, ...

  7. ( ̄y▽ ̄)~ 智能手机II

    ( ̄y▽ ̄)~ 智能手机II TimeLimit: 3000/1000 MS (Java/Others)  MenoryLimit: 32768/32768 K (Java/Others) 64-bi ...

  8. Core Animation中的基础动画

    基础动画 在开发过程中很多情况下通过基础动画就可以满足开发需求,前面例子中使用的UIView代码块进行图像放大缩小的演示动画也是基础动画(在iOS7 中UIView也对关键帧动画进行了封装),只是UI ...

  9. redis1--redis的介绍

    (1)持久化数据库的缺点平常我们使用的关系型数据库有Mysql.Oracle以及SqlServer等,在开发的过程中,数据通常都是通过Web提供的数据库驱动来链接数据库进行增删改查.那么,我们日常使用 ...

  10. Sql Sever语句 (续2)

    日期时间 把日期类型的字段下默认值或绑定里的 设置成getdate()   ,新建字段时候不设置会自动获取当前服务器时间 在当前时间加上xx年xx月xx天 select datedd(yy,100,g ...