先看个未经优化的二维空间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. java 用双向链表实现SJBLinkedList

    public class SJBLinkedList{ private Node first; private Node last; private int size; public int size ...

  2. 【Python@Thread】thread模块

    一.关于Python多线程 Python解释器中可以同时运行多个线程,但是再任意时刻只能有一个线程在解释器运行. Python虚拟机的访问是由全局解锁器(GIL)控制的,由GIL保证同时只有一个线程的 ...

  3. Map获取键值,Map的几种遍历方法

    Map 类提供了一个称为entrySet()的方法,这个方法返回一个Map.Entry实例化后的对象集.接着,Map.Entry类提供了一个 getKey()方法和一个getValue()方法,Map ...

  4. POJ 2039 To and Fro

    To and Fro Description Mo and Larry have devised a way of encrypting messages. They first decide sec ...

  5. Python之深浅拷贝&函数

    一.深浅拷贝 深浅拷贝是指copy模块下的copy()和deepcopy()方法. 1.浅拷贝 示例: >>> import copy >>> a = 'hello ...

  6. HTTP Status 400 - Required request part 'file' is not present

    今天使用Spring MVC做一个文件上传的功能,在提交表单的时候出现了如下错误:

  7. 实现简单的手写涂鸦板(demo源码)

    在一些软件系统中,需要用到手写涂鸦的功能,然后可以将涂鸦的结果保存为图片,并可以将"真迹"通过网络发送给对方.这种手写涂鸦功能是如何实现的了?最直接的,我们可以使用Windows提 ...

  8. java基础(1)

    class test { static { a=3; //System.out.println(a); } static int a = 1; String b = "ff"; p ...

  9. ubuntu下打开chm文件

    CHM文件格式是微软1998年推出的基于HTML文件特性的帮助文件系统,以替代早先的WinHelp帮助系统,它在Windows 98中把CHM类型文件称作“已编译的HTML帮助文件”. chm文件因为 ...

  10. Python 学习笔记5

    Life is like a box of chocolate. 今天继续学习Python数据结构. http://www.pythondoc.com/pythontutorial3/datastru ...