hdu 2126 Buy the souvenirs(二维0/1背包)
来源:互联网 发布:itunes12怎么更新软件 编辑:程序博客网 时间:2024/04/30 03:32
点击打开链接hdu2126
思路: 二维0/1背包
分析:
1 题目给定n个物品的价钱和m的钱,问最多能买到的物品数有几种方案。
2 很明显就可以写出状态转移方程dp[i][j][k]表示的是前i个物品选j个总价钱为k的方案数
那么dp[i][j][k] = dp[i-1][j][k]+dp[i-1][j-1][k-v[i]]。由于都可以把第一维去掉,所以正常的情况下直接写出dp[j][k] = dp[j][k] + dp[j-1][k-v[i]]
代码:
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int N = 35;const int MAXN = 510;int n , m , v[N];int dp[N][MAXN];void solve(){ memset(dp , 0 , sizeof(dp)); dp[0][0] = 1; for(int i = 1 ; i <= n ; i++){ for(int j = n ; j >= 1 ; j--){ for(int k = m ; k >= v[i] ; k--) dp[j][k] += dp[j-1][k-v[i]]; } } int ans; for(int j = n ; j >= 1 ; j--){ ans = 0; for(int k = m ; k >= 0 ; k--) ans += dp[j][k]; if(ans){ printf("You have %d selection(s) to buy with %d kind(s) of souvenirs.\n" , ans , j); return; } } printf("Sorry, you can't buy anything.\n");}int main(){ int Case; scanf("%d" , &Case); while(Case--){ scanf("%d%d" , &n , &m); for(int i = 1 ; i <= n ; i++) scanf("%d" , &v[i]); solve(); } return 0;}
- hdu 2126 Buy the souvenirs(二维0/1背包)
- HDU 2126 Buy the souvenirs (dp 二维01背包)
- Buy the souvenirs - HDU 2126 背包dp
- HDU 2126 - Buy the souvenirs(01背包)
- hdu 2126 Buy the souvenirs 01背包
- HDU 2126 Buy the souvenirs (贪心&0-1背包最优解的个数)
- HDU 2126 Buy the souvenirs(DP:01背包)
- 01背包变形 之 hdu 2126 Buy the souvenirs
- HDU 2126 Buy the souvenirs 01背包 输出方案数
- HDU 2126 Buy the souvenirs (01背包)
- hdu 2126 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
- hdu 2126 Buy the souvenirs
- java中的null的剖析
- Java多线程发展简史
- 一致性hash算法及其在分布式系统中的应用
- 537 - Artificial Intelligence?
- List of Useful Libraries for iOS Projects
- hdu 2126 Buy the souvenirs(二维0/1背包)
- Command模式实例(C#)
- (转自cocoachina)关于iOS 7 设计师需要知道的一些事
- 嵌入式Linux系统基础知识
- android平台调度工作流开发
- (转自cocoachina)iOS7新特征汇总[01]:UI和多任务增强
- 数据库相关概念
- iBATIS缓存cacheModel属性浅析
- 嵌入式Linux系统的图形用户界面