hdu2126 Buy the souvenirs(01背包+路径)
来源:互联网 发布:ubuntu桌面图标消失 编辑:程序博客网 时间:2024/04/26 13:40
http://acm.hdu.edu.cn/showproblem.php?pid=2126
题意:给你n件纪念品,你有m元钱,然后给出每个纪念品的花费,求m元钱可以买到最多多少件纪念品以及这种方案的数量。
思路:求最优方案总数的路径用法。刚开始想着最后一层的dp值只要等于最优解的都是最优方案,还是我图样。能够成最优解的方案可能不止一个,而这个dp值无法代表能够成当前方案的方案数,这种方法完全行不通。那么就需要另外开一个数组保存路径。第一次遇到输出最优方案数的路径表示,dp过程中更新,学到了(貌似也只有学的份了)。
#include <stdio.h>#include <algorithm>#include <stdlib.h>#include <string.h>#include <iostream>using namespace std;typedef long long LL;const int N = 1005;const int INF = 0x3f3f3f3f;int dp[N], path[N];int main(){ // freopen("in.txt", "r", stdin); int t, V, n; int cost[N]; scanf("%d", &t); while(t--) { scanf("%d%d", &n, &V); memset(dp, 0, sizeof(dp));//未装满 fill(path, path+N, 1); for(int i = 1; i <= n; i++) scanf("%d", &cost[i]); for(int i = 1; i <= n; i++) for(int j = V; j >= cost[i]; j--) { if(dp[j-cost[i]]+1>dp[j]) { dp[j] = dp[j-cost[i]]+1; path[j] = path[j-cost[i]]; } else if(dp[j-cost[i]]+1==dp[j]) { path[j]+=path[j-cost[i]]; } // dp[j] = max(dp[j], dp[j-cost[i]]+1); } if(dp[V] != 0) printf("You have %d selection(s) to buy with %d kind(s) of souvenirs.\n", path[V], dp[V]); else printf("Sorry, you can't buy anything.\n"); } return 0;}
0 0
- hdu2126 Buy the souvenirs(01背包+路径)
- hdu2126 Buy the souvenirs(01背包变化)
- 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背包方案数)
- 【01背包方案数】HDU2126-Buy the souvenirs
- HDU2126:Buy the souvenirs
- hdu2126 Buy the souvenirs
- 【HDU2126】【Buy the souvenirs】
- hdu2126——Buy the souvenirs——————【01背包记录物品最多时种数】
- HDU 2126 Buy the souvenirs (01背包)
- Buy the souvenirs(方案数+01背包)
- HDU 2126 - Buy the souvenirs(01背包)
- hdu 2126 Buy the souvenirs 01背包
- HDU 2126 Buy the souvenirs (dp 二维01背包)
- HDU 2126 Buy the souvenirs(DP:01背包)
- 【书单】matlab 科学计算、数值分析以及数学物理问题
- Android UI布局与控件及API Guide学习(一)
- 从ng1到ng2的平滑升级
- 百度地图4.1_1开发教程(2)
- 姚虹波移动电商大胆预言
- hdu2126 Buy the souvenirs(01背包+路径)
- MRS,MSR指令详解
- CDN与缓存加速有什么区别?
- apue 第8章 进程控制
- 我的caffe学习之路<第三天>
- Wex5发送短信的两种方法分享
- java bigdecimal
- apue 第12章 线程控制
- 【C语言 每日一题】SOJ 1000 A-B