poj2923 Relocation
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
这道题是用状态压缩+01背包,因为物品只有10件,所以会想到状压dp,先把所有的物品都用0和1表示,0表示物品还没有被搬走,1表示已经被搬走,用dp[state]记录从所有物品都未被搬走到状态state所要搬的最少次数。可以先算出哪些状态是一次就能搬走的,这里要用到01背包的思想,先看看该状态下c1可以容纳多少重量,然后用看看把这些重量搬走后剩下的质量能不能被c2所容纳。这些都处理完后,就用状态转移方程dp[j|state]=min(dp[j|state,dp[state]+1},这个方程很巧妙啊,开始还以为是两个能一次搬走的状态结合,其实是利用能一次搬走,所以在之前的基础上加上这个一次能搬走的状态。最后的结果就是dp[1<<(n-1)]啦。
#include<stdio.h>
#include<string.h>
#define inf 88888888
int c1,c2,n,w[15],state,yici[1500],vis[150],dp[1500];
int min(int x,int y){
return x<y?x:y;
}
int panduan(int x)
{
int i,j,sum=0;
memset(vis,0,sizeof(vis));
vis[0]=1;
for(i=1;i<=n;i++){
if((1<<(i-1))&x){
sum+=w[i];
for(j=c1;j>=w[i];j--){
if(vis[j-w[i]]){
vis[j]++;
}
}
}
}
if(sum>c1+c2)return 0;
for(i=0;i<=c1;i++){
if(vis[i] && sum-i<=c2)return 1;
}
return 0;
}
int main()
{
int m,i,j,T,t,num1=0;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&c1,&c2);
for(i=1;i<=n;i++){
scanf("%d",&w[i]);
}
t=0;
memset(yici,0,sizeof(yici));
for(i=1;i<(1<<n);i++){
dp[i]=inf;
if(panduan(i)){
yici[++t]=i;
dp[i]=1;
}
}
dp[0]=0;
for(i=1;i<(1<<n);i++){
for(j=1;j<=t;j++){
if(yici[j]&i)continue;
dp[i|yici[j]]=min(dp[i|yici[j]],dp[i]+1);
}
}
num1++;
printf("Scenario #%d:\n",num1);
printf("%d\n\n",dp[(1<<n)-1]);
}
return 0;
}
poj2923 Relocation的更多相关文章
- Relocation POJ-2923
题目链接 题目意思: 有 n 个货物,并且知道了每个货物的重量,每次用载重量分别为c1,c2的火车装载,问最少需要运送多少次可以将货物运完. 分析:本题可以用二进制枚举所有不冲突的方案,再来dp 一下 ...
- [poj2923]Relocation_状压dp_01背包
Relocation poj-2923 题目大意:给出n个物品,有两辆车,两辆车必须一起出动并且每辆车有单独的容量.问最少需要运输多少次才能运走所有货物. 注释:n<=10,容量,物品代价< ...
- relocation 错误
icc test/train/test_lm2.o -shared -lpthread -ldl ./lib/liblm2.a -o liblm2.so ld: ./lib/liblm2.a(cJSO ...
- ffmpeg relocation error
在向imx6移植ffmpeg后,一般的编解码操作没有问题,但是当从摄像头录视频时, ffmpeg -f video4linux2 -s 640*480 -r 10 -i /dev/video0 tes ...
- rac one node在线relocation
1.查看数据库运行状态 $ srvctl status database -d rone Instance rone_2 is running on node rone2 Online relocat ...
- uboot的relocation原理具体分析
近期在一直在做uboot的移植工作,uboot中有非常多值得学习的东西.之前总结过uboot的启动流程,但uboot一个非常核心的功能没有细致研究.就是uboot的relocation功能. 这几天研 ...
- Relocation 状态压缩DP
Relocation Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- SVN报E155024: Invalid relocation destination
大家开发过程会遇到一个场景! 我们在使用SVN版本管理工具进行开发的过程中,前一个版本在Branch->201803 分支开发完成之后,后一版本要求在Branch->201804版本开发 ...
- POJ-2923 Relocation---01背包+状态压缩
题目链接: https://vjudge.net/problem/POJ-2923 题目大意: 有n个货物,给出每个货物的重量,每次用容量为c1,c2的火车运输,问最少需要运送多少次可以将货物运完 思 ...
随机推荐
- PAT天梯赛练习 L3-003 社交集群 (30分) DFS搜索
题目分析: 一共有N个编号为1~1000的人,以及一共有编号为1~1000种不同的兴趣,在题目给出1~N编号的人员每个人喜欢的兴趣的id后,要求统计出不同的人员集合的个数以及每个人员几个的人数从大到小 ...
- python中re模块的使用(正则表达式)
一.什么是正则表达式? 正则表达式,又称规则表达式,通常被用来检索.替换那些符合某个模式(规则)的文本. 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合, ...
- DC-DC变换器,24v转5v稳压芯片,3A输出电流
在24V输入中,比较合适的LDO可以选择:PW6206,输出电压3V,3.3V,5V 输入电压最高40V,功耗也低4uA左右,采用SOT23-3封装. PW6206系列是一个高精度,高输入电压低静态电 ...
- 【Android】关于连续多次点击控件的控制方案(新建监听类)
参考:防止Android过快点击造成多次事件的三种方法_胖胖的博客-CSDN博客 实现逻辑很简单: 设置限定时间 在用户点击时开始计时 若计时未超过限定时间,则不允许触发点击事件 因还未学习过Rxja ...
- websocket的应用---Django
websocket的应用---Django 1.长轮询 轮询:在前端通过写js实现.缺点:有延迟.服务器压力大. 就是客户端通过一定的时间间隔以频繁请求的方式向服务器发送请求,来保持客户端和服务器端的 ...
- python的Counter类
python的Counter类 Counter 集成于 dict 类,因此也可以使用字典的方法,此类返回一个以元素为 key .元素个数为 value 的 Counter 对象集合 from coll ...
- Linux中LPC、RPC、IPC的区别
其实这玩意儿就是纸老虎,将英文缩写翻译为中文就明白一半了. IPC:(Inter Process Communication )跨进程通信 这个概念泛指进程之间任何形式的通信行为,是个可以拿来到处套的 ...
- Socket的用法——NIO包下SocketChannel的用法 ———————————————— 版权声明:本文为CSDN博主「茶_小哥」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/ycgslh/article/details/79604074
服务端代码实现如下,其中包括一个静态内部类Handler来作为处理器,处理不同的操作.注意在遍历选择键集合时,没处理完一个操作,要将该请求在集合中移除./*模拟服务端-nio-Socket实现*/pu ...
- list中map 的value值时间排序
public static void main(String[] args) { String sys=DateUtil.getTime().substring(0,5); System.out.pr ...
- URI与URL傻傻分不清楚?
前言 总所周知,缓存是解决Http1.1协议传输性能的问题中最主要的手段. 缓存既可以存在于浏览器上,也可以存在于服务器中. 而影响缓存的Http头部有很多,其中Cache-Control是比较重要的 ...