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 nC1 and C2C1 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)的更多相关文章

  1. HDU 2923 Relocation(状压dp+01背包)

    题目代号:HDU2923 题目链接:http://poj.org/problem?id=2923 Relocation Time Limit: 1000MS Memory Limit: 65536K ...

  2. 【POJ 2923】Relocation(状压DP+DP)

    题意是给你n个物品,每次两辆车运,容量分别是c1,c2,求最少运送次数.好像不是很好想,我看了网上的题解才做出来.先用状压DP计算i状态下,第一辆可以运送的重量,用该状态的重量总和-第一辆可以运送的, ...

  3. POJ 2923 Relocation(状压DP)题解

    题意:有2辆车运货,每次同时出发,n(<10),各自装货容量c1 c2,问最少运几次运完. 思路:n比较小,打表打出所有能运的组合方式,用背包求出是否能一次运走.然后状压DP运的顺序. 代码: ...

  4. BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3336  Solved: 1936[Submit][ ...

  5. nefu1109 游戏争霸赛(状压dp)

    题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1109 //我们校赛的一个题,状压dp,还在的人用1表示,被淘汰 ...

  6. poj3311 TSP经典状压dp(Traveling Saleman Problem)

    题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...

  7. [NOIP2016]愤怒的小鸟 D2 T3 状压DP

    [NOIP2016]愤怒的小鸟 D2 T3 Description Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于(0,0)处,每次Kiana可 ...

  8. 【BZOJ2073】[POI2004]PRZ 状压DP

    [BZOJ2073][POI2004]PRZ Description 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 桥已经很旧了, 所以它不能承受太重的东西. 任何时候队伍 ...

  9. bzoj3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一(spfa+状压DP)

    数据最多14个有宝藏的地方,所以可以想到用状压dp 可以先预处理出每个i到j的路径中最小权值的最大值dis[i][j] 本来想用Floyd写,无奈太弱调不出来..后来改用spfa 然后进行dp,这基本 ...

  10. HDU 1074 Doing Homework (状压dp)

    题意:给你N(<=15)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则 ...

随机推荐

  1. 二分图匹配 + 构造 E. Arpa’s overnight party and Mehrdad’s silent entering

    http://codeforces.com/contest/742/problem/E 跪着看题解后才会的. 对于任何一对BF[i]和GF[i] 连接了一条边后,那么他们和隔壁都是不会有边相连的了,这 ...

  2. flutter基础

    1.flutter安装 1.参考官网安装sdk https://flutter.io/get-started/install 安卓和IOS需要分别配置对应的开发环境,安卓建议使用as开发,安装Flut ...

  3. mac下elasticsearch安装部署

    下载elaticsearch集成包 优势:封装了对插件的支持,且安装方式较简单 地址:https://github.com/medcl/elasticsearch-rtf 解压到指定目录后,获取该集成 ...

  4. webapi之fiddler头设置

    Host: localhost:16648Connection: keep-aliveContent-Length: 36Accept: application/json, text/javascri ...

  5. Visual studio每次build自动增加版本号

    关键词:visual studio,rc file,VS_VERSION_INFO,FILEVERSION,PRODUCTVERSION 目标:希望每次在vs中编译项目时,生成的可执行程序版本号自动+ ...

  6. EOS Dapp体验报告

    EOS Dapp体验报告 EOS通过并行链和DPOS的方式解决了延迟和数据吞吐量的难题. EOS能够实现每秒百万级的处理量,而目前比特币是每秒7笔,以太坊是30-40笔,EOS的这一超强能力吊打比特币 ...

  7. sybase sql anywhere 5.0 安装后sybase central中无法打开视图等的解决办法

    无法打开的原因初步分析要用英文版的xp,后来在如下处发现问题,是sql anywhere的版本太旧了, 可能没有使用Unicode编码,设置一下如下选项可以解决问题.

  8. SQLyog连接MySQL时出现的2058错误解决方法

    配置新连接报错:错误号码 2058,分析是 mysql 密码加密方法变了. 解决方法:windows 下cmd 登录 mysql -u root -p 登录你的 mysql 数据库,然后执行这条SQL ...

  9. css广告弹窗满屏跑

    window.onload=function(){ //广告滚动 var oneInner = $('#divid')[0]; //定时器 var a1a = setInterval(moves,10 ...

  10. (转)使用Spring的注解方式实现AOP入门

    http://blog.csdn.net/yerenyuan_pku/article/details/52865330 首先在Eclipse中新建一个普通的Java Project,名称为spring ...