题目:2. 01背包问题
题解:
#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef pair<int ,int > PII;
typedef long long LL;
const int N=1e4+10;
const int mod=1e9;
int f[1010];
int main(){
int n,m;
cin>>n>>m;
for(int i=0;i<n;i++){
int v,w;
cin>>v>>w;
for(int j=m;j>=v;j--){
f[j]=max(f[j],f[j-v]+w);
}
}
cout<<f[m];
return 0;
}
题目:3. 完全背包问题
题解:
#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef pair<int ,int > PII;
typedef long long LL;
const int N=1e4+10;
const int mod=1e9;
int f[1010];
int main(){
int n,m;
cin>>n>>m;
for(int i=0;i<n;i++){
int v,w;
cin>>v>>w;
for(int j=v;j<=m;j++){
f[j]=max(f[j],f[j-v]+w);
}
}
cout<<f[m];
return 0;
}
题目:4. 多重背包问题 I
题解:
#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef pair<int ,int > PII;
typedef long long LL;
const int N=1e4+10;
const int mod=1e9;
int f[1010];
int main(){
int n,m;
cin>>n>>m;
for(int i=0;i<n;i++){
int v,w,s;
cin>>v>>w>>s;
for(int j=0;j<s;j++){
for(int k=m;k>=v;k--){
f[k]=max(f[k],f[k-v]+w);
}
}
}
cout<<f[m];
return 0;
}