当前位置: 首页 > news >正文

太原北京网站建设建设培训考试服务网站

太原北京网站建设,建设培训考试服务网站,网络推广培训机构排名,万网域名价格有依赖的背包是指多个物品变成一个复合物品(互斥),每件复合物品不要和怎么要多种可能性展开。时间复杂度O(物品个数 * 背包容量),额外空间复杂度O(背包容量)。 下面通过题目加深理解。 题目一 测试链接:[NOIP2006 提…

有依赖的背包是指多个物品变成一个复合物品(互斥),每件复合物品不要和怎么要多种可能性展开。时间复杂度O(物品个数 * 背包容量),额外空间复杂度O(背包容量)。

下面通过题目加深理解。

题目一

测试链接:[NOIP2006 提高组] 金明的预算方案 - 洛谷

分析:对于这道题,可以参考01背包是对每个物品进行可能性的展开,有依赖的背包是对主件进行可能性的展开,所以可能性就比01背包的展开多。对于一个没有附件的主件可能性的展开,就是01背包的展开,即选或不选主件。对于有一个附件的主件可能性的展开,就有三种,选主件、不选主件、主件和附件一起选。对于有两个附件的主件可能性的展开,就有五种,选主件、不选主件、主件和第一个附件一起选、主件和第二个附件一起选、主件和两个附件一起选。对于输入,代码中采用了几个数组结构存储信息,cost数组存储花费代价,value数组存储收益,king数组存储是否是主件,fans数组存储主件有多少个附件,follows数组存储每个主件拥有的附件。下面代码采用计划搜索,并没有去做空间压缩,代码如下。

#include <iostream>
#include <vector>
using namespace std;
int n, m;
int cost[61];
int value[61];
bool king[61];
int fans[61] = {0};
vector<vector<int>> follows;
int dp[61][32001];
int f(int index, int money){if(index == m+1){return 0;}if(dp[index][money] != -1){return dp[index][money];}if(!king[index]){return f(index+1, money);}int ans = f(index+1, money);if(money - cost[index] >= 0){ans = ans > f(index+1, money-cost[index]) + value[index] ?ans : f(index+1, money-cost[index]) + value[index];}if(fans[index] >= 1 && money - cost[index] - cost[follows[index][0]] >= 0){ans = ans > f(index+1, money-cost[index]-cost[follows[index][0]]) + value[index] + value[follows[index][0]] ?ans : f(index+1, money-cost[index]-cost[follows[index][0]]) + value[index] + value[follows[index][0]];}if(fans[index] == 2){if(money - cost[index] - cost[follows[index][1]] >= 0){ans = ans > f(index+1, money-cost[index]-cost[follows[index][1]]) + value[index] + value[follows[index][1]] ?ans : f(index+1, money-cost[index]-cost[follows[index][1]]) + value[index] + value[follows[index][1]];}if(money - cost[index] - cost[follows[index][0]] - cost[follows[index][1]] >= 0){ans = ans > f(index+1, money-cost[index]-cost[follows[index][0]]-cost[follows[index][1]]) + value[index] + value[follows[index][0]] + value[follows[index][1]] ?ans : f(index+1, money-cost[index]-cost[follows[index][0]]-cost[follows[index][1]]) + value[index] + value[follows[index][0]] + value[follows[index][1]];}}dp[index][money] = ans;return ans;
}
int main(void){int v, p, q;scanf("%d%d", &n, &m);vector<int> temp;for(int i = 0;i <= m;++i){follows.push_back(temp);}for(int i = 1;i <= m;++i){scanf("%d%d%d", &v, &p, &q);cost[i] = v;value[i] = v * p;if(q != 0){king[i] = false;fans[q]++;follows[q].push_back(i);}else{king[i] = true;}}for(int i = 1;i < 61;++i){for(int j = 1;j < 32001;++j){dp[i][j] = -1;}}printf("%d", f(1, n));return 0;
}

http://www.yayakq.cn/news/734128/

相关文章:

  • 建设的电影网站总是无法连接网站云空间大小
  • 住房与城乡建设部违法举报网站wordpress 国定链接
  • 展示用网站模板南京百度网站制作
  • 手机网站制作良心服务平阳网站建设公司
  • 百度收录网站之后又怎么做百度网盘怎么用
  • 建议网站的方案wordpress 分类置顶
  • 欧普建站上市公司查询网站
  • 做单页网站需要做什么科技公司很多都是骗子公司吗
  • 河北唐山建设工程协会网站门户网站建设工作
  • 万年历网站做市场监督管理局投诉电话是多少
  • 免费制作微信小程序的网站网站设计济南
  • 搭建网站实时访问地图中国施工企业协会官网
  • 图片预览网站 末班网站开发推广方案策划书
  • 网址如何备案石家庄百度seo代理
  • 男女做那个暖暖网站深圳做网站推广的公司
  • 湖南网站建设优化iis7架设网站教程
  • 分析网站结构想让客户公司做网站的话语
  • 宁夏银川网站建设深圳外贸建站模板
  • 门户网站建设进一步提升邯郸网站设计应搜韦欣cidun8上词
  • led 网站模板江苏智能网站建设流程
  • 四川德立胜建设工程有限公司网站网站制作全包多少钱
  • 电子政务 和网站建设总结松山湖网站建设
  • 网页导航网站设计免费制作图片带字 手机软件
  • 沈阳市有做网站的公司谁给我一个企业邮箱认证
  • 让你做一个旅游网站你会怎么做wordpress 前台上传
  • 网站关键字选择标准网站pv uv 多少算好站
  • 营销网站定制的优势wordpress两个域名访问不了
  • 做平台网站怎么赚钱网站系统
  • 快速提高网站关键词排名优化网站制作费用贵不贵
  • 权重的网站个人wordpress怎么赚钱