Relocation(状压DP)
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
题目大意:
输入n,c1,c2分别代表有n个物品,和两辆最大能承受的重量,求最少能几次运完n个物品。
将一个数的二进制状况表示当前物品的集合,比如10的二进制为1010,即代表第二个和第四个的集合。
枚举所有状态,把能被一次运走的状态记录下来,然后01背包即可。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
int a[],dp[<<],st[<<],vis[<<];
int tot,n,c1,c2;
bool check(int s)///检查该状态能不能被一次运完
{
int sum=;
memset(vis,,sizeof vis);
vis[]=;
for(int i=;i<n;i++)
{
if((<<i)&s)
{
sum+=a[i];
for(int j=c1;j>=a[i];j--)
if(vis[j-a[i]])
vis[j]=;
}
}
if(sum>c1+c2) return ;
for(int i=;i<=c1;i++)
if(vis[i]&&sum-i<=c2) return ;
return ;
}
int main()
{
int T,o=;
scanf("%d",&T);
while(T--)
{
memset(dp,INF,sizeof dp);
scanf("%d%d%d",&n,&c1,&c2);
for(int i=;i<n;i++)
scanf("%d",&a[i]);
tot=;
for(int i=;i<(<<n);i++)
if(check(i))
st[tot++]=i;
dp[]=;
for(int i=;i<tot;i++)///枚举可一次运完状态
for(int j=(<<n)-;j>=;j--)
if((j&st[i])==)///j与st[i]没有交集
dp[j|st[i]]=min(dp[j|st[i]],dp[j]+);
printf("Scenario #%d:\n%d\n\n",++o,dp[(<<n)-]);
}
return ;
}
Relocation(状压DP)的更多相关文章
- HDU 2923 Relocation(状压dp+01背包)
题目代号:HDU2923 题目链接:http://poj.org/problem?id=2923 Relocation Time Limit: 1000MS Memory Limit: 65536K ...
- 【POJ 2923】Relocation(状压DP+DP)
题意是给你n个物品,每次两辆车运,容量分别是c1,c2,求最少运送次数.好像不是很好想,我看了网上的题解才做出来.先用状压DP计算i状态下,第一辆可以运送的重量,用该状态的重量总和-第一辆可以运送的, ...
- POJ 2923 Relocation(状压DP)题解
题意:有2辆车运货,每次同时出发,n(<10),各自装货容量c1 c2,问最少运几次运完. 思路:n比较小,打表打出所有能运的组合方式,用背包求出是否能一次运走.然后状压DP运的顺序. 代码: ...
- BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3336 Solved: 1936[Submit][ ...
- nefu1109 游戏争霸赛(状压dp)
题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1109 //我们校赛的一个题,状压dp,还在的人用1表示,被淘汰 ...
- poj3311 TSP经典状压dp(Traveling Saleman Problem)
题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...
- [NOIP2016]愤怒的小鸟 D2 T3 状压DP
[NOIP2016]愤怒的小鸟 D2 T3 Description Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于(0,0)处,每次Kiana可 ...
- 【BZOJ2073】[POI2004]PRZ 状压DP
[BZOJ2073][POI2004]PRZ Description 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 桥已经很旧了, 所以它不能承受太重的东西. 任何时候队伍 ...
- bzoj3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一(spfa+状压DP)
数据最多14个有宝藏的地方,所以可以想到用状压dp 可以先预处理出每个i到j的路径中最小权值的最大值dis[i][j] 本来想用Floyd写,无奈太弱调不出来..后来改用spfa 然后进行dp,这基本 ...
- HDU 1074 Doing Homework (状压dp)
题意:给你N(<=15)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则 ...
随机推荐
- 抽象类 abstract
抽象类就是拿来继承的抽象方法就是拿来重写的 1.用abstract可以用来修饰类或方法,分别叫抽象类和抽象方法. 2.含有抽象方法的类必须被声明为抽象类.,抽象类必须被继承,抽象方法也必须被重写. 3 ...
- Learn More Study Less `my notes`
整体性学习概念: 广泛扎实的基础知识 抽象知识成生活中的模型,便于记忆 融会贯通,创造新的东西 整体性学习组成 获取:积极阅读:标记并结合其他的知识点 主要观点 怎么记住:联系和比喻其他的知识 拓展和 ...
- HBase备份恢复练习
一.冷备 1.创建测试表并插入测试数据 [root@weekend05 ~]# hbase shell hbase(main):005:0> create 'scores','grade','c ...
- 2019/05/11 Java内存结构
1. 类加载子系统:负责从文件系统或者网络加载Class信息,加载的信息存放在一块称之方法区的内存空间. 2. 方法区:就是存放类的信息.常量信息.常量池信息.包括字符串字面量和数字常量等. 3. ...
- Java_静态变量
class c1c { private static int num = 0; private static double pi = 3.14; private double radius; priv ...
- IOS OS X 中集中消息的传递机制
1 KVO (key-value Observing) 是提供对象属性被改变是的通知机制.KVO的实现实在Foundation中,很多基于 Foundation 的框架都依赖与它.如果只对某一个对象的 ...
- 微信小程序开发系列四:微信小程序之控制器的初始化逻辑
微信小程序开发系列教程 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 微信小程序开发系列二:微信小程序的视图设计 微信小程序开发系列三:微信小程序的调试方法 这个教程的前两篇文章,介绍了如何 ...
- MySql数据库--持续记录ing
1 基本,引擎,数据类型,运算1.1 基本操作启动:net start mysql停止:net stop mysql连接: mysql –uroot -h127.0.0.1 -proot断开连接:qu ...
- js 输出某年某月某日的天数/判断闰年
console.log(getDays(2017,12,12)); function getDays(year,month,day){ var arr = [31,28,31,30,31,30,31, ...
- uva1352 Colored Cubes LA3401
白书第一章例题8 好麻烦! 正方体每面编号为0-5,那么根据顶点和正面,就能确定形态.一共6*4=24种形态. P[i]表示编号i所在位置.比如P[1]=3,表示第二面转到了第四面. 就可以表示出所有 ...