动态规划part06 完全背包理论基础 518. 零钱兑换 II 377. 组合总和 Ⅳ
完全背包理论基础
acm可运行代码(先遍历物品再遍历背包,一维dp)
#include<iostream>
#include<vector>
using namespace std;int Solution(vector<int>& weights,vector<int>& values,int V){vector<int> dp(V+1);for(int i = 0; i<weights.size();i++){for(int j = weights[i]; j<=V;j++){dp[j] = max(dp[j],dp[j-weights[i]]+values[i]);}}return dp[V];
}
int main(){int N,V;cin>>N>>V;vector<int> weights(N);vector<int> values(N);while(N--){int weight, value;cin>>weight>>value;weights.push_back(weight);values.push_back(value);}std::cout << Solution(weights,values,V) << std::endl;return 0;
}
先遍历背包再遍历物品,一维dp
#include<iostream>
#include<vector>
using namespace std;int Solution(vector<int>& weights,vector<int>& values,int V){vector<int> dp(V+1);for(int j = 0; j <=V;j++){for(int i = 0; i<weights.size();i++){if(j>=weights[i])dp[j] = max(dp[j],dp[j-weights[i]]+values[i]);}}return dp[V];
}
int main(){int N,V;cin>>N>>V;vector<int> weights(N);vector<int> values(N);while(N--){int weight, value;cin>>weight>>value;weights.push_back(weight);values.push_back(value);}std::cout << Solution(weights,values,V) << std::endl;return 0;
}
先遍历背包再遍历物品,二维dp
#include<iostream>
#include<vector>
using namespace std;int Solution(vector<int>& weights,vector<int>& values,int V){int kinds = weights.size();vector<vector<int>> dp(kinds,vector<int>(V+1,0));for(int i= weights[0];i<=V;i++){dp[0][i] = dp[0][i-weights[0]]+values[0];}for(int i=1;i<weights.size();i++){for(int j = 0; j<=V;j++){if(j>=weights[i]){dp[i][j] = max(dp[i-1][j],dp[i][j-weights[i]]+values[i]);}else{dp[i][j] = dp[i-1][j];}}}return dp[kinds-1][V];
}
int main(){int N,V;cin>>N>>V;vector<int> weights(N);vector<int> values(N);while(N--){int weight, value;cin>>weight>>value;weights.push_back(weight);values.push_back(value);}std::cout << Solution(weights,values,V) << std::endl;return 0;
}
518. 零钱兑换 II
必须先遍历物品,再遍历背包
class Solution {
public:int change(int amount, vector<int>& coins) {vector<int> dp(amount+1);dp[0] = 1;for(int i = 0; i<coins.size();i++){for(int j = coins[i]; j<=amount;j++){dp[j] += dp[j-coins[i]];}}return dp[amount];}
};
377. 组合总和 Ⅳ
必须先遍历背包,再遍历物品
class Solution {
public:int combinationSum4(vector<int>& nums, int target) {vector<int> dp(target+1);dp[0] =1;for(int j = 0; j <=target;j++){for(int i =0; i<nums.size();i++){if(j>=nums[i] && dp[j] < INT_MAX-dp[j-nums[i]]) dp[j] += dp[j-nums[i]];}}return dp[target];}
};