博客
关于我
【代码超详解】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 binlog三种模式
查看>>
multi-angle cosine and sines
查看>>
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>
Multicast1
查看>>
mysql client library_MySQL数据库之zabbix3.x安装出现“configure: error: Not found mysqlclient library”的解决办法...
查看>>
MySQL Cluster 7.0.36 发布
查看>>
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
查看>>
MySQL Cluster与MGR集群实战
查看>>
multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
查看>>
mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
查看>>
Multiple websites on single instance of IIS
查看>>
mysql CONCAT()函数拼接有NULL
查看>>
multiprocessing.Manager 嵌套共享对象不适用于队列
查看>>
multiprocessing.pool.map 和带有两个参数的函数
查看>>
MYSQL CONCAT函数
查看>>
multiprocessing.Pool:map_async 和 imap 有什么区别?
查看>>
MySQL Connector/Net 句柄泄露
查看>>
multiprocessor(中)
查看>>
mysql CPU使用率过高的一次处理经历
查看>>