【题解】Luogu P3052 【USACO12】摩天大楼里的奶牛Cows in a Skyscraper
迭代加深搜索基础
题目描述
A little known fact about Bessie and friends is that they love stair climbing races. A better known fact is that cows really don’t like going down stairs. So after the cows finish racing to the top of their favorite skyscraper, they had a problem. Refusing to climb back down using the stairs, the cows are forced to use the elevator in order to get back to the ground floor.
The elevator has a maximum weight capacity of W (1 <= W <= 100,000,000) pounds and cow i weighs C_i (1 <= C_i <= W) pounds. Please help Bessie figure out how to get all the N (1 <= N <= 18) of the cows to the ground floor using the least number of elevator rides. The sum of the weights of the cows on each elevator ride must be no larger than W.
给出n个物品,体积为w[i],现把其分成若干组,要求每组总体积<=W,问最小分组。(n<=18)
输入输出格式
输入格式:
Line 1: N and W separated by a space.
Lines 2..1+N: Line i+1 contains the integer C_i, giving the weight of one of the cows.
输出格式:
Line 1: A single integer, R, indicating the minimum number of elevator rides needed.
Lines 2..1+R: Each line describes the set of cows taking
one of the R trips down the elevator. Each line starts with an integer giving the number of cows in the set, followed by the indices of the individual cows in the set.
输入输出样例
输入样例#1: 复制
4 10
5
6
3
7
输出样例#1: 复制
3
2 1 3
1 2
1 4
说明
There are four cows weighing 5, 6, 3, and 7 pounds. The elevator has a maximum weight capacity of 10 pounds.
We can put the cow weighing 3 on the same elevator as any other cow but the other three cows are too heavy to be combined. For the solution above, elevator ride 1 involves cow #1 and #3, elevator ride 2 involves cow #2, and elevator ride 3 involves cow #4. Several other solutions are possible for this input.
思路
- 迭代加深搜索:这是一种类似广搜的深搜,但是它不需要广搜如此大的空间,它的空间与深搜的空间一样
可以看做带深度限制的DFS。
首先设置一个搜索深度,然后进行DFS,当目前深度达到限制深度后验证当前方案的合理性,更新答案。
不断调整搜索深度,直到找到最优解。
本题中枚举电梯数num,就是搜索的深度 - 剪枝: 可证第i奶牛放到i后车厢没有意义
代码
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define re register int
using namespace std;
int n, m, c[19], tot(0), ans(0), v[19];
bool dfs(int x, int num){
for(re i=1;i<=x&&i<=num;++i)
if(v[i]+c[x]<=m){
v[i]+=c[x];
if(x==n) return 1;
if(dfs(x+1,num)) return 1;
v[i]-=c[x];
}
return 0;
}
int main(){
scanf("%d%d",&n,&m);
for(re i=1;i<=n;++i) scanf("%d", &c[i]);
for(re i=1;i<=n;++i){//枚举厢数
memset(v,0,sizeof(v));
if (dfs(1,i)){
printf("%d\n",i);
break;
}
}
return 0;
}
【题解】Luogu P3052 【USACO12】摩天大楼里的奶牛Cows in a Skyscraper的更多相关文章
- LUOGU P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better k ...
- 洛谷P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 题目描述 A little known fact about Bessie and friends is ...
- 洛谷 P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better k ...
- P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
题目描述 给出n个物品,体积为w[i],现把其分成若干组,要求每组总体积<=W,问最小分组.(n<=18) 输入格式: Line 1: N and W separated by a spa ...
- P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 状压dp
这个状压dp其实很明显,n < 18写在前面了当然是状压.状态其实也很好想,但是有点问题,就是如何判断空间是否够大. 再单开一个g数组,存剩余空间就行了. 题干: 题目描述 A little k ...
- [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
洛谷题目链接:[USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 题目描述 A little known fact about Bessie and friends is ...
- [USACO12MAR] 摩天大楼里的奶牛 Cows in a Skyscraper
题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better k ...
- [bzoj2621] [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
题目链接 状压\(dp\) 根据套路,先设\(f[sta]\)为状态为\(sta\)时所用的最小分组数. 可以发现,这个状态不好转移,无法判断是否可以装下新的一个物品.于是再设一个状态\(g[sta] ...
- [luoguP3052] [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper(DP)
传送门 输出被阉割了. 只输出最少分的组数即可. f 数组为结构体 f[S].cnt 表示集合 S 最少的分组数 f[S].v 表示集合 S 最少分组数下当前组所用的最少容量 f[S] = min(f ...
随机推荐
- 二、postman断言及正则表达式取值
postman老式断言与新式断言总结:本文以微信开发者文档为例 断言处如图所示 一.老式断言 老式断言总结:var variables相当于代码中定义的变量,test['']=true;相当于pyth ...
- lombok,Invalid byte tag in constant pool: 19
今天偶到一个奇怪的问题: 三台生产服务器部署同样的代码,同样的tomcat ,jdk等环境. 其中有一台服务器启动时报lombok-1.18.6.jar! Invalid byte tag in ...
- 序列化-Hessian
引用官网介绍: Hessian is a dynamically-typed, binary serialization and Web Services protocol designed for ...
- Asp.NetCore Web开发之RazorPage
这节讲一下Razor页面. 首先要明确,Razor 不是一种编程语言.它是服务器端的标记语言,配合C#语言,就可以像PHP语言语言一样(但它们并不相同),处理HTML页面逻辑.它是Asp.NetCor ...
- 简单说几个MySQL高频面试题
前言: 在各类技术岗位面试中,似乎 MySQL 相关问题经常被问到.无论你面试开发岗位或运维岗位,总会问几道数据库问题.经常有小伙伴私信我,询问如何应对 MySQL 面试题.其实很多面试题都是大同小异 ...
- Zabbix 5.0 优化建议
Blog:博客园 个人 在使用Zabbix过程中,正确的调整Zabbix系统,使之保持高性能是非常重要的,能够充分利用硬件资源,监控更多主机和性能指标. 硬件 关于zabbix server端硬件的建 ...
- 【近取 key】Alpha 阶段任务分配
项目 内容 这个作业属于哪个课程 2021春季计算机学院软件工程(罗杰 任健) 这个作业的要求在哪里 alpha阶段初始任务分配 我在这个课程的目标是 进一步提升工程化开发能力,积累团队协作经验,熟悉 ...
- CMMI V2.0丨如何通过CMMI真正在企业中的实施规模化敏捷开发
在过去的几年中,敏捷开发已经从一个利基概念(利基是指针对企业的优势细分出来的市场,这个市场不大,而且没有得到令人满意的服务.产品推进这个市场,有盈利的基础.)转变为全球许多大公司采用的标准实践. 通过 ...
- [DB] Spark Core (2)
RDD WordCount处理流程 sc.textFile("/root/temp/data.txt").flatMap(_.split(" ")).map(( ...
- Linux_配置辅助DNS服务(基础)
[RHEL8]-DNSserver1:[RHEL7]-DNSserver2:[Centos7]-DNSclient !!!测试环境我们首关闭防火墙和selinux(DNSserver1.DNSserv ...