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

你可能感兴趣的文章
Nginx负载均衡与动静分离架构实现
查看>>
Nginx负载均衡和F5的区别---系统运维工作笔记001
查看>>
nginx负载均衡和反相代理的配置
查看>>
nginx负载均衡器处理session共享的几种方法(转)
查看>>
nginx负载均衡的5种策略
查看>>
nginx负载均衡的5种策略(转载)
查看>>
nginx负载均衡的五种算法
查看>>
Nginx负载均衡详解
查看>>
Nginx负载均衡(upstream)
查看>>
Vue中删除el-table当前行的方法
查看>>
nginx转发端口时与导致websocket不生效
查看>>
Nginx运维与实战(一)-Nginx不同场景使用方法
查看>>
Nginx运维与实战(二)-Https配置
查看>>
Nginx部署_mysql代理_redis代理_phoenix代理_xxljob代理_websocket代理_Nacos代理_内网穿透代理_多系统转发---记录021_大数据工作笔记0181
查看>>
nginx部署本地项目如何让异地公网访问?服务器端口映射配置!
查看>>
Nginx配置HTTPS服务
查看>>
Nginx配置https的一个误区(导致404错误)
查看>>
Nginx配置Https证书
查看>>
Nginx配置http跳转https
查看>>
Nginx配置ssl实现https
查看>>