博客
关于我
【代码超详解】CometOJ C0302 [USACO]健康的荷斯坦奶牛(暴力枚举,3 ms)
阅读量:719 次
发布时间:2019-03-21

本文共 1175 字,大约阅读时间需要 3 分钟。

一、题目描述

在这里插入图片描述

二、算法分析说明与代码编写指导

通过递归进行暴力枚举,递归函数的参数 cur 是当前正在考虑是否选择的饲料。

递归中止的条件是最后一种饲料是否选择亦考虑完毕。
进入每层递归,即正在判断每种可能的选择时,统计已选择的饲料含有的每种维生素数量是否都满足要求。如果满足,且当前方案选择的饲料种类数比已有方案更少,就将最少方案更新为当前的方案。用长度为 15 的 bitset b 和 b0 分别保存当前的选择方案和已经发现的最优选择方案。
第 cur 种饲料选择后,要将该饲料含有的各种维生素的数量加入到已选的饲料可提供的每种维生素的总数中,这个总数用数组 s 表示。当枚举不选择该种饲料的情形时,相应的维生素数量要扣除。

三、AC 代码(3 ms)

#include
#include
using namespace std;#pragma warning(disable:4996)unsigned V, m[25], G, c[15][25], s[25]; bitset<15> b, b0; bool ok;void dfs(const unsigned& cur) { ok = true; for (unsigned i = 0; i < V; ++i)if (s[i] < m[i])ok = false; if (ok == true && b.count() < b0.count()) { b0 = b; } if (cur == G)return; b[cur] = 1; for (unsigned i = 0; i < V; ++i) { s[i] += c[cur][i]; } dfs(cur + 1); b[cur] = 0; for (unsigned i = 0; i < V; ++i)s[i] -= c[cur][i]; dfs(cur + 1);}int main() { scanf("%u", &V); for (unsigned i = 0; i < V; ++i)scanf("%u", &m[i]); scanf("%u", &G); for (unsigned i = 0; i < G; ++i) for (unsigned j = 0; j < V; ++j) scanf("%u", &c[i][j]); b0.set(); dfs(0); printf("%u", b0.count()); for (unsigned i = 0, j = 0; j < b0.count(); ++i)if (b0[i] == true) { printf(" %u", i + 1); ++j; } return 0;}

转载地址:http://bltez.baihongyu.com/

你可能感兴趣的文章
mysql启动和关闭外键约束的方法(FOREIGN_KEY_CHECKS)
查看>>
Mysql启动失败解决过程
查看>>
MySQL启动失败:Can't start server: Bind on TCP/IP port
查看>>
mysql启动报错
查看>>
mysql启动报错The server quit without updating PID file几种解决办法
查看>>
MySQL命令行登陆,远程登陆MySQL
查看>>
mysql命令:set sql_log_bin=on/off
查看>>
mySQL和Hive的区别
查看>>
MySQL和Java数据类型对应
查看>>
mysql和oorcale日期区间查询【含左右区间问题】
查看>>
MySQL和SQL入门
查看>>
mysql在centos下用命令批量导入报错_Variable ‘character_set_client‘ can‘t be set to the value of ‘---linux工作笔记042
查看>>
Mysql在Linux运行时新增配置文件提示:World-wrirable config file ‘/etc/mysql/conf.d/my.cnf‘ is ignored 权限过高导致
查看>>
Mysql在Windows上离线安装与配置
查看>>
MySQL在渗透测试中的应用
查看>>
Mysql在离线安装时启动失败:mysql服务无法启动,服务没有报告任何错误
查看>>
Mysql在离线安装时提示:error: Found option without preceding group in config file
查看>>
MySQL基于SSL的主从复制
查看>>
Mysql基本操作
查看>>
mysql基本操作
查看>>