【题解】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 ...
随机推荐
- 浅入浅出 MySQL 索引
简单了解索引 首先,索引(Index)是什么?如果我直接告诉你索引是数据库管理系统中的一个有序的数据结构,你可能会有点懵逼. 为了避免这种情况,我打算举几个例子来帮助你更容易的认识索引. 我们查询字典 ...
- valgrind 内存泄漏分析
概述 valgrind 官网 https://www.valgrind.org/ valgrind 是 Linux 业界主流且非常强大的内存泄漏检查工具.在其官网介绍中,内存检查(memcheck)只 ...
- CAS的理解
CAS(CompareAndSweep)工作方式 CAS是乐观锁技术,当多个线程尝试使用CAS同时更新同一个变量时,只有其中一个线程能更新变量的值,而其它线程都失败,失败的线程并不会被挂起,而是被 ...
- Javac·编码GBK的不可映射字符
阅文时长 | 0.04分钟 字数统计 | 79.2字符 主要内容 | 1.引言&背景 2.声明与参考资料 『Javac·编码GBK的不可映射字符』 编写人 | SCscHero 编写时间 | ...
- zabbix screen 图片以邮件形式发送
zabbix screen 图片以邮件形式发送 #! /usr/bin/env python #coding=utf-8 # Andy_f import time,os import urllib i ...
- SVN库迁移到GitHub
创建新目录,cmd进入到新目录,执行如下命令: git svn init svn://10.10.10.10/net/QA_Dept git svn fetch git remote add orig ...
- 串口配合DMA接收不定长数据(空闲中断+DMA接收)-(转载)
1.空闲中断和别的接收完成(一个字节)中断,发送完成(发送寄存器控)中断的一样是串口中断: 2.空闲中断是接收到一个数据以后,接收停顿超过一字节时间 认为桢收完,总线空闲中断是在检测到在接收数据后, ...
- spring mvc下实现通过邮箱找回密码功能
1功能分析 通过spring mvc框架实现通过邮箱找回密码. 2 实现分析 主要是借助某个邮箱的pop3/smtp服务实现的邮件代发功能. 3 源码分析 3.1首先在用户表对应的javabean中加 ...
- 使用 .NET 升级助手将.NET Framework应用迁移到.NET 5
从.NET Framework 迁移到.NET 5 犹如搬家,我们都知道搬家是很痛苦的,我们请求搬家公司来减轻我们的压力,.NET 升级助手 的作用就类似我们聘请的搬家公司,帮助我们处理繁重乏味的迁移 ...
- python实战项目练习-Django商城项目之注册功能实现
设计到的前端知识 项目的前端页面使用vue来实现局部刷新,通过数据的双向绑定实现与用户的交互,下面来看一下需求,在用户输入内容后,前端需要做一些简单的规则校验,我们希望在在用户输入后能够实时检测,如果 ...