比较巧妙的一道题目,拿到题目就想用暴力直接搜索,仔细分析了下发现复杂度达到了2^n*n! ,明显不行,于是只好往背包上想。 于是又想二分找次数判断可行的方法,但是发现复杂度10^8还是很悬。。。
然后学习了这种背包状压的好思路, 巧妙的转化成了背包的模型。 一道经典的题目!
 
Relocation
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1664   Accepted: 678

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

Source

TUD Programming Contest 2006, Darmstadt, Germany
 
 
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <string>
using namespace std;
#define INF 0x3ffffff int c1,c2,n;
int g[];
int save[];
int dp[]; int check(int s)
{
int tg[];
int cnt=;
int sum=;
for(int i=;i<n;i++)
{
if( ((<<i)&s) != )
{
tg[cnt++]=g[i];
sum+=g[i];
}
}
for(int i=;i<(<<cnt);i++)
{
int tmp,tmp1;
tmp=tmp1=;
for(int j=;j<cnt;j++)
{
if( ((<<j)&i)!=)
{
tmp+=tg[j];
}
}
tmp1=sum-tmp;
if(tmp<=c1&&tmp1<=c2)
{
return ;
}
}
// tmp tmp1 分别代表放入的不同地方
return ;
} int main()
{
int T;
scanf("%d",&T);
int tt=;
while(T--)
{
scanf("%d%d%d",&n,&c1,&c2);
for(int i=;i<n;i++)
scanf("%d",g+i);
int cnt=;
for(int i=;i<(<<n);i++)//不能从0开始吧
{
if(check(i)==)
{
save[cnt++]=i;
}
}
// 求出所有可以当成一次背包的所有情况
// 从000000 -> 111111
for(int i=;i<(<<n);i++)
dp[i]=INF;
dp[]=;
for(int i=;i<cnt;i++)
{
for(int j=(<<n);j>=save[i];j--)
{
if( (j| save[i] ) != j) continue;
dp[j]=min(dp[j],dp[j^save[i]]+);
}
} printf("Scenario #%d:\n",tt++);
printf("%d\n",dp[(<<n)-]);
printf("\n");
}
return ;
}

poj 2923(状态压缩+背包)的更多相关文章

  1. poj 2923(状态压缩dp)

    题意:就是给了你一些货物的重量,然后给了两辆车一次的载重,让你求出最少的运输次数. 分析:首先要从一辆车入手,搜出所有的一次能够运的所有状态,然后把两辆车的状态进行合并,最后就是解决了,有两种方法: ...

  2. POJ 2923 Relocation(01背包变形, 状态压缩DP)

    Q: 如何判断几件物品能否被 2 辆车一次拉走? A: DP 问题. 先 dp 求解第一辆车能够装下的最大的重量, 然后计算剩下的重量之和是否小于第二辆车的 capacity, 若小于, 这 OK. ...

  3. HDU 4739 Zhuge Liang's Mines (状态压缩+背包DP)

    题意 给定平面直角坐标系内的N(N <= 20)个点,每四个点构成一个正方形可以消去,问最多可以消去几个点. 思路 比赛的时候暴力dfs+O(n^4)枚举写过了--无意间看到有题解用状压DP(这 ...

  4. poj 3254(状态压缩+动态规划)

    http://poj.org/problem?id=3254 题意:有一个n*m的农场(01矩阵),其中1表示种了草可以放牛,0表示没种草不能放牛,并且如果某个地方放了牛,它的上下左右四个方向都不能放 ...

  5. POJ 1185 状态压缩DP(转)

    1. 为何状态压缩: 棋盘规模为n*m,且m≤10,如果用一个int表示一行上棋子的状态,足以表示m≤10所要求的范围.故想到用int s[num].至于开多大的数组,可以自己用DFS搜索试试看:也可 ...

  6. POJ 2923 【01背包+状态压缩/状压DP】

    题目链接 Emma and Eric are moving to their new house they bought after returning from their honeymoon. F ...

  7. POJ 1185 状态压缩DP 炮兵阵地

    题目直达车:   POJ 1185 炮兵阵地 分析: 列( <=10 )的数据比较小, 一般会想到状压DP. Ⅰ.如果一行10全个‘P’,满足题意的状态不超过60种(可手动枚举). Ⅱ.用DFS ...

  8. poj 1324 状态压缩+bfs

    http://poj.org/problem?id=1324 Holedox Moving Time Limit: 5000MS   Memory Limit: 65536K Total Submis ...

  9. HDU 4281 (状态压缩+背包+MTSP)

    Judges' response Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. Cassandra VS HBase

    HBase(dfs三副本,syncwal) Cassandra(N=3,W=2,R=2, batch commitlog) CAP CP CA 数据存储模型 LSM LSM 数据写入网络开销 Rpc  ...

  2. Arm Cache学习总结

    cache,高速缓存,其原始意义是指访问速度比一般随机存取内存(RAM)快的一种RAM,通常它不像系统主存那样使用DRAM技术,而使用昂贵但较快速的SRAM技术. 1.cache映射方式 cache中 ...

  3. jquery easyUi columns日期格式化

    jquery easyUi  columns日期格式化 方法一 Date.prototype.format = function (format) { var o = { "M+" ...

  4. CodeSmith单表生成实体模板与生成多表实体模板

    生成单实体模板: <%@ Template Language="C#" TargetLanguage="C#" %> <%@ Assembly ...

  5. Fiddler设置代理抓手机包

    启动Fiddler,打开菜单栏中的 Tools > Fiddler Options,打开“Fiddler Options”对话框. 在Fiddler Options”对话框切换到“Connect ...

  6. SQL 2005示例库(转载)

    sql2005数据库实例 从网上找还得麻烦,转了过来,点击就可以下载! 在学习SQL2005中离开不了SQL2005示例数据库,AdventureWorks数据库下载安装,,northwind数据库下 ...

  7. Bash中的括号(三)

    1.两个小括号用来对整数进行算术运算和逻辑运算,比如. 例如给变量赋值: $ a=+; echo $a + $ (( b = + )); echo $b 1+1 只是一个字符串,而 b 就是一个算术表 ...

  8. ListView局部更新(非notifyDataSetChanged)

    package com.example.test; import java.util.ArrayList; import java.util.List; import android.app.Acti ...

  9. 使用tar+lz4/pigz+ssh更快的数据传输

    使用tar+lz4/pigz+ssh更快的数据传输 -- | :41分类:Linux,MySQL | 前面一篇介绍了如何最大限度的榨取SCP的传输速度,有了这个基础,就可以进一步的使用压缩来加速传输速 ...

  10. Unity 扩展编辑器

    扩展Inspector界面 继承自Editor,添加CustomEditorAttribute,传入定制的类型 定制显示的类型要求: 类型中所有的public 字段都会自动暴露给Inspector编辑 ...