比较巧妙的一道题目,拿到题目就想用暴力直接搜索,仔细分析了下发现复杂度达到了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. 使用Python爬虫爬取网络美女图片

    代码地址如下:http://www.demodashi.com/demo/13500.html 准备工作 安装python3.6 略 安装requests库(用于请求静态页面) pip install ...

  2. bootstrap学习笔记 插件概述

    Bootstrap插件概览 在前面布局组件章节中所讨论的组件仅仅是个开始.Bootstrap自带的12种jQuery插件,扩展了功能,可以给站点添加更多的互动.即使您不是一名高级的js开发人员, 你也 ...

  3. asp.net 抓取新闻

    前台页面: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...

  4. IOT表

    以前在接触索引的时候,就想过要是表字段太少,索引效果不是很不好吗,直接用索引不是更直接吗?后来因为懒惰也没有去查找相关资料.正好今天看到了table organization index,看了一下,实 ...

  5. MVC数据验证Model Validation

    Required必须项验证属性 [Required] public string FirstName { get; set; } //ID编号 [ScaffoldColumn(false)] [Req ...

  6. homebrew osx下面最优秀的包管理工具

    官方网站:https://brew.sh/index_zh-cn 安装 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubuserconten ...

  7. Atitit.故障排除系列---NoClassDefFoundError  NoClassDefFoundError ClassNotFoundException

    Atitit.故障排除系列---NoClassDefFoundError  NoClassDefFoundError ClassNotFoundException 1. java.lang.Class ...

  8. 李洪强iOS开发之iOS技术博客

    李洪强iOS开发之iOS技术博客 注意:访问博客请直接点击博客,不要点击后面的RSS地址 博客地址 RSS地址 南峰子的技术博客   剑尖博客   图拉鼎   Henry Lee   Dev Talk ...

  9. linux学习笔记9--命令cat

    cat命令的用途是连接文件或标准输入并打印.这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用. cat命令连接文件并打印到标准输出设备上, ...

  10. centos yum安装及手动编译ettercap

    眼下流行的软件包有二种形式 ,一种是以rpm包,deb包为代表的智能安装包.还有一种是以file.tar.gz形式的压缩 一 智能安装 以 mysql为例 yum search mysqld 二 手动 ...