标签:style blog http color strong 数据
直接说题意,完全背包定义有N种物品和一个容量为V的背包,每种物品都有无限件可用。第i种物品的体积是c,价值是w。求解将哪些物品装入背包可使这些物品的体积总和不超过背包容量,且价值总和最大。本题要求是背包恰好装满背包时,求出最大价值总和是多少。如果不能恰好装满背包,输出NO
2 1 5 2 2 2 5 2 2 5 1
NO 1
解题:RT
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <climits> 7 #include <algorithm> 8 #include <cmath> 9 #define LL long long 10 using namespace std; 11 const int INF = INT_MAX>>2; 12 int c[2001],w[2001],dp[50001]; 13 int main(){ 14 int kase,n,i,j,v,k; 15 scanf("%d",&kase); 16 while(kase--){ 17 scanf("%d %d",&n,&v); 18 for(i = 1; i <= n; i++) 19 scanf("%d %d",c+i,w+i); 20 for(i = 0; i <= v; i++) 21 dp[i] = -INF; 22 dp[0] = 0; 23 for(i = 1; i <= n; i++){ 24 for(j = c[i]; j <= v; j++) 25 if(dp[j] < dp[j-c[i]]+w[i]) dp[j] = dp[j-c[i]]+w[i]; 26 } 27 if(dp[v] > 0){ 28 printf("%d\n",dp[v]); 29 }else puts("NO"); 30 } 31 return 0; 32 }
标签:style blog http color strong 数据
原文地址:http://www.cnblogs.com/crackpotisback/p/3833968.html