POJ 2923 Relocation(状态压缩DP+DP:01背包)
来源:互联网 发布:电脑淘宝精选怎么删除 编辑:程序博客网 时间:2024/05/01 19:07
POJ 2923 Relocation(状态压缩DP+DP:01背包)
http://poj.org/problem?id=2923
题意:有N件物品和两辆车,每辆车有个重量上限,现在要搬家且每次从老家到新家都是两车同时出动,问你至少需要多少次能搬完?
分析:
首先本题先要求出所有物品合法的组合,即两车能一次装下的物品组合,然后在利用状态压缩DP求解.
首先对于有a1,a2,…ak物品组成的物品组合,是否能装下,只需要用一次DP01背包求出c1能装的最大物品数目,然后看看剩下的部分c2能不能装下即可.(想想是不是?)
第二步利用状态压缩DP,假设可行的物品组合有cnt个,ok[i]=j表示第i个可行的组合为j,令dp[i][j]=y表示决策完前i个可行组合后,当前物品选择状态为j时,最少需要y次运输.
dp[i][ j+ok[i] ]=min( dp[i-1][j+ok[i] ] , dp[i-1][j] )其中j与ok[i]没有重叠物品.
注意:代码实现最后计算状态压缩DP的时候用了类似Bellmanford算法的松弛技术.
AC代码:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int MAXN=1100;const int INF=1e8;int n,c1,c2,w[15];int ok[MAXN],dp[MAXN],cnt;bool good(int st){ int f[110]; memset(f,0,sizeof(f));//f[i]=j 表示容量为i的车最多能装j重的物品 int sum=0; for(int i=0;i<n;i++)if(st&(1<<i)) { sum +=w[i]; for(int j=c1;j>=w[i];j--) { f[j] = max(f[j] , f[j-w[i]]+w[i]); } } if(sum>c1+c2) return false; for(int j=c1;j>=0;j--) if(sum-f[j]<=c2) return true; return false;}int main(){ int T; scanf("%d",&T); for(int kase=1;kase<=T;kase++) { scanf("%d%d%d",&n,&c1,&c2); for(int i=0;i<n;i++) scanf("%d",&w[i]); cnt=0; for(int i=0;i<(1<<n);i++) if(good(i)) ok[cnt++]=i; for(int j=0;j<(1<<n);j++) dp[j]=INF; dp[0]=0; for(int i=0;i<cnt;i++)//松弛 { for(int j=0;j<(1<<n);j++)if( !(j&ok[i]) ) { dp[j|(ok[i])] = min(dp[j|(ok[i])] , dp[j]+1); } } printf("Scenario #%d:\n%d\n\n",kase,dp[(1<<n)-1]); }}
0 0
- POJ 2923 Relocation(状态压缩DP+DP:01背包)
- poj 2923 Relocation(DP+状态压缩+01背包)
- POJ 2923 Relocation 状态DP+01背包
- poj 2923 Relocation(状态压缩dp+背包)
- POJ 2923 Relocation 状态压缩DP + 0-1背包
- POJ 2923 Relocation / 状态压缩DP
- POJ 2923-Relocation-状态压缩+DP
- poj 2923 Relocation(状态压缩+01背包)
- poj 2923 Relocation 状态压缩01背包
- poj 2923 Relocation 状态压缩+01背包
- POJ 2923 - Relocation(状态压缩+01背包)
- POJ 2923 Relocation(状态压缩+01背包)
- POJ 2923 Relocation(状态压缩+ 01背包)
- POJ 2923 Relocation(状压DP+01背包)
- poj 2923 Relocation (枚举+背包 | 状态压缩+01背包)
- poj2923 Relocation(压缩dp+01背包)
- POJ 2923 Relocation (状态压缩+背包)
- POJ 2923 Relocation(状态压缩+背包思想)
- android Json数据分装Post请求
- 大数用法--JAVA
- ubuntu系统关闭ssh的自动启动
- Android webview刷新 Uncaught ReferenceError: isReadyForPullUp is not defined at null:1
- SUMO仿真快速入门系列三:产生车辆移动模型
- POJ 2923 Relocation(状态压缩DP+DP:01背包)
- A5每日一问2014年3月精彩问答推荐15问
- ‘for’ loop initial declarations are only allowed in C99 mode
- java字符流和字节流
- 孙鑫VC++视频教程关键代码在VS2010中出现问题
- Android Camera OMXCameraAdapter.cpp初始化分析
- vs2012编译cocos2d-x3.0,编译到一半自动关机,原因是cpu占用率达到100%
- 前端开发IE8问题
- Android4.2模拟器无法弹出软键盘解决