POJ 2923 状压好题
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2631 | Accepted: 1075 |
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
Source
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
using namespace std; #define N 100005
#define inf 999999999 int n, c1, c2;
int w[];
int num[]; bool solve(int nn){
int i, j, k;
int sum=;
bool visited[];
memset(visited,false,sizeof(visited));
visited[]=true;
for(i=;i<n;i++){
if((<<i)&nn){
sum+=w[i];
for(j=c1-w[i];j>=;j--){//DP
if(visited[j])
visited[j+w[i]]=true;
}
}
}
for(i=;i<=c1;i++){
if(visited[i]&&sum-i<=c2)
return true;
}
return false;
} main()
{
int t, i, j, k;
int kase=;
cin>>t;
while(t--){
scanf("%d %d %d",&n,&c1,&c2);
for(i=;i<n;i++) scanf("%d",&w[i]);
int len=;
int cnt=(<<n)-;
for(i=;i<=cnt;i++){
if(solve(i))
num[len++]=i;
}
int dp[];
for(i=;i<=cnt;i++) dp[i]=inf;
dp[]=;
for(i=;i<len;i++){//DP
for(j=cnt;j>=;j--){
if(!(j&num[i])){
dp[j|num[i]]=min(dp[j|num[i]],dp[j]+);
}
}
}
printf("Scenario #%d:\n%d\n\n",kase++,dp[cnt]);
}
}
POJ 2923 状压好题的更多相关文章
- poj 2923 状压dp+01背包
好牛b的思路 题意:一系列物品,用二辆车运送,求运送完所需的最小次数,两辆车必须一起走 解法为状态压缩DP+背包,本题的解题思路是先枚举选择若干个时的状态,总状态量为1<<n,判断这些状态 ...
- POJ 3254 (状压DP) Corn Fields
基础的状压DP,因为是将状态压缩到一个整数中,所以会涉及到很多比较巧妙的位运算. 我们可以先把输入中每行的01压缩成一个整数. 判断一个状态是否有相邻1: 如果 x & (x << ...
- POJ 3254 状压DP
题目大意: 一个农民有一片n行m列 的农场 n和m 范围[1,12] 对于每一块土地 ,1代表可以种地,0代表不能种. 因为农夫要种草喂牛,牛吃草不能挨着,所以农夫种菜的每一块都不能有公共边. ...
- poj 1170状压dp
题目链接:https://vjudge.net/problem/POJ-1170 题意:输入n,表示有那种物品,接下来n行,每行a,b,c三个变量,a表示物品种类,b是物品数量,c代表物品的单价.接下 ...
- POJ 2411 状压DP经典
Mondriaan's Dream Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 16771 Accepted: 968 ...
- poj 3254 状压dp入门题
1.poj 3254 Corn Fields 状态压缩dp入门题 2.总结:二进制实在巧妙,以前从来没想过可以这样用. 题意:n行m列,1表示肥沃,0表示贫瘠,把牛放在肥沃处,要求所有牛不能相 ...
- POJ 3254 状压DP(基础题)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17749 Accepted: 9342 Desc ...
- Islands and Bridges(POJ 2288状压dp)
题意:给你一个图和每个点的价值,边权值为连接两点权值的积,走哈密顿通路,若到达的点和上上个点相连则价值加三点乘积,求哈密顿通路的最大价值,和最大价值哈密顿通路的条数. 分析:开始看这个题很吓人,但想想 ...
- poj 1185(状压dp)
题目链接:http://poj.org/problem?id=1185 思路:状态压缩经典题目,dp[i][j][k]表示第i行状态为j,(i-1)行状态为k时最多可以放置的士兵个数,于是我们可以得到 ...
随机推荐
- 描述性统计分析-用脚本将统计量函数批量化&分步骤逐一写出
计算各种描述性统计量函数脚本(myDescriptStat.R)如下: myDescriptStat <- function(x){ n <- length(x) #样本数据个数 m &l ...
- [bzoj 3732] Network (Kruskal重构树)
kruskal重构树 Description 给你N个点的无向图 (1 <= N <= 15,000),记为:1-N. 图中有M条边 (1 <= M <= 30,000) ,第 ...
- LINQ的基本认识
前些日子,我的一个兄弟问我一个关于LINQ的问题,他问我AsEnumerable()在他写的一大段代码中的作用. 我不太清楚他是知道想考考我,还是不太清楚,想问题一下,反正我不太知道. 以前接触过一些 ...
- Executor框架(转载)
Executor框架是指java 5中引入的一系列并发库中与executor相关的一些功能类,其中包括线程池,Executor,Executors,ExecutorService,Completion ...
- 通过js引用外部脚本(嘿嘿,方便直接在浏览器上调试抓取代码)
最近折腾爬虫,后端使用jQuery进行数据采集,一般都是先从浏览器中将采集代码调试好后直接放到后端跑了. 有些网址没有引用jQuery,那调试起来就不方便了,可以用以下代码动态添加script标签,将 ...
- Xcode 7免证书真机调试
在Xcode 7中,苹果改变了自己在许可权限上的策略,此前Xcode只开放给注册开发者下载,但Xcode 7改变了这种惯有的做法,无需注册开发者账号,仅使用普通的Apple ID就能下载和上手体验.此 ...
- android应用内存使用情况
单个应用程序最大内存限制,超过这个值会产生OOM(内存溢出) 命令:adb shell ->dalvik.vm.heapgrowthlimit 应用启动后分配的初始内存 命令:adb shell ...
- spark的standlone模式安装和application 提交
spark的standlone模式安装 安装一个standlone模式的spark集群,这里是最基本的安装,并测试一下如何进行任务提交. require:提前安装好jdk 1.7.0_80 :scal ...
- Mac 安装 MySQL
在 Mac 下用 Homebrew 安装 MySQL, 网上的教程倒是很多,不过大多数都很默契地雷同.如果稍有点定制要求,就无从下手了. 我先也不免俗,从基本的开始: 一.首先安装 Homebrew ...
- TAG-9F10 发卡行相关数据(转)
Tag-9F10 发卡行相关数据 9F10-发卡行应用数据 标签:9F10 字节1 –“ 07” 字节2 –DKI 长度:var10-25字节 字节3 – 密文版本号 格式:b 字节4 –“ 03” ...