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. memcache的分布式配置

    public static class MemcacheHelper { private static MemcachedClient mc; static MemcacheHelper() { St ...

  2. LN : leetcode 684 Redundant Connection

    lc 684 Redundant Connection 684 Redundant Connection In this problem, a tree is an undirected graph ...

  3. Android学习笔记(十六) ContentProvider

    1.相关概念 ContentProvider:不同应用程序之间进行数据交换的标准API:程序“暴露”数据的方法. ContentResolver:一个程序访问另一个程序被“暴露”的数据的方法. Uri ...

  4. redis集群架构(含面试题解析)

    老规矩,我还是以循序渐进的方式来讲,我一共经历过三套集群架构的演进! Replication+Sentinel 这套架构使用的是社区版本推出的原生高可用解决方案,其架构图如下! 这里Sentinel的 ...

  5. ubuntu下安装memcache及memcached

    memcache 和 memcached 有什么区别呢? memcache最早是在2004年2月开发的,而memcached最早是在2009年1月开发的.所以memcache的历史比memcached ...

  6. 【HEVC帧间预测论文】P1.6 A Fast HEVC Inter CU Selection Method Based on Pyramid Motion Divergence

    A Fast HEVC Inter CU Selection Method Based on Pyramid Motion Divergence <HEVC标准介绍.HEVC帧间预测论文笔记&g ...

  7. 《深入理解Java虚拟机》读书笔记

    堆分配参数: -XX:+PrintGC 使用该参数,虚拟机启动后,只要遇到GC就会打印日志: -XX:+UseSerialGC 配置串行回收器: -XX:+PrintGCDeltails 可以查看详细 ...

  8. MATLAB GUI制作快速入门

    创建空白的GUI在MATLAB命令行中输入guide新建GUI,选择Blank GUI (Default),点击确定后就生成了一个空白的GUI制作界面,如下图所示 图1制作GUI的具体过程简单加法器将 ...

  9. mysql-mmm 部署高可用集群

    集群的部署(mysql-mmm + 主从同步) 什么是集群? 多台服务器提供相同的服务 集群的好处? 节省成本. 配置集群? 准备环境:准备4台数据库服务器 准备服务器,能够ping 4 台数据库服务 ...

  10. flask学习规划

    实现基本登录注册: 留言板功能: 并且部署到服务器: 预计完成时间6.16:. 后续页面美化: 各种其他功能的探索一个月时间: 并行地学习python,java,css,html,js,sql 暑假预 ...