[POJ_3624] 0-1背包 题解
来源:互联网 发布:苹果7怎么快速打开数据 编辑:程序博客网 时间:2024/05/03 21:02
本人半路出家 算法从未规范的学习过,前几天在学习DP的时候,同伴惊呼:你竟然不知道背包,此乃dp基础......
闲来无事,学学背包也好~
废话少说,题目如下:
Description
Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible from the N (1 ≤ N ≤ 3,402) available charms. Each charm i in the supplied list has a weight Wi (1 ≤ Wi ≤ 400), a 'desirability' factor Di (1 ≤ Di ≤ 100), and can be used at most once. Bessie can only support a charm bracelet whose weight is no more than M (1 ≤ M ≤ 12,880).
Given that weight limit as a constraint and a list of the charms with their weights and desirability rating, deduce the maximum possible sum of ratings.
Input
* Line 1: Two space-separated integers: N and M
* Lines 2..N+1: Line i+1 describes charm i with two space-separated integers: Wi and Di
Output
* Line 1: A single integer that is the greatest sum of charm desirabilities that can be achieved given the weight constraints
Sample Input
4 61 42 63 122 7
Sample Output
23
题意:现在有N颗宝石,每颗宝石都有自己的重量W以及自己的价值D,现在我们来串手链,已知手链所能承受的最大重量为M,求得在手链能承受的最大重量之下,手链的最大价值。此题是典型的0-1背包。
给出状态转移方程: dp[j]=max(dp[j], dp[j - w[i]]+D[i]);
因为手链的最大价值是不确定的,因此,dp的赋值为0
现给出AC代码
#include <iostream>#include<string.h>#include<cstdio>using namespace std;const int MAX = 3500;const int Max = 13000;int dp[13000];int M[3500];int D[13000];int N,M1;int main(){ int ans = -1;//赋值为负数 memset(dp,0,sizeof(dp)); scanf("%d%d",&N,&M1); for(int i = 1; i <= N; i++) { scanf("%d%d",&M[i],&D[i]); } for(int i = 1; i <= N; i++) { for(int j = M1; j >= M[i]; j--) { dp[j]=max(dp[j],dp[j-M[i]]+D[i]); ans = max(ans,dp[j]); } } printf("%d\n",ans); return 0;}
- [POJ_3624] 0-1背包 题解
- poj_3624 01背包
- poj_3624
- POJ_3624
- HDU3448Bag Problem题解背包
- hud1114 完全背包 题解
- hdu2602 01背包 题解
- 背包问题题解
- HDU 01背包题解
- 背包问题(0-1背包、完全背包、多重背包)详解
- 0-1背包----完全背包
- 0-1背包+分数背包
- 【DP】【01背包】【完全背包】Birthday 题解
- HDU2062题解 01背包而已
- 01背包问题—题解
- 【BZOJ】【P4247】【挂饰】【题解】【背包】
- hdu2063investment题解(完全背包)
- 【JZOJ4916】完全背包问题 题解
- WHAT ARE TRAINING SET, VALIDATION SET AND TEST SET?
- javaweb开发之servlet
- java 转 c# 的6款最佳工具
- 替代B树成为一些Nosql采用的数据结构
- 用户管理
- [POJ_3624] 0-1背包 题解
- sql运算符
- jquery ajax 的success回调函数不执行
- EXT+struts2实现文件的预览和下载
- select和select1
- 婚姻不易,且行且珍惜
- opencv学习日记之在算法设计中使用策略(strategy)模式(即:使用面向对象类编程)
- "flush shared_pool" "version_count过高引起的bug"
- 做响应式网站底部toolbar的设计