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. 题解报告:NYOJ #78 圈水池(打印凸包顶点)

    描述: 有一个牧场,牧场上有很多个供水装置,现在牧场的主人想要用篱笆把这些供水装置圈起来,以防止不是自己的牲畜来喝水,各个水池都标有各自的坐标,现在要你写一个程序利用最短的篱笆将这些供水装置圈起来!( ...

  2. html2canvas如何将div转成图片并下载,如何将滚动条的内容截取下来

    <!DOCTYPE html> <html lang="en"> <head> <meta name="layout" ...

  3. 508 Most Frequent Subtree Sum 出现频率最高的子树和

    详见:https://leetcode.com/problems/most-frequent-subtree-sum/description/ C++: /** * Definition for a ...

  4. 正则表达式test报错 is not a function

    var reg = "/^1[34578]\d{9}$/"; //错误格式,这是一个字符串 var reg2 = /^1[34578]\d{9}$/; //正确格式 reg .te ...

  5. Unity项目学习笔记

    1.TCP和IP IP:主要作用是在复杂的网络环境中将数据包发送给的最终的目标地址. 端口号:系统会分给系统端口号  一般知名的端口号在0-1023之间,而我们经常使用的自定义/动态分配的端口号则一般 ...

  6. BBS项目总结

    数据库(Oracle): BBSUserid:主键username:用户名password:密码pic:头像 blobpagenum:每个人分页喜好数量,每页显示多少行 Article :ID:主键, ...

  7. CCF|最大波动|Java|100

    import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Sc ...

  8. Bean with name 'xxxService' has been injected into other beans [xxxServiceA,xxxServiceB] in its raw version as part of a circular reference, but has eventually been wrapped

    启动项目,通过@Autowired注入对象,出现循环依赖,导致项目启动失败,具体报错信息如下: Exception encountered during context initialization ...

  9. Android Studio V4 V7 包冲突的问题

    最近被包冲突的问题搞奔溃了,特别是V4,V7 V4和V7包冲突的解决方式就是!版本要一致!! 比如我的一个项目中应用本来是这样引用包的 compile 'com.android.support:sup ...

  10. express搭建平台

    1.nodeJs的安装(npm的安装) nodejs官方下载地址:https://nodejs.org 2.express的安装( $ npm install -g express #全局安装expr ...