当前位置:网站首页>动态规划 01背包
动态规划 01背包
2022-08-01 03:24:00 【lucky九年】
01背包相信小伙伴们都清楚。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
int all , n, w[105], v[105], ans[105][1000005];
int main() {
cin >> all >> n;
for (int i = 1; i <= n; i++) {
cin >> w[i] >> v[i];
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= all; j++) {
if (j < w[i]) {
ans[i][j] = ans[i - 1][j];
} else {
ans[i][j] = max(ans[i- 1][j], ans[i - 1][j - w[i]] + v[i]);
}
}
}
cout << ans[n][all] << endl;
return 0;
}
//int main() {
// ios::sync_with_stdio(false); //虽然ios::sync_with_stdio对cin有加速所用,但是还是没有scanf的速度快。
// cin >> all >> n;
// for (int i = 1; i <= n; i++) {
// cin >> w[i] >> v[i];
// }
// for (int i = 1; i <= n; i++ ){
// for (int j = all; j <= w[i]; j--) {
// ans[j] = max(ans[j], v[i] + ans[j - w[i]]);
// }
// }
// cout << ans[all] << endl;
// return 0;
//}
边栏推荐
- 初出茅庐的小李第114篇博客项目笔记之机智云智能浇花器实战(3)-基础Demo实现
- <JDBC> 批量插入 的四种实现方式:你真的get到了吗?
- [Getting Started Tutorial] Rollup Module Packager Integration
- "Youth Pie 2": The new boyfriend stepped on two boats, and the relationship between Lin Miaomiao and Qian Sanyi warmed up
- Character encoding and floating point calculation precision loss problem
- 软考高级系统架构设计师系列之:信息系统基础知识
- lua entry case combat 123DIY
- Raspberry pie arm version of GCC installed configuration and environment variables
- 软件测试面试(三)
- Handwritten binary search tree and test
猜你喜欢
随机推荐
device node结构体转换成platform_device结构体
每周小结(*67):为什么不敢发表观点
设备树——dtb格式到struct device node结构体的转换
MySQL3
Game Security 03: A Simple Explanation of Buffer Overflow Attacks
从设备树(dtb格式数据)中解析出bootargs
使用ts-node报错
【消息通知】用公众号模板消息怎么样?
链式编程、包、访问权限
The fledgling Xiao Li's 113th blog project notes: Wisdom cloud smart flower watering device combat (2) - basic Demo implementation
Four implementations of
batch insert: have you really got it? IDEA调试
修改Postman安装路径
Unity在BuildIn渲染管线下实现PlanarReflection的初级方法
IDEA无法识别module(module右下角没有蓝色小方块)
更换树莓派内核
The IDEA can't find or unable to load The main class or Module "*" must not contain The source root "*" The root already belongs to The Module "*"
软件测试面试(三)
MySQL4
测试