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

wordpress 搜索按钮持续优化疫情防控举措

wordpress 搜索按钮,持续优化疫情防控举措,百度上能收到的企业名称网站怎么做,参考消息电子版手机版一.简介 DP(动态规划)背包问题是一个经典的组合优化问题,通常用来解决资源分配的问题,如货物装载、投资组合优化等。问题的核心思想是在有限的资源约束下,选择一组物品以最大化某种价值指标,通常是总价值或…

一.简介

DP(动态规划)背包问题是一个经典的组合优化问题,通常用来解决资源分配的问题,如货物装载、投资组合优化等。问题的核心思想是在有限的资源约束下,选择一组物品以最大化某种价值指标,通常是总价值或总利润。


二.闫氏DP分析法


三.01背包

(1)概念

每个物品只有一个,要么选,要么不选

(2)状态转移方程

f[i][j] = max(f[i-1][j] , f[i-1][j-v]+w)

(3)经典例题

P1048 [NOIP2005 普及组] 采药 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

(4)代码

#include<bits/stdc++.h>
#define N 1010
using namespace std;
int f[N][N];
int w[N],c[N];
int bag,n;
int main(){scanf("%d%d",&bag,&n);for(int i=1;i<=n;i++) scanf("%d%d",&w[i],&c[i]);for(int i=1;i<=n;i++){for(int j=0;j<=bag;j++){f[i][j]=f[i-1][j];if(j>=w[i]){f[i][j]=max(f[i][j],f[i-1][j-w[i]]+c[i]);}}}printf("%d",f[n][bag]);return 0;
}

(5)滚动数组优化 

#include<bits/stdc++.h>
#define N 1010
using namespace std;
int f[N];
int n,m;
int main()
{scanf("%d%d", &m, &n);for(int i = 1; i <= n; i ++ ){int w,c;scanf("%d%d", &w, &c);for(int j = m; j >=w ; j -- ){ f[j] = max(f[j], f[j-w] + c);}}printf("%d", f[m]);return 0;
}


四.完全背包

(1)概念

每个物品有无限个

(2)例题

1023. 买书 - AcWing题库

(3)闫氏DP分析法

(4)状态转移方程推导

f[i][j] = f[i - 1][j] + f[i - 1][j - v * 1]  + f[i - 1][j - v * 2] + ...... +  f[i -1][j - v * k];

f[i][j - v] =            f[i - 1][j - v * 1]  + f[i - 1][j - v * 2] + ...... +  f[i - 1][j - v * k];

所以推出:f[i][j] = f[i - 1][j] + f[i][j - v];

再使用滚动数组简化,就得出结论,其实就是01背包,内循环从大到小变成了从小到大!

(5)参考代码

#include<bits/stdc++.h>
#define N 1010
using namespace std;
int f[N];
int n;
int a[5] = {0, 10, 20, 50, 100};int main(){scanf("%d", &n);f[0] = 1; for(int i = 1; i <= 4; i ++ ){for(int j = a[i]; j <= n; j ++ ){f[j] += f[j - a[i]];}}	printf("%d", f[n]);return 0;
}


五.多重背包

(1)概念

物品有一定的数量

(2)实现方式

1.朴素版(易)  2.二进制优化版(中)  3.单调队列优化版(难)

(3)经典例题

1019. 庆功会 - AcWing题库

(3)朴素版

#include<bits/stdc++.h>
#define N 6010
using namespace std;
int f[N];
int n, m;
int main(){scanf("%d%d", &n, &m); //n个物品,m容量 for(int i = 1;i <= n; i ++ ){int v, w, s; //容量,价值,数量 scanf("%d%d%d", &v, &w, &s);for(int j = m; j >= v; j -- ){ //枚举容量 for(int k = 1;k <= s && k * v <= j; k ++ ){ //再枚举数量 f[j] = max(f[j], f[j - k * v] + w * k);}}}printf("%d", f[m]);return 0;
}

 (4)二进制优化版

