Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature:Update 0056.携带矿石资源.md C++题解 #149

Open
DIDA-lJ opened this issue Nov 23, 2023 · 0 comments
Open

feature:Update 0056.携带矿石资源.md C++题解 #149

DIDA-lJ opened this issue Nov 23, 2023 · 0 comments

Comments

@DIDA-lJ
Copy link
Contributor

DIDA-lJ commented Nov 23, 2023

#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
int c,n; // 容量 和 种类数量 
void chose(){
	vector<int>weight(n);
	vector<int>value(n);
	vector<int>nums(n);
	for(int i = 0;i < n;i++){
		cin >> weight[i];
	}
	for(int i = 0;i < n;i++){
		cin >> value[i];
	}
	for(int i = 0;i < n;i++){
		cin >> nums[i];
	}

	//标准的01背包
	vector<int>dp(c+1,0);
	for(int i = 0;i < n;i++){
		for(int j = c;j >= weight[i];j--){
			for(int k = 0;k <= nums[i] && (j - k*weight[i]) >= 0;k++){
				dp[j] = max(dp[j], dp[j-k*weight[i]] + k*value[i]);
			}
			
		}
	} 
	cout << dp[c] << endl;
}
 

int main(){
	cin >> c >> n;
	chose();
	return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant