当前位置:网站首页>背包问题 AcWing 9. 分组背包问题
背包问题 AcWing 9. 分组背包问题
2022-07-05 06:16:00 【T_Y_F666】
背包问题 AcWing 9. 分组背包问题
原题链接
算法标签
背包问题 DP
思路
代码
#include<bits/stdc++.h>
#define int long long
#define rep(i, a, b) for(int i=a;i<b;++i)
#define Rep(i, a, b) for(int i=a;i>b;--i)
using namespace std;
const int N = 105;
int s[N];
int v[N][N], w[N][N],f[N];
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void put(int x) {
if(x<0) putchar('-'),x=-x;
if(x>=10) put(x/10);
putchar(x%10^48);
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n=read(), vv=read();
rep(i, 1, n+1){
s[i]=read();
rep(j, 0, s[i]){
v[i][j]=read(), w[i][j]=read();
}
}
// n组物品
rep(i, 1, n+1){
// 背包剩余容量 由于物品只能使用一次 因此从大到小循环
Rep(j, vv, 0){
// 第i组中选取一个物品使价值最大
rep(k, 0, s[i]){
if(v[i][k]<=j){
f[j]=max(f[j], f[j-v[i][k]]+w[i][k]);
}
}
}
}
printf("%lld", f[vv]);
return 0;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- Appium automation test foundation - Summary of appium test environment construction
- QT判断界面当前点击的按钮和当前鼠标坐标
- MySQL advanced part 2: the use of indexes
- 1.15 - 输入输出系统
- LeetCode 1200.最小绝对差
- 传统数据库逐渐“难适应”,云原生数据库脱颖而出
- C Primer Plus Chapter 15 (bit operation)
- 【Rust 笔记】14-集合(下)
- SQLMAP使用教程(二)实战技巧一
- Multi screen computer screenshots will cut off multiple screens, not only the current screen
猜你喜欢
MySQL advanced part 2: optimizing SQL steps
SQL三种连接:内连接、外连接、交叉连接
LeetCode-54
Data visualization chart summary (I)
MySQL advanced part 1: View
Leetcode-6110: number of incremental paths in the grid graph
SQLMAP使用教程(一)
Overview of variable resistors - structure, operation and different applications
阿里新成员「瓴羊」正式亮相,由阿里副总裁朋新宇带队,集结多个核心部门技术团队
Doing SQL performance optimization is really eye-catching
随机推荐
leetcode-3:无重复字符的最长子串
Is it impossible for lamda to wake up?
MySQL advanced part 2: MySQL architecture
leetcode-6110:网格图中递增路径的数目
Leetcode-9: palindromes
Liunx starts redis
QQ computer version cancels escape character input expression
【Rust 笔记】16-输入与输出(上)
Appium自动化测试基础 — Appium测试环境搭建总结
1.15 - 输入输出系统
高斯消元 AcWing 884. 高斯消元解异或线性方程组
RGB LED infinite mirror controlled by Arduino
LeetCode 0107.二叉树的层序遍历II - 另一种方法
One question per day 1447 Simplest fraction
阿里巴巴成立企业数智服务公司“瓴羊”,聚焦企业数字化增长
【Rust 笔记】17-并发(上)
2021apmcm post game Summary - edge detection
Quickly use Amazon memorydb and build your own redis memory database
Appium foundation - use the first demo of appium
Matrixdb V4.5.0 was launched with a new mars2 storage engine!