当前位置:网站首页>背包问题 AcWing 9. 分组背包问题
背包问题 AcWing 9. 分组背包问题
2022-07-27 10:35: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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
猜你喜欢

Compete for the key battle of stock users and help enterprises build a perfect labeling system - 01 live review

最长上升子序列模型 AcWing 1017. 怪盗基德的滑翔翼

Opengauss kernel analysis - statistics and row count estimation

c语言指针函数和函数指针的辨析

Budweiser, a well-known beer, plans to launch NFT in an attempt to unveil the "long planned" uplink?

熵与形态的非递进现象

Local and overall differences between emergence and morphology

Play with the cluster configuration center and learn about the Taier console

最长上升子序列模型 AcWing 482. 合唱队形

How to build a data index system is the most effective. It will take you a quick start from 0 to 1
随机推荐
Introduction to software vulnerability analysis (I)
The second method of calculating overlapping integral
Overview of radar communication integrated waveform design
C language 2: find the maximum value of three numbers, find the middle value of three numbers, and write program steps
6 find the smallest letter larger than the target letter
How to build a data index system is the most effective. It will take you a quick start from 0 to 1
Analysis of new communication security risks brought by quantum computer and Countermeasures
14 check whether integers and their multiples exist
Object array de duplication
Li Hongyi_ Machine learning_ Assignment 4 (detailed explanation)_ HW4 Classify the speakers
Problems and Countermeasures of minors' digital security protection
Self optimization of wireless cell load balancing based on machine learning technology
[QNX hypervisor 2.2 user manual]9.9 logger
MIMO array 3D imaging technology based on mobile terminal
神经网络学习笔记
2022牛客多校训练(3)A-Ancestor 题目翻译
Description and feelings
The longest ascending subsequence model acwing 1017. The glider wing of the strange thief Kidd
2022牛客多校 (3)J.Journey
正则form表单判断