hdu 4501(背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4501
思路:dp[l][i][j][p]表示选前l件时花费i元,积分j,免费p时最大价值
View Code
1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 const int N=104; 5 using namespace std; 6 7 struct Node{ 8 int price; 9 int score; 10 int value; 11 }node[N]; 12 int dp[N][N][N][6];//表示选前l件时花费i元,积分j,免费p时最大价值// 13 14 15 int main(){ 16 int n,v1,v2,k; 17 while(~scanf("%d%d%d%d",&n,&v1,&v2,&k)){ 18 int ans=0; 19 for(int i=1;i<=n;i++){ 20 scanf("%d%d%d",&node[i].price,&node[i].score,&node[i].value); 21 } 22 memset(dp,0,sizeof(dp)); 23 for(int l=1;l<=n;l++){ 24 for(int i=0;i<=v1;i++){ 25 for(int j=0;j<=v2;j++){ 26 for(int p=0;p<=k;p++){ 27 dp[l][i][j][p]=dp[l-1][i][j][p]; 28 if(i-node[l].price>=0){ 29 dp[l][i][j][p]=max(dp[l][i][j][p],dp[l-1][i-node[l].price][j][p]+node[l].value); 30 } 31 if(j-node[l].score>=0){ 32 dp[l][i][j][p]=max(dp[l][i][j][p],dp[l-1][i][j-node[l].score][p]+node[l].value); 33 } 34 if(p>0)dp[l][i][j][p]=max(dp[l][i][j][p],dp[l-1][i][j][p-1]+node[l].value); 35 ans=max(ans,dp[l][i][j][p]); 36 } 37 } 38 } 39 } 40 printf("%d\n",ans); 41 } 42 return 0; 43 }