Poj 2923 Relocation(状态压缩+动态规划)
来源:互联网 发布:js新窗口打开url 编辑:程序博客网 时间:2024/06/16 10:04
这道题目想了好久,还是做不出来,最后在网上搜了大大们的题解,发现思路完全不一样,竟然用到了状态压缩,
然而状态压缩是什么鬼呢?不太明白呢....
题目大意:有家人要搬家,而一共有两辆车,最大载重量分别是C1,C2(1<=C1,C2<=100),他们一共有n(1<=n<=10)
件家具要搬,问至少需要走多少趟.
解释:家具的状态一共有2^n种(包括不可行的),首先在这么多的方案中分别找出 车1 和 车2 可以载得料的方案,
然后进一步处理,将 车1 和 车2 没有交集的方案(即每样家与只会被搬运一次)收集起来,得出一系列的可行方案.
然后进行一次dp,这里的转移方程是:dp[t]=min(dp[t], dp[i] + 1),有必要说明一下,初始状态dp[0]=0是成立的,转移方程中
的i 用二进制表示,那么dp[i]代表帮了某些家具所走的最少趟.同理,而dp[t]表示在i的情况下执行可行方案后的最优解.
可能表述有些问题,细节反面看代码的实现把.
#include <cstdio>#include <queue>#include <cstring>#include <iostream>#include <cstdlib>#include <algorithm>#include <vector>#include <map>#include <set>#include <ctime>#include <cmath>using namespace std;const int N = 100000;int dp[N];int c1[N],c2[N];int ct1,ct2;int bag[N],bat;int main(){//freopen("/home/user/桌面/in","r",stdin);int t,ca=1;scanf("%d",&t);while(ca<=t){int n,v1,v2,fur[15];scanf("%d%d%d",&n,&v1,&v2);ct1=ct2=bat=0;for(int i=0;i<n;++i) scanf("%d",&fur[i]);for(int i=0;i<(1<<n);++i){int w=0;for(int j=0;j<10;++j){if(i&(1<<j)) w+=fur[j];}if(w<=v1) c1[ct1++]=i;if(w<=v2) c2[ct2++]=i;}for(int i=0;i<ct1;++i){for(int j=0;j<ct2;++j){if((c1[i]&c2[j])==0) bag[bat++] =(c1[i] | c2[j]);//if(i==0) cout<<" + "<<(c1[i]&c2[j])<<" -- "<<(c1[i]|c2[j])<<" - "<<bat<<endl;}}//sort(bag,bag+bat);//for(int i=0;i<bat;++i) cout<<bag[i]<<endl;memset(dp,-1,sizeof(dp));dp[0]=0;for(int i=0;i<(1<<n);++i){//if(dp[i]==-1) continue;for(int j=0;j<bat;++j){if(i&bag[j]) continue;int t = i|bag[j];if(dp[t]==-1) dp[t]=dp[i]+1;else dp[t]=min(dp[t],dp[i]+1);}}printf("Scenario #%d:\n",ca++);printf("%d\n\n",dp[(1<<n)-1]);}//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);return 0;}
0 0
- Poj 2923 Relocation(状态压缩+动态规划)
- poj 2923 Relocation 状态压缩
- 动态规划——Relocation 动态规划+状态压缩
- poj 2923 Relocation(状态压缩+01背包)
- poj 2923 Relocation 状态压缩01背包
- poj 2923 Relocation 状态压缩+01背包
- POJ 2923 Relocation / 状态压缩DP
- POJ 2923 - Relocation(状态压缩+01背包)
- POJ 2923 Relocation(状态压缩+01背包)
- POJ 2923 Relocation(状态压缩+ 01背包)
- POJ - 2923 Relocation(BFS+状态压缩)
- POJ 2923Relocation(状态压缩入门)
- POJ 2923 Relocation (状态压缩+背包)
- POJ 2923 Relocation(状态压缩+背包思想)
- POJ 2923-Relocation-状态压缩+DP
- POJ 2923 Relocation (状态压缩 0-1背包)
- poj 2923 Relocation 二进制状态压缩 深搜+广搜
- poj 2923 Relocation(状态压缩+01背包)
- 高斯函数
- Lucene系列-index弹性扩展
- uva oj 340 - Master-Mind Hints
- 使用SQLAlchemy ORM进行一些基础的操作(建表,插入,查询,删除)
- 重建二叉树
- Poj 2923 Relocation(状态压缩+动态规划)
- LeetCodeOJ_7_Reverse Integer
- 【南大软院大神养成计划】html+css之网页布局
- 字符编码的问题详解
- final修饰符---final变量
- 创建基于Gradle的Web项目
- Localtunnel内网穿透工具的安装与使用
- 笑看昨天 珍爱今天 期待明天
- 三元组顺序表-转置运算-三种算法