HDU 2923 Relocation(状压dp+01背包)
题目代号:HDU2923
题目链接:http://poj.org/problem?id=2923
Relocation
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3472 | Accepted: 1422 |
Description
Emma and Eric are moving to their new house they bought after returning from their honeymoon. Fortunately, they have a few friends helping them relocate. To move the furniture, they only have two compact cars, which complicates everything a bit. Since the furniture does not fit into the cars, Eric wants to put them on top of the cars. However, both cars only support a certain weight on their roof, so they will have to do several trips to transport everything. The schedule for the move is planed like this:
- At their old place, they will put furniture on both cars.
- Then, they will drive to their new place with the two cars and carry the furniture upstairs.
- Finally, everybody will return to their old place and the process continues until everything is moved to the new place.
Note, that the group is always staying together so that they can have more fun and nobody feels lonely. Since the distance between the houses is quite large, Eric wants to make as few trips as possible.
Given the weights wi of each individual piece of furniture and the capacities C1 and C2 of the two cars, how many trips to the new house does the party have to make to move all the furniture? If a car has capacity C, the sum of the weights of all the furniture it loads for one trip can be at most C.
Input
The first line contains the number of scenarios. Each scenario consists of one line containing three numbers n, C1 and C2. C1 and C2 are the capacities of the cars (1 ≤ Ci ≤ 100) and n is the number of pieces of furniture (1 ≤ n ≤ 10). The following line will contain n integers w1, …, wn, the weights of the furniture (1 ≤ wi ≤ 100). It is guaranteed that each piece of furniture can be loaded by at least one of the two cars.
Output
The output for every scenario begins with a line containing “Scenario #i:”, where i is the number of the scenario starting at 1. Then print a single line with the number of trips to the new house they have to make to move all the furniture. Terminate each scenario with a blank line.
Sample Input
2
6 12 13
3 9 13 3 10 11
7 1 100
1 2 33 50 50 67 98
Sample Output
Scenario #1:
2 Scenario #2:
3
题目大意:搬家只有两辆车运家具,两辆车要一起行动,并且两辆车的最大载重分别为c1,c2,有n件家具,给出了n件家具分别的重量,问要最少多少次才能运完所有家具。
题目分析:第一次接触状压dp的问题,纠结了很久也不知道状态压缩的实质与意义是什么,弄了整整一天才把这道题ac,哎~(心情复杂脸),咳咳,不扯淡了,开始正题。其实这道题状态压缩的实质就是用二进制的1和0代表物品的有无,比如说二进制011代表的是整数3,在状态压缩中的含义是整数3所代表的状态中,有第一和第二件物品,所以用二进制的方式来表达物品的存在状态是很方便的事情。
注意:输出时每两组数据都需要一个额外的空行(POJ中的题目可以在每组数据的末尾添加两个换行,但是UVA中的不允许返回的是格式错误)
PS:这是一道状态压缩好题,多研究有好处
AC代码:
//# define FLAG
///delet....................................
# include <iostream>
# include <cstring>
# include <cstdlib>
# include <cstdio>
# include <string>
# include <cmath>
# include <ctime>
# include <set>
# include <map>
# include <queue>
# include <stack>
# include <bitset>
# include <vector>
# include <fstream>
# include <algorithm>
using namespace std;
# define eps 1e-8
# define pb push_back
# define mp make_pair
# define pi acos(-1.0)
# define bug puts("H");
# define mem(a,b) memset(a,b,sizeof(a))
# define IOS ios::sync_with_stdio(false);
# define FO(i,n,a) for(int i=n; i>=a; --i)
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define MAX 0x7fffffffffffff
# define INF 0x3f3f3f3f
# define MOD 1000000007
/// 123456789
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef unsigned long long ULL;
typedef long long LL;
inline int Scan(){
int x=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
///coding...................................
const int MAXM=(1<<15);
int a[15],b[MAXM],c[MAXM],dp[MAXM],vis[MAXM],n,c1,c2;
int main()
{
IOS
#ifdef FLAG
freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
#endif /// FLAG
int t,cnt=0;
cin>>t;
while(t--) {
int n,c1,c2;
cin>>n>>c1>>c2;
for(int i=0;i<n;++i)
cin>>a[i];
int sum,t1=0,t2=0;
for(int i=0;i<(1<<n);++i) { ///遍历所有可能的方案
sum=0;
for(int j=0;j<n;++j) ///遍历i所代表的状态中,拥有物品的总重量
if(i&(1<<j))sum+=a[j];
if(sum<=c1)b[t1++]=i; ///如果满足重量小于c1则为一种可选择的方案
if(sum<=c2)c[t2++]=i; ///同上
}
int t=0;
for(int i=0;i<t1;++i)
for(int j=0;j<t2;++j)
if((b[i]&c[j])==0)vis[t++]=(b[i]|c[j]); ///确保两个方案中没有相同的物品
for(int i=0;i<(1<<n);++i)dp[i]=INF; ///初始化
dp[0]=0;
for(int i=0;i<t;i++)
for(int j=(1<<n)-1-vis[i];j>=0;j--)
if((j&vis[i])==0)
dp[j|vis[i]]=min(dp[j|vis[i]],dp[j]+1);
if(cnt)puts("");
printf("Scenario #%d:\n%d\n",++cnt,dp[(1<<n)-1]);
}
return 0;
}
///delete FLAG..............................
HDU 2923 Relocation(状压dp+01背包)的更多相关文章
- poj 2923 状压dp+01背包
好牛b的思路 题意:一系列物品,用二辆车运送,求运送完所需的最小次数,两辆车必须一起走 解法为状态压缩DP+背包,本题的解题思路是先枚举选择若干个时的状态,总状态量为1<<n,判断这些状态 ...
- BZOJ 4145: [AMPPZ2014]The Prices( 状压dp + 01背包 )
我自己只能想出O( n*3^m )的做法....肯定会T O( nm*2^m )做法: dp( x, s ) 表示考虑了前 x 个商店, 已买的东西的集合为s. 考虑转移 : 先假设我们到第x个商店去 ...
- POJ 2923 Relocation(状压DP+01背包)题解
题意:给你汽车容积c1,c2,再给你n个包裹的体积,问你最少运几次能全运走 思路:用2进制表示每次运送时某物在不在此次运送之中,1在0不在.我们把运送次数抽象成物品价值,把状态抽象成体积,用一个dp[ ...
- 树形DP和状压DP和背包DP
树形DP和状压DP和背包DP 树形\(DP\)和状压\(DP\)虽然在\(NOIp\)中考的不多,但是仍然是一个比较常用的算法,因此学好这两个\(DP\)也是很重要的.而背包\(DP\)虽然以前考的次 ...
- NOI 2015 寿司晚宴 (状压DP+分组背包)
题目大意:两个人从2~n中随意取几个数(不取也算作一种方案),被一个人取过的数不能被另一个人再取.两个人合法的取法是,其中一个人取的任何数必须与另一个人取的每一个数都互质,求所有合法的方案数 (数据范 ...
- NOIP模拟 乘积 - 状压dp + 分组背包
题目大意: 给出n和k,求从小于等于n的数中取出不超过k个,其乘积是无平方因子数的方案数.无平方因子数:不能被质数的平方整除. 题目分析: 10(枚举\(n\le8\)),40(简单状压\(n\le1 ...
- HDU - 6125: Free from square (状压DP+分组背包)
problem:给定N,K.表示你有数1到N,让你最多选择K个数,问有多少种方案,使得选择的数的乘积无平方因子数.N,K<500: solution:显然可以状压DP做,但是500以内的素数还是 ...
- HDU 4284Travel(状压DP)
HDU 4284 Travel 有N个城市,M条边和H个这个人(PP)必须要去的城市,在每个城市里他都必须要“打工”,打工需要花费Di,可以挣到Ci,每条边有一个花费,现在求PP可不可以从起点1 ...
- HDU 4336 容斥原理 || 状压DP
状压DP :F(S)=Sum*F(S)+p(x1)*F(S^(1<<x1))+p(x2)*F(S^(1<<x2))...+1; F(S)表示取状态为S的牌的期望次数,Sum表示 ...
随机推荐
- 交换机安全学习笔记 第八章 针对POE的攻击
POE即 Power over Ethernet 借助于以太网供电.最初为了IP电话,目前主要用于功耗小于15.4w的设备例如Ap和视频监控设备.并且简化了相关设备的电力线布线. 英文缩写注释:PSE ...
- HDU 5437 & ICPC 2015 Changchun Alisha's Party(优先队列)
Alisha’s Party Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- numpy使用数组进行数据处理
numpy使用数组进行数据处理 meshgrid函数 理解: 二维坐标系中,X轴可以取三个值1,2,3, Y轴可以取三个值7,8, 请问可以获得多少个点的坐标? 显而易见是6个: (1,7)(2,7) ...
- 设备程序远程升级采用两种方式(优先采用IP方式)
设备程序远程升级采用两种方式(优先采用IP方式): 采用应急广播TS流传输技术规范的消息内容表携带升级包数据.当辅助数据类型值为44时,消息内容表传输的数据为程序升级包. 采用IP方式传输升级包数据. ...
- gcc 数据对齐之:总结篇.
通过上面的分析,总结结构体对齐规则如下: 1.数据成员对齐规则:结构(struct)(或联合(union))的数据成员,第一个数据成员放在offset为0的地方,以后每个数据成员的对齐按照#pragm ...
- CentOS7下载与安装错误全记录
这篇文章记录安装CentOS7过程错误全记录,供大家和自己参考 起因:笔记本用的win10系统,开启热点的时候,总是10分钟就自动关闭.于是折腾linux系统,平时用win10系统,也切换到linux ...
- JS获取指定范围随机数
常用取整数的方法 : Math.floor(Math.random() * (max - min + 1)) + min 一步步来解析: Math.random() 函数返回一个浮点, 伪随机数在范 ...
- 掌握 analyze API,一举搞定 Elasticsearch 分词难题
初次接触 Elasticsearch 的同学经常会遇到分词相关的难题,比如如下这些场景: 为什么明明有包含搜索关键词的文档,但结果里面就没有相关文档呢? 我存进去的文档到底被分成哪些词(term)了? ...
- 微信小程序与内嵌webview之间来回跳转的几点总结,以及二维码的使用
截止到发稿小程序支持的功能,后续如果小程序更新在完善文稿. 1. 小程序可以内嵌组件跳转到h5页面,前提是在小程序后台配置相应的业务域名.新打开的h5页面会替代小程序组件内的其它组件,即为h5不能与小 ...
- wex5 如何写后台BAAS
Data.java: 在class中链接数据源: 配置的numsql数据源 private static final String DATASOURCE_NUMYSQL = "numysql ...