博客
关于我
【代码超详解】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/

你可能感兴趣的文章
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No static resource favicon.ico.
查看>>
no such file or directory AndroidManifest.xml
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>
no1
查看>>
NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
查看>>
NOAA(美国海洋和大气管理局)气象数据获取与POI点数据获取
查看>>
NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
查看>>
node
查看>>
node exporter完整版
查看>>
node HelloWorld入门篇
查看>>
Node JS: < 一> 初识Node JS
查看>>
Node JS: < 二> Node JS例子解析
查看>>