标签:
Description
Input
Output
Sample Input
Sample Output
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int main() { int dp[550][2]; int T; int w[550]; int n,m; scanf("%d",&T); while(T--){ scanf("%d%d",&n,&m); for(int i = 1; i <= n ; i++) scanf("%d",&w[i]); memset(dp,0,sizeof(dp)); for(int i = 0 ; i <= m ; i++) dp[i][1] = 1; for(int i = 1; i <= n ; i++){ for(int j = m ; j >= w[i]; j--){ if(dp[j][0] == dp[j-w[i]][0] + 1){//dp[j][0]指用了j钱时的买的礼物数目,如果用j钱买的礼物数目和用不买i物品时所用的钱的买的礼物数目差1,说明用j钱买的礼物的种类就是这两者之和 dp[j][1] = dp[j][1] + dp[j-w[i]][1];// } else if(dp[j][0] < dp[j-w[i]][0] + 1){//如果不大于,说明dp[j-w[i]][0]优于dp[j][0] dp[j][1] = dp[j-w[i]][1]; dp[j][0] = dp[j-w[i]][0] + 1; } } } if(dp[m][0]) printf("You have %d selection(s) to buy with %d kind(s) of souvenirs.\n",dp[m][1],dp[m][0]); else printf("Sorry, you can‘t buy anything.\n"); } return 0; }
HDU2126——背包DP(开状态)——Buy the souvenirs
标签:
原文地址:http://www.cnblogs.com/zero-begin/p/4457732.html