多维费用背包
1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 5 using namespace std; 6 7 struct good { 8 int a,b,val; 9 }; 10 11 int main(){ 12 int n,v1,v2,k; 13 while(cin >> n>>v1>>v2>>k){ 14 vector<good> commodity(n); 15 for(int i = 0; i < n; i ++ ) 16 cin >> commodity[i].a>>commodity[i].b>>commodity[i].val; 17 int dp[105][105][6] ={0}; 18 for(int i = 0; i <n; i ++ ){ 19 for(int j = v1; j >= 0; j-- ){ 20 for(int s = v2; s >= 0; s --){ 21 for(int d = k; d >= 0; d -- ){ 22 int temp = 0; 23 if(j >= commodity[i].a) 24 temp = max(temp,dp[j-commodity[i].a ][s][d] + commodity[i].val); 25 if(s >= commodity[i].b) 26 temp = max(temp,dp[j][ s-commodity[i].b ][d] + commodity[i].val); 27 if(d >= 1) 28 temp = max(temp,dp[j][s][ d-1 ] + commodity[i].val); 29 dp[j][s][d] = max(temp,dp[j][s][d]); 30 } 31 } 32 } 33 } 34 cout<<dp[v1][v2][k]<<endl; 35 } 36 return 0; 37 }