View Code of Problem 3195

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cctype>
#include<map>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 20000;
int dp[maxn];
int volume[maxn], value[maxn], c[maxn];
int n, v; // 总物品数,背包容量量
//val 最大容量,val 总价值,amount 数量
// 01 背包
void ZeroOnepark(int val, int vol) {
	for (int j = v ; j >= vol; j--)
		dp[j] = max(dp[j], dp[j - vol] + val);
}

int main(){
	while(~scanf("%d %d",&n,&v)){
		memset(dp,0,sizeof(dp));
		for(int i=1;i<=n;i++){
			scanf("%d %d",&volume[i],&value[i]);
		}
		
		for(int i=1;i<=n;i++){
			ZeroOnepark(value[i],volume[i]);
		}
		printf("%d",dp[v]);
	}
	
	return 0;
}

Double click to view unformatted code.


Back to problem 3195