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

你可能感兴趣的文章
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>
MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
查看>>
Mysql: 对换(替换)两条记录的同一个字段值
查看>>
mysql:Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘解决方法
查看>>
MYSQL:基础——3N范式的表结构设计
查看>>
MYSQL:基础——触发器
查看>>
Mysql:连接报错“closing inbound before receiving peer‘s close_notify”
查看>>
mysqlbinlog报错unknown variable ‘default-character-set=utf8mb4‘
查看>>