动态规划-01背包(2)
来源:互联网 发布:竞价账户优化ppt 编辑:程序博客网 时间:2024/06/09 15:46
解题:poj3624
解题之前,进行反思,以后的程序不用cin和cout,打自己脸一下,(▔ε(#▔)☆╰╮(▔▽▔///)
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 theN (1 ≤N ≤ 3,402) available charms. Each charmi in the supplied list has a weightWi (1 ≤Wi ≤ 400), a 'desirability' factorDi (1 ≤Di ≤ 100), and can be used at most once. Bessie can only support a charm bracelet whose weight is no more thanM (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 andM
* Lines 2..N+1: Line i+1 describes charm i with two space-separated integers:Wi andDi
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
我要说,打死都不给你们翻译_(:з」∠)_
贴代码:
#include <stdio.h>#include <string.h>int d[3500], w[3500], n, W;int val[13500] = {0};int knapsack(){ int i,j; for(i=1; i<=n; i++) for(j=W; j>=1; j--){ if(j>=w[i] && val[j-w[i]]+d[i]>val[j]) val[j] = val[j-w[i]] + d[i]; } return 0;}int main(){ int i; while(scanf("%d %d", &n, &W) != EOF) { for(i=1; i<=n; i++) scanf("%d %d",&w[i],&d[i]); knapsack(); printf("%d\n", val[W]); } return 0;}
这个题的数据不要用二维数组,用一维数组,就这一点需要注意的地方XD
- 动态规划-01背包(2)
- 动态规划--01背包问题(2)
- (动态规划)01背包
- 动态规划总结(01背包 完全背包 多重背包)
- 动态规划初步( 01 背包、完全背包、多重背包)
- 动态规划背包算法(01背包和完全背包)
- 动态规划 01背包
- 01背包-动态规划
- 01背包动态规划
- 01背包 动态规划
- 01背包动态规划
- 01背包 动态规划
- 动态规划01背包
- 动态规划01背包
- 动态规划01背包
- 动态规划-01背包
- 动态规划:01背包
- 动态规划 01背包
- ”ImportError: No module named t2“---python的import机制+模块的搜索路径
- 关于hibernate的缓存使用
- python 属性包装之property.
- 尼康D7100配什么微距镜头比较好?
- C语言正则表达式使用详解
- 动态规划-01背包(2)
- NameNode 与 SecondaryNameNode 的关系
- ZOJ 1008 Gnome Tetravex
- hdu1716排序(挺不错)
- 【leetcode SQL】Employees Earning More Than Their Managers
- 冒泡排序
- java基础:final
- pip常用命令
- 比赛5(并查集)