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表示 ...
随机推荐
- hive udf编程教程
hive udf编程教程 https://blog.csdn.net/u010376788/article/details/50532166
- Educational Codeforces Round 64 -C(二分)
题目链接:https://codeforces.com/contest/1156/problem/C 题意:给出n个数和整形数z,定义一对数为差>=z的数,且每个数最多和一个数组成对,求最多有多 ...
- 创建B树,动态添加节点,并使用三种遍历算法对树进行遍历
ks17:algorithm apple$ cat btree_test.c ///********************************************************** ...
- Java细节----method和function的区别
面向对象的语言叫方法 面向过程的语言叫函数 在java中没有函数这么一说,只有方法一说.属于概念上的区别. 硬要说区别. Method必须依赖于Object. Function 是独立的,不需要依赖于 ...
- 洛谷 P3368 树状数组 题解
题面 本题随便看两眼就知道该题满足了优美的查分性质: 对于在区间[x,y]内操作时,应该将查分数组的第x项和第y+1项进行相反操作: 询问答案时,问第i个数的值就是查分数组的前i项和: 暴力+玄学卡常 ...
- 从入门到自闭之Python三大器--迭代器
函数名的第一类对象(概述): 使用方式: 函数名可以当做值赋值给变量 def func(): print(1) print (func) #查看函数的内存地址 a = func print (a) # ...
- vue - helloVue
开始学习vue了 1.数据绑定:{{data}} 2.el属性(挂载对象): el:标签任意(例如:#app,.app,app) 3.data:{} :存放数据. <!DOCTYPE html& ...
- Git 操作 GitHub
Git安装 https://www.cnblogs.com/taopanfeng/p/11076702.html 设置用户名(设置一次 以后就不用再设置了) git config --global u ...
- axios 请求常用组件,及其错误
1. var nodeModules = path.join(processPath, 'node_modules') var querystring = require(nodeModules + ...
- zabbix监控之同时向多人邮件报警
安装环境: zabbix-server zabbix邮件报警配置步骤说明: 安装发送邮件的工具sendEmail 准备一个发送邮件的脚本 修改zabbix配置文件中指定的脚本路径 关联脚本名称 用户 ...