forked from githubToLearn/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiple knapsack.cpp
More file actions
47 lines (42 loc) · 834 Bytes
/
Multiple knapsack.cpp
File metadata and controls
47 lines (42 loc) · 834 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<stdio.h>
using namespace std;
#define Inf 0x3f3f3f3f
#define N 10005
struct node{
int value;
int volumn;
}g[N];
int dp[N];
int main(){
int v,m;
cin>>v>>m;
memset(dp,0,sizeof(dp));
memset(g,0,sizeof(g));
int cnt = 0;
for(int i=0;i<m;i++){
int temp = 1;
int num,volumn,value;
cin>>num>>volumn>>value;
while(num-temp>0){//进行拆分
g[cnt].volumn = temp*volumn;
g[cnt].value = temp*value;
cnt++;
num = num - temp;
temp = temp*2;
}
g[cnt].volumn = num*volumn;
g[cnt].value = num*value;
cnt++;
}
for(int i=0;i<cnt;i++){//按0-1背包进行求解
for(int j=v;j>=g[i].volumn;j--){
dp[j] = max(dp[j],dp[j-g[i].volumn]+g[i].value);
}
}
cout<<dp[v]<<endl;
return 0;
}