hdu2126——Buy the souvenirs——————【01背包记录物品最多时种数】
来源:互联网 发布:网络车管所 编辑:程序博客网 时间:2024/04/19 01:04
/**
解题思路:多加一维表示物品个数。dp[j][k]代表背包容量为j,物品个数为k时的组合种类数目。
*/
/*
题目大意:给你n种物品,有m块钱,然后给出n种物品的花费,问物品个数最多时的组合种数有多少。
*/
#include<stdio.h>#include<iostream>#include<string.h>using namespace std;#define maxn 550int dp[maxn][35],n,m,t,flag;int c[35];int main(){ int i,j,k; scanf("%d",&t); while(t--) { memset(c,0,sizeof(c)); memset(dp,0,sizeof(dp)); scanf("%d %d",&n,&m); flag=-1; int Min=0x3f3f3f3f; for(i=0; i<n; i++){ scanf("%d",&c[i]); if(Min>c[i]) Min=c[i]; } if(Min>m){ printf("Sorry, you can't buy anything.\n");continue; } dp[0][0]=1; //起点 for(i=0; i<n; i++) { for(j=m; j>=c[i]; j--) //花j元 { for(k=n; k>=1; k--) //买k种 { if(dp[j-c[i]][k-1]) { dp[j][k]+=dp[j-c[i]][k-1]; if(dp[j][k]!=0&&k>flag) flag=k; //计最大种数 } } } } int ans=0; //计组合数 for(i=1; i<=m; i++) { if(dp[i][flag]!=0) ans+=dp[i][flag]; } if(ans==0) printf("Sorry, you can't buy anything.\n"); else printf("You have %d selection(s) to buy with %d kind(s) of souvenirs.\n",ans,flag); } return 0;}
0 0
- hdu2126——Buy the souvenirs——————【01背包记录物品最多时种数】
- hdu2126 Buy the souvenirs (01)
- HDU2126:Buy the souvenirs(01)
- hdu2126 Buy the souvenirs 01背包变形 dp
- hdu2126---Buy the souvenirs(01背包方案数)
- hdu2126 Buy the souvenirs(01背包变化)
- 【01背包方案数】HDU2126-Buy the souvenirs
- hdu2126 Buy the souvenirs(01背包+路径)
- HDU2126:Buy the souvenirs(类01背包)
- HDU2126:Buy the souvenirs
- hdu2126 Buy the souvenirs
- 【HDU2126】【Buy the souvenirs】
- hdu2126Buy the souvenirs (01背包+记录路径的种数)
- HDU 2126 - Buy the souvenirs(01背包)
- hdu 2126 Buy the souvenirs 01背包
- HDU-2126 Buy the souvenirs (记录最优方案数的01背包问题 入门题)
- 动态规划——物品无限的背包问题
- 背包问题——“完全背包”详解及实现(包含背包具体物品的求解)
- hdu 1874 畅通工程续(最短路)
- 如何在Mac OS X上安装 Ruby运行环境
- [算法系列之九]Karatsuba快速相乘算法
- PHP官方Windows扩展列表
- 自定义日期
- hdu2126——Buy the souvenirs——————【01背包记录物品最多时种数】
- 加载依赖的jar包在命令行编译和运行java文件
- YT14-HDU-棋盘与骨牌块
- 2010山东ACM省赛 Hello World! Sdut2158
- Cocos2d-x3.3Final(4) ImageView常用成员函数(C++)
- oracle分页查询
- eclipse使用技巧1——工作空间导入配置文件
- Vector< Vector<Point> >
- hdu 2113 Secret Number (水题)