当前位置:网站首页>Knapsack problem acwing 9 Group knapsack problem
Knapsack problem acwing 9 Group knapsack problem
2022-07-05 06:24:00 【T_ Y_ F666】
knapsack problem AcWing 9. Group knapsack problem
Original link
AcWing 9. Group knapsack problem
Algorithm tags
knapsack problem DP
Ideas
Code
#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 Group items
rep(i, 1, n+1){
// The remaining capacity of the backpack Because items can only be used once So cycle from large to small
Rep(j, vv, 0){
// The first i Select an item in the group to maximize the value
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;
}
Originality is not easy.
Reprint please indicate the source
If it helps you Don't forget to praise and support
边栏推荐
- 博弈论 AcWing 894. 拆分-Nim游戏
- Leetcode-6111: spiral matrix IV
- Leetcode array operation
- [rust notes] 17 concurrent (Part 2)
- 中国剩余定理 AcWing 204. 表达整数的奇怪方式
- 【LeetCode】Day95-有效的数独&矩阵置零
- 阿里新成员「瓴羊」正式亮相,由阿里副总裁朋新宇带队,集结多个核心部门技术团队
- Traditional databases are gradually "difficult to adapt", and cloud native databases stand out
- MySQL advanced part 1: View
- Leetcode divide and conquer / dichotomy
猜你喜欢
4. Object mapping Mapster
How to make water ripple effect? This wave of water ripple effect pulls full of retro feeling
1.14 - assembly line
4.Oracle-重做日志文件管理
[2020]GRAF: Generative Radiance Fields for 3D-Aware Image Synthesis
MySQL advanced part 2: SQL optimization
MySQL advanced part 1: View
Liunx starts redis
高斯消元 AcWing 884. 高斯消元解异或線性方程組
2.Oracle-数据文件的添加及管理
随机推荐
2048项目实现
区间问题 AcWing 906. 区间分组
Real time clock (RTC)
WordPress switches the page, and the domain name changes back to the IP address
New title of module a of "PanYun Cup" secondary vocational network security skills competition
Leetcode-6108: decrypt messages
5.Oracle-錶空間
Leetcode heap correlation
Alibaba established the enterprise digital intelligence service company "Lingyang" to focus on enterprise digital growth
在新线程中使用Handler
MySQL advanced part 2: the use of indexes
MySQL advanced part 1: index
LeetCode-61
MPLS experiment
Single chip computer engineering experience - layered idea
Leetcode recursion
Leetcode-9: palindromes
4.Oracle-重做日志文件管理
Applicable to Net free barcode API [off] - free barcode API for NET [closed]
2.Oracle-数据文件的添加及管理