题目代号: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:

  1. At their old place, they will put furniture on both cars.
  2. Then, they will drive to their new place with the two cars and carry the furniture upstairs.
  3. 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背包)的更多相关文章

  1. poj 2923 状压dp+01背包

    好牛b的思路 题意:一系列物品,用二辆车运送,求运送完所需的最小次数,两辆车必须一起走 解法为状态压缩DP+背包,本题的解题思路是先枚举选择若干个时的状态,总状态量为1<<n,判断这些状态 ...

  2. BZOJ 4145: [AMPPZ2014]The Prices( 状压dp + 01背包 )

    我自己只能想出O( n*3^m )的做法....肯定会T O( nm*2^m )做法: dp( x, s ) 表示考虑了前 x 个商店, 已买的东西的集合为s. 考虑转移 : 先假设我们到第x个商店去 ...

  3. POJ 2923 Relocation(状压DP+01背包)题解

    题意:给你汽车容积c1,c2,再给你n个包裹的体积,问你最少运几次能全运走 思路:用2进制表示每次运送时某物在不在此次运送之中,1在0不在.我们把运送次数抽象成物品价值,把状态抽象成体积,用一个dp[ ...

  4. 树形DP和状压DP和背包DP

    树形DP和状压DP和背包DP 树形\(DP\)和状压\(DP\)虽然在\(NOIp\)中考的不多,但是仍然是一个比较常用的算法,因此学好这两个\(DP\)也是很重要的.而背包\(DP\)虽然以前考的次 ...

  5. NOI 2015 寿司晚宴 (状压DP+分组背包)

    题目大意:两个人从2~n中随意取几个数(不取也算作一种方案),被一个人取过的数不能被另一个人再取.两个人合法的取法是,其中一个人取的任何数必须与另一个人取的每一个数都互质,求所有合法的方案数 (数据范 ...

  6. NOIP模拟 乘积 - 状压dp + 分组背包

    题目大意: 给出n和k,求从小于等于n的数中取出不超过k个,其乘积是无平方因子数的方案数.无平方因子数:不能被质数的平方整除. 题目分析: 10(枚举\(n\le8\)),40(简单状压\(n\le1 ...

  7. HDU - 6125: Free from square (状压DP+分组背包)

    problem:给定N,K.表示你有数1到N,让你最多选择K个数,问有多少种方案,使得选择的数的乘积无平方因子数.N,K<500: solution:显然可以状压DP做,但是500以内的素数还是 ...

  8. HDU 4284Travel(状压DP)

    HDU 4284    Travel 有N个城市,M条边和H个这个人(PP)必须要去的城市,在每个城市里他都必须要“打工”,打工需要花费Di,可以挣到Ci,每条边有一个花费,现在求PP可不可以从起点1 ...

  9. 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表示 ...

随机推荐

  1. windows qt 使用openssl API

    1.下载安装openssl http://dl.pconline.com.cn/download/355862-1.html 版本: OpenSSL(Win32) 1.0.1g 2.配置QT项目文件, ...

  2. 7大python 深度学习框架的描述及优缺点绍

    Theano https://github.com/Theano/Theano 描述: Theano 是一个python库, 允许你定义, 优化并且有效地评估涉及到多维数组的数学表达式. 它与GPUs ...

  3. 克隆完虚拟机后修改网卡,修改静态IP

    命令:vim  /etc/sysconfg/network-scripts/ifcfg-eth0 编辑: vi /etc/udev/rules.d/70-persistent-net.rules 克隆 ...

  4. bind函数作用、应用场景以及模拟实现

    bind函数 bind 函数挂在 Function 的原型上 Function.prototype.bind 创建的函数都可以直接调用 bind,使用: function func(){ consol ...

  5. sql server 函数详解(1)字符串函数

    ASCII()函数 CHAR()函数 LEFT()函数 RIGHT()函数 LTRIM()函数 RTRIM()函数 STR()函数 字符串逆序的函数REVERSE() 计算字符串的长度函数LEN(st ...

  6. 多线程之继承Thread类及多线程内存分析

    *创建多线程的一种方式:继承Thread类 * java.lang.Thread是描述多线程的类,要实现多线程程序,一种方式就是继承Thread类 * 1.创建一个类Mythread让其extends ...

  7. 53道Java线程面试题

    53道Java线程面试题 下面是Java线程相关的热门面试题,你可以用它来好好准备面试. 1) 什么是线程? 线程是操作系统能够进行运算调度的最小单位,它被包含在进程之中,是进程中的实际运作单位.程序 ...

  8. Visual Studio Code python 代码快速自动提示

    1.file --> setting->设置 搜索 python 或者auto_complete setting.json { "explorer.confirmDelete&q ...

  9. poj 1915 KnightMoves(bfs)

    Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 24094   Accepted: 11364 De ...

  10. PHP 优化之php -fpm 进程

    一,php-fpm的启动参数 1 2 3 4 5 6 7 8 9 10 11 12 13 #测试php-fpm配置 /usr/local/php/sbin/php-fpm -t /usr/local/ ...