Relocation(状压DP)
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:
- At their old place, they will put furniture on both cars.
- Then, they will drive to their new place with the two cars and carry the furniture upstairs.
- 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 n, C1 and C2. C1 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)的更多相关文章
- HDU 2923 Relocation(状压dp+01背包)
题目代号:HDU2923 题目链接:http://poj.org/problem?id=2923 Relocation Time Limit: 1000MS Memory Limit: 65536K ...
- 【POJ 2923】Relocation(状压DP+DP)
题意是给你n个物品,每次两辆车运,容量分别是c1,c2,求最少运送次数.好像不是很好想,我看了网上的题解才做出来.先用状压DP计算i状态下,第一辆可以运送的重量,用该状态的重量总和-第一辆可以运送的, ...
- POJ 2923 Relocation(状压DP)题解
题意:有2辆车运货,每次同时出发,n(<10),各自装货容量c1 c2,问最少运几次运完. 思路:n比较小,打表打出所有能运的组合方式,用背包求出是否能一次运走.然后状压DP运的顺序. 代码: ...
- BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3336 Solved: 1936[Submit][ ...
- nefu1109 游戏争霸赛(状压dp)
题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1109 //我们校赛的一个题,状压dp,还在的人用1表示,被淘汰 ...
- poj3311 TSP经典状压dp(Traveling Saleman Problem)
题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...
- [NOIP2016]愤怒的小鸟 D2 T3 状压DP
[NOIP2016]愤怒的小鸟 D2 T3 Description Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于(0,0)处,每次Kiana可 ...
- 【BZOJ2073】[POI2004]PRZ 状压DP
[BZOJ2073][POI2004]PRZ Description 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 桥已经很旧了, 所以它不能承受太重的东西. 任何时候队伍 ...
- bzoj3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一(spfa+状压DP)
数据最多14个有宝藏的地方,所以可以想到用状压dp 可以先预处理出每个i到j的路径中最小权值的最大值dis[i][j] 本来想用Floyd写,无奈太弱调不出来..后来改用spfa 然后进行dp,这基本 ...
- HDU 1074 Doing Homework (状压dp)
题意:给你N(<=15)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则 ...
随机推荐
- josephus 问题的算法(转载)
Josephus 问题: 一群小孩围成一个圈,任意假定一个数 m,从第一个小孩起,顺时针方向数,每数到第 m 个小孩时,该小孩便离开.小孩不断离开,圈子不断缩小,最后剩下的一个小孩便是胜利者.究竟胜利 ...
- ImageView加载长图(适用不需要缩放的情况)
此案例适用于加载网络长图且图片的宽和高已知的情况.由于ImageView加载图片有一个4096*4096的限制,所以对于巨长图的加载比较麻烦,需要我们自己去手动处理. 有两种解决方案:第一种就是比较l ...
- NIO服务端主要创建过程
NIO服务端主要创建过程: 步骤一:打开ServerSocketChannel,用于监听客户端的连接,它是所有客户端连接的副管道,示例代码如下: ServerSocketChannel ...
- 014、BOM与DOM对象的应用
Screen屏幕对象 Width:屏幕的宽度 Height:屏幕的高度 availWidth:屏幕的有效宽度(不含任务栏) availHeight:屏幕的有效高度(不含任务栏) colorDepth: ...
- 十个 JDBC 的最佳实践
JDBC是Java为多种关系型数据库提供的统一的访问接口,以下是我长期使用JDBC总结的十个最佳实践. 1. 使用PrearedStatement 任何一个使用过JDBC的Java程序员几乎都知道这个 ...
- redis源码分析之事务Transaction(下)
接着上一篇,这篇文章分析一下redis事务操作中multi,exec,discard三个核心命令. 原文地址:http://www.jianshu.com/p/e22615586595 看本篇文章前需 ...
- android开发中设置字体
转自:http://segmentfault.com/q/1010000000494116 http://ryanhoo.github.io/blog/2014/05/05/android-bette ...
- 一行命令杀掉defunct进程
一行命令杀掉defunct进程 今天在杀掉defunc过程中一直搞不完,索性写一行命令,注意先看懂谨慎使用 ps -ef|grep defunct|awk '{print " ps -ef| ...
- TCP和流
http://www.cnblogs.com/lwzz/archive/2011/07/03/2096963.html TCP是一种流协议(stream protocol).这就意味着数据是以字节流的 ...
- linux下C的建立、编译和运行 gcc (附上Windows下visual c++的用法)
2019/6/24 1. 环境:window10下安装了MobaXterm,这里申请了阿里云的服务账号,可以直接使用linux系统,避免安装虚拟机等. 2. 判断linux下是否有GCC编译工具(我们 ...