HDU 2126 Buy the souvenirs (dp 二维01背包)
来源:互联网 发布:清镇市大数据云平台 编辑:程序博客网 时间:2024/04/26 06:09
题目: 点击打开链接
题目大意:
有n(0<n<=30)件物品,每件物品的价格是Pi,要用m(0<=m<=500)块钱去买这些物品,要求买尽量多数量的物品,问买最多数量的物品共有多少总方案?
思路:
这题还是比较容易想到的
f[i][j][k], 表示前i个物品,用费用j,买k个物品共有多少个方案
得到状态转移方程:
f[i][j][k] += f[i-1][j-c[i]][k-1];
初始化f[0][0][0] = 1
代码:
#include<iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<cstring>#include<vector>#define SQ(x) ((x)*(x))#define MP make_pairconst int INF = 0x3f3f3f3f;const double PI = acos(-1.0);typedef long long int64;using namespace std;const int MAXN = 32;int n, m;int w[MAXN];int f[510][MAXN];int main(){int nCase;scanf("%d", &nCase);while(nCase--){scanf("%d%d", &n, &m);for(int i=1; i<=n; ++i)scanf("%d", &w[i]);memset(f, 0, sizeof(f));f[0][0] = 1;for(int i=1; i<=n; ++i){for(int v=m; v>=w[i]; --v){for(int k=n; k>=1; --k){f[v][k] += f[v-w[i]][k-1];}}}bool flag=false;for(int i=n; i>=1; --i){int cnt = 0;for(int j=m; j>0; --j)cnt += f[j][i];if(cnt > 0){flag=true;printf("You have %d selection(s) to buy with %d kind(s) of souvenirs.\n", cnt, i);break;}}if(!flag) puts("Sorry, you can't buy anything.");}return 0;}
- HDU 2126 Buy the souvenirs (dp 二维01背包)
- Buy the souvenirs - HDU 2126 背包dp
- HDU 2126 Buy the souvenirs(DP:01背包)
- hdu 2126 Buy the souvenirs 01背包+计数dp
- HDU 2126 - Buy the souvenirs(01背包)
- hdu 2126 Buy the souvenirs 01背包
- hdu 2126 Buy the souvenirs(二维0/1背包)
- 01背包变形 之 hdu 2126 Buy the souvenirs
- HDU 2126 Buy the souvenirs 01背包 输出方案数
- HDU 2126 Buy the souvenirs (01背包)
- HDU-2126 Buy the souvenirs (DP)
- hdu2126 Buy the souvenirs 01背包变形 dp
- hdu 2126 Buy the souvenirs
- hdu 2126 Buy the souvenirs
- Buy the souvenirs(hdu(2126)
- HDU 2126 Buy the souvenirs
- HDU 2126 Buy the souvenirs
- HDU 2126 Buy the souvenirs
- 【求助帖】有谁会搭建时间轴网页项目
- 多线程小结(一)
- 百度地图API之ItemizedOverlay的使用(Android)
- 【总有一些东西要弄清】——说说面试时一系列的CSS问题
- subclipse中javahl库找不到的解决办法
- HDU 2126 Buy the souvenirs (dp 二维01背包)
- javascript 闭包的理解
- Yslow
- 代理
- UVA 10236 The Fibonacci Primes
- OpenGL入门学习[十一 03]bmp纹理
- WEB开发中需要注意哪些安全问题?
- HDOJ 1010 迷宫搜索 剪枝
- vim(七) 双显示器