题目代号: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. MSF魔鬼训练营-3.1.2信息收集-通过搜索引擎进行信息搜集

    1.Google hacking 技术 自动化的Google搜索工具 SiteDigger https://www.mcafee.com/us/downloads/free-tools/sitedig ...

  2. Java第六周实验+总结

    一.实验目的 (1)掌握类的继承 1.子类继承父类中非private的成员变量和成员方法,同时,注意构造方法不能被子类继承. 2.定义类时若缺省extends关键字,则所定义的类为java.lang. ...

  3. spring boot-3.原理探究

    新建的项目结构如下图: 1.POM 文件 项目会默认依赖 spring-boot-starter-parent 项目 <parent> <groupId>org.springf ...

  4. Springboot与springcloud

    1.什么是Spring Boot? 它简化了搭建Spring项目,自动配置Spring,简化maven配置,自带tomcat无需部署war包,创建独立的spring引用程序main方法运行: 2.Sp ...

  5. Vue.js官方文档学习笔记(一)起步篇

    Vue.js起步 Vue.js介绍 Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用.Vue 的核心库 ...

  6. vs 小贴士

    1. ubuntu上vs code root执行 sudo code --user-data-dir="~/.vscode-root" 2. 设置vs code 打开在新的标签页打 ...

  7. python3.6 使用newspaper库的Article包来快速抓取网页的文章或者新闻等正文

    我主要是用了两个方法来抽去正文内容,第一个方法,诸如xpath,css,正则表达式,beautifulsoup来解析新闻页面的时候,总是会遇到这样那样各种奇奇怪怪的问题,让人很头疼.第二个方法是后面标 ...

  8. layer.prompt绑定确认键

    case 'eventkc': top.layer.prompt({ formType: , title: '修改<span style="color:red">' + ...

  9. 04.AutoMapper 之投影(Projection)

    https://www.jianshu.com/p/031553705417 投影(Projection) 投影将源转换为目标而不是扁平化对象模型.如果没有额为的配置AutoMapper需要一个扁平化 ...

  10. node.js--使用Express中app.use()分模块开发

    app.use(path,callback)中的callback既可以是router对象又可以是函数:将一个URL路径与一个函数绑定,第一个参数为访问的路径,如果第一参数为空,则表示任何路径都触发这个 ...