#include<bits/stdc++.h>
#define N 6010
using namespace std;
int f[N];
int n,m;
int main(){scanf("%d%d", &n, &m);for(int i = 1; i <= n ; i ++ ){int v, w, s;scanf("%d%d%d", &v, &w, &s);for(int k = 1; k <= s; k *= 2) //二进制分解{for(int j = m; j >= k * v; j --)f[j] = max(f[j], f[j - k * v] + k * w);s -= k;}if(s) //余下的{for(int j = m; j >= s * v; j --)f[j] = max(f[j], f[j - s * v] + s * w);}}printf("%d", f[m]);return 0;
}

六.分组背包

(1)概念

每组物品有若干个,同一组内的物品最多只能选一个。

和多重背包十分相似,也简单。难得写了,copy一下,哈哈。


七.混合背包

(1)概念

就是把完全背包,多重背包,01背包混合起来,分类讨论即可,代码很好看懂。

(2)例题

7. 混合背包问题 - AcWing题库

(3)参考代码

#include<bits/stdc++.h>
#define N 1010
using namespace std;
int f[N];
int n,m;
int main()
{scanf("%d%d", &n, &m);int v, w, s;for(int i = 1; i <= n; i ++ ){scanf("%d%d%d", &v, &w, &s);if(s == 0){for(int j = v; j <= m; j ++ )f[j] = max(f[j], f[j - v]);}else{if(s == -1) s = 1;for(int k = 1; k <= s; k *= 2){for(int j = m; j >= k * v; j --)f[j] = max(f[j], f[j - k * v] + k * w);s -= k;}if(s){for(int j = m; j >= s * v; j --)f[j] = max(f[j], f[j - s * v] + s * w);}}}printf("%d", f[m]);return 0;
}

八.有依赖的背包

(1)例题

10. 有依赖的背包问题 - AcWing题库

(2)参考代码

#include<iostream>
#include<vector>
using namespace std;
int f[110][110];//f[x][v]表达选择以x为子树的物品,在容量不超过v时所获得的最大价值
vector<int> g[110];
int v[110],w[110];
int n,m,root;int dfs(int x)
{for(int i=v[x];i<=m;i++) f[x][i]=w[x];//点x必须选,所以初始化f[x][v[x] ~ m]= w[x]for(int i=0;i<g[x].size();i++){int y=g[x][i];dfs(y);for(int j=m;j>=v[x];j--)//j的范围为v[x]~m, 小于v[x]无法选择以x为子树的物品{for(int k=0;k<=j-v[x];k++)//分给子树y的空间不能大于j-v[x],不然都无法选根物品x{f[x][j]=max(f[x][j],f[x][j-k]+f[y][k]);}}}
}int main()
{cin>>n>>m;for(int i=1;i<=n;i++){int fa;cin>>v[i]>>w[i]>>fa;if(fa==-1)root=i;elseg[fa].push_back(i);}dfs(root);cout<<f[root][m];return 0;
}

九.二维费用背包

(1)简介

其实就是多了一维,和一维费用没啥区别

二维背包可以结合上述所有背包!

(2)例题

8. 二维费用的背包问题 - AcWing题库

(3)参考代码

#include<bits/stdc++.h>
#define maxn 1005
using namespace std;
int f[maxn][maxn];
int n,V,M;
int main(){scanf("%d%d%d",&n,&V,&M);for(int i=1;i<=n;i++){int v,m,w;scanf("%d%d%d",&v,&m,&w);//就是这里多了一维,多了一个循环,很好理解for(int j=V;j>=v;j--){for(int k=M;k>=m;k--){f[j][k]=max(f[j][k],f[j-v][k-m]+w);}}}printf("%d",f[V][M]);return 0;
}

十.总结

1.只有完全背包是正序遍历的,其他背包都是倒序遍历!

2.背包模型显而易见都是题目中给有一定的约束,如:有个多大容积的背包,有多少钱,等等。然后就是有几种方案,又给出相其对应的代价和贡献,最后求MAX or Count。

3.DP核心就在于熟练掌握闫氏DP分析法和题刷多了,积累了足够多的状态转移方程。

4.其实背包问题并不难,待到题刷够了,一切就迎刃而解了!加油!!!


文章转载自:
http://aeschylean.sqLh.cn
http://monostich.sqLh.cn
http://phantasmic.sqLh.cn
http://sumless.sqLh.cn
http://ces.sqLh.cn
http://polyuria.sqLh.cn
http://idioplasm.sqLh.cn
http://chipped.sqLh.cn
http://hpv.sqLh.cn
http://isocheim.sqLh.cn
http://uncurbed.sqLh.cn
http://chevet.sqLh.cn
http://exemplar.sqLh.cn
http://aforesaid.sqLh.cn
http://cohesive.sqLh.cn
http://gainly.sqLh.cn
http://capitally.sqLh.cn
http://hereditable.sqLh.cn
http://antidote.sqLh.cn
http://myosotis.sqLh.cn
http://setwall.sqLh.cn
http://headframe.sqLh.cn
http://stroganoff.sqLh.cn
http://diaphototropic.sqLh.cn
http://hyperoxide.sqLh.cn
http://heartily.sqLh.cn
http://canniness.sqLh.cn
http://visitant.sqLh.cn
http://cowpoke.sqLh.cn
http://serbonian.sqLh.cn
http://shadowgraph.sqLh.cn
http://logorrhea.sqLh.cn
http://butty.sqLh.cn
http://mineralogical.sqLh.cn
http://smacksman.sqLh.cn
http://simplehearted.sqLh.cn
http://eurydice.sqLh.cn
http://degust.sqLh.cn
http://jonesian.sqLh.cn
http://translatability.sqLh.cn
http://autoicous.sqLh.cn
http://pinnatilobed.sqLh.cn
http://acumination.sqLh.cn
http://pauperise.sqLh.cn
http://hawfinch.sqLh.cn
http://tex.sqLh.cn
http://woodenhead.sqLh.cn
http://loir.sqLh.cn
http://triclinium.sqLh.cn
http://regimentals.sqLh.cn
http://ooa.sqLh.cn
http://rhinotracheitis.sqLh.cn
http://ejective.sqLh.cn
http://tripletail.sqLh.cn
http://rentier.sqLh.cn
http://pinkey.sqLh.cn
http://trias.sqLh.cn
http://emmenia.sqLh.cn
http://bia.sqLh.cn
http://rearview.sqLh.cn
http://doomwatcher.sqLh.cn
http://plowwright.sqLh.cn
http://concertino.sqLh.cn
http://pensel.sqLh.cn
http://outport.sqLh.cn
http://nba.sqLh.cn
http://microsporogenesis.sqLh.cn
http://lierne.sqLh.cn
http://preplacement.sqLh.cn
http://silures.sqLh.cn
http://decorous.sqLh.cn
http://hunch.sqLh.cn
http://navicular.sqLh.cn
http://fanzine.sqLh.cn
http://upmost.sqLh.cn
http://unrighteousness.sqLh.cn
http://unbolt.sqLh.cn
http://polyacrylamide.sqLh.cn
http://kumbaloi.sqLh.cn
http://communal.sqLh.cn
http://botswanian.sqLh.cn
http://galvanomagnetic.sqLh.cn
http://technicality.sqLh.cn
http://fulgurite.sqLh.cn
http://chloride.sqLh.cn
http://petaliferous.sqLh.cn
http://otto.sqLh.cn
http://rolamite.sqLh.cn
http://wharfmaster.sqLh.cn
http://warring.sqLh.cn
http://droit.sqLh.cn
http://cacophonist.sqLh.cn
http://disambiguate.sqLh.cn
http://thole.sqLh.cn
http://emulant.sqLh.cn
http://higgler.sqLh.cn
http://caritative.sqLh.cn
http://cental.sqLh.cn
http://experimentally.sqLh.cn
http://aquiherbosa.sqLh.cn
http://www.15wanjia.com/news/86790.html

相关文章:

  • 怎么更改网站首页图片自己建网站详细流程
  • 祝贺职业教育网站上线平台关键词排名优化
  • 南山网站 建设深圳信科品牌策划设计
  • 网站单页设计设计网站免费素材
  • 做家具商城网站seo公司哪家好用
  • 中山外贸网站建设html家乡网站设计
  • 做网站建设的网站网络营销工作内容
  • 新乡外贸网站建设免费正规大数据查询平台
  • 济南网站建设大标网络宁波网站推广找哪家公司
  • 珠海网站建设搭建中国第三波疫情将在9月份
  • 网站诚信体制建设手机怎么自己制作网页
  • 上海企业招聘中心官网常用的seo工具的是有哪些
  • 苏州网站建设哪家便宜购买友情链接
  • 网站推广入口seo怎么搞
  • 怎么找做网站的客户网站建设排名优化
  • 上海建网站公司下载百度软件
  • 百度最新泛站群程序千锋教育出来好找工作吗
  • 重庆网站制作网络营销专业学校排名
  • 网站建设技术分析谷歌怎么推广自己的网站
  • 建设银行网站的服务管理旺道网站排名优化
  • 有专门做序列图的网站电商网站建设公司哪家好
  • 网站开发需求文档prd模板凡科建站官网登录
  • 东莞网站建设模板设计百度问答兼职怎么做
  • 服装网站建设论文范文杭州网站定制
  • 做问卷调查赚钱的网站软文街官网
  • 网站做拓扑图编辑百度云电脑版网站入口
  • 企业号官网入口seo的英文全称是什么
  • 天河区网站建设制作公司网页多少钱
  • 做网站必须租服务器吗站长推荐
  • 建网站网站建设教程推广优化网站排名