bzoj4145 [AMPPZ2014]The Prices (状压DP)
来源:互联网 发布:淘宝手机页面设为首页 编辑:程序博客网 时间:2024/05/18 01:16
bzoj4145 [AMPPZ2014]The Prices
原题地址:http://www.lydsy.com/JudgeOnline/problem.php?id=4145
题意:
你要购买m种物品各一件,一共有n家商店,你到第i家商店的路费为d[i],在第i家商店购买第j种物品的费用为c[i][j],求最小总费用。
数据范围
1<=n<=100,1<=m<=16,1<=d[i]<=1000000,1<=c[i][j]<=1000000
题解:
f[i][j]在前i家商店,买的物品状态为j的最小花费。
值得注意的是枚举物品子集的方式,为了方便转移及不重复枚举,先都加上路费,然后每次枚举一个物品转移到同层,最后再与不去i店的最小值比较。
(其实同一层之间的是类似背包的转移)
代码:
#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>using namespace std;const int N=105;const int M=18;int n,m,c[N][M],w[N],dp[N][1<<16];int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%d",&w[i]); for(int j=1;j<=m;j++) scanf("%d",&c[i][j]); } memset(dp,0x3f,sizeof(dp)); dp[0][0]=0; int top=(1<<m)-1; for(int i=1;i<=n;i++) { for(int j=0;j<=top;j++) dp[i][j]=min(dp[i][j],dp[i-1][j]+w[i]); for(int j=0;j<=top;j++) { for(int k=1;k<=m;k++) { if(!(j&(1<<(k-1)))) dp[i][j|(1<<(k-1))]=min(dp[i][j]+c[i][k],dp[i][j|(1<<(k-1))]); } } for(int j=0;j<=top;j++) dp[i][j]=min(dp[i][j],dp[i-1][j]); } printf("%d\n",dp[n][top]); return 0;}
阅读全文
1 0
- bzoj4145 [AMPPZ2014]The Prices (状压DP)
- 【bzoj4145】【AMPPZ2014】【The Prices】【状压dp】
- 【BZOJ4145】[AMPPZ2014]The Prices【状压DP】【背包】
- bzoj4145 [AMPPZ2014]The Prices 状压dp
- 【bzoj4145】[AMPPZ2014]The Prices
- 【AMPPZ2014】【BZOJ4145】The Prices
- [BZOJ4145] [AMPPZ2014]The Prices
- bzoj4145 [AMPPZ2014]The Prices
- [bzoj4145][AMPPZ2014]The Prices
- BZOJ4145: [AMPPZ2014]The Prices
- BZOJ 4145 AMPPZ2014 The Prices 状压DP
- 4145: [AMPPZ2014]The Prices 状压DP
- bzoj 4145: [AMPPZ2014]The Prices 状压dp
- 【BZOJ4145】【装压DP】 The Prices 题解
- bzoj 4145 [AMPPZ2014]The Prices dp
- BZOJ 4145: [AMPPZ2014]The Prices
- 状压BZOJ 4145 The Prices题解
- 【AMPPZ2014】【BZOJ4143】The Lawyer
- echarts 调整图表大小的方法
- 热更新之生成文件md5
- 线程通信
- TP5 where多条件查询
- Elastic Search快速上手(2):将数据存入ES
- bzoj4145 [AMPPZ2014]The Prices (状压DP)
- 鼠标经过显示下拉选择下拉
- Java调用C++动态库具体实现
- Elastic Search快速上手(3):搜索
- CSS、HTMl使用position: fixed;实现div的高度填满剩余空间
- c和c++总结
- CCF之打酱油
- hi3716m v310怎么进行网络配置?
- centos7 yum安装mysql