题目链接:https://vjudge.net/contest/125308#problem/M
题意:有N件物品和一个容量为V的背包。第i件物品的费用是体积c[i],价值是w[i]。求解将哪些物品装入背包可使价值总和最大。简单的01背包问题主要是状态转移式
f[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]},即可求解:
AC代码:
1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 using namespace std; 5 int dp[1005]; 6 struct Node 7 { 8 int volume; 9 int value; 10 } a[1005]; 11 int main() 12 { 13 int t1,i, j, t, m; 14 scanf("%d",&t1); 15 while(t1--) 16 { 17 scanf("%d%d", &t, &m); 18 memset(dp,0,sizeof(dp)); 19 for (i = 1; i <= t; i++) 20 scanf("%d",&a[i].value); 21 for(i = 1; i <= t; i ++) 22 scanf("%d",&a[i].volume); 23 for (i = 1; i <= t; i++) 24 { 25 for (j = m; j >= a[i].volume; j--) 26 { 27 dp[j] = max(dp[j], dp[j - a[i].volume] + a[i].value); 28 } 29 } 30 printf("%d\n", dp[m]); 31 } 32 return 0; 33 } 34 FAQ | About | Google Group | Discuss | Github | Author 35 All Copyright Reserved ©2010-2016 HUST ACM/ICPC TEAM