Healthy Holsteins
Burch & Kolstad

Farmer John prides himself on having the healthiest dairy cows in the world. He knows the vitamin content for one scoop of each feed type and the minimum daily vitamin requirement for the cows. Help Farmer John feed his cows so they stay healthy while minimizing the number of scoops that a cow is fed.

Given the daily requirements of each kind of vitamin that a cow needs, identify the smallest combination of scoops of feed a cow can be fed in order to meet at least the minimum vitamin requirements.

Vitamins are measured in integer units. Cows can be fed at most one scoop of any feed type. It is guaranteed that a solution exists for all contest input data.

PROGRAM NAME: holstein

INPUT FORMAT

Line 1: integer V (1 <= V <= 25), the number of types of vitamins
Line 2: V integers (1 <= each one <= 1000), the minimum requirement for each of the V vitamins that a cow requires each day
Line 3: integer G (1 <= G <= 15), the number of types of feeds available
Lines 4..G+3: V integers (0 <= each one <= 1000), the amount of each vitamin that one scoop of this feed contains. The first line of these G lines describes feed #1; the second line describes feed #2; and so on.

SAMPLE INPUT (file holstein.in)

4
100 200 300 400
3
50 50 50 50
200 300 200 300
900 150 389 399

OUTPUT FORMAT

The output is a single line of output that contains:

  • the minimum number of scoops a cow must eat, followed by:
  • a SORTED list (from smallest to largest) of the feed types the cow is given

If more than one set of feedtypes yield a minimum of scoops, choose the set with the smallest feedtype numbers.

SAMPLE OUTPUT (file holstein.out)

2 1 3

题目大意:就是说给奶牛喂食物,奶牛需要V种维生素每天,给出你每种维生素需要的量,有G种食物,给出你每种食物一勺含有每种维生素的量,同种食物每天最多喂一勺,问你想满足奶牛营养需求每天最少喂多少勺食物。输出勺数和哪几种食物(多组答案的时候输出字典序最小的)。
思路:明显的搜索呀,枚举没种食物喂或者不喂,我想如果找到答案后面的搜索就可以省略,于是用的迭代加深搜索(就是枚举勺数进行搜索)。第一次没加可行性剪纸只过了七组,第二次加上只过了九组还是T了(第八组耗时0.55sec),代码如下
 /*
ID:fffgrdc1
PROB:holstein
LANG:C++
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
int n,m;
int vis[]={};
int temp[];
int aim[];
int a[][];
void check(int ans)
{
memset(temp,,sizeof(temp));
for(int i=;i<=n;i++)
{
if(vis[i])
{
for(int j=;j<=m;j++)
{
temp[j]+=a[i][j];
}
}
}
/*
for(int i=1;i<=m;i++)
{
printf("%d ",temp[i]);
}printf("\n");
*/
for(int i=;i<=m;i++)
{
if(temp[i]<aim[i])return ;
}
printf("%d",ans);
for(int i=;i<=n;i++)
if(vis[i])printf(" %d",i);printf("\n");
exit();
}
void dfs(int x,int maxn)
{
if(x==maxn)
{
check(maxn);
return ;
}
for(int i=;i<=n;i++)
{
if(n-i+<maxn-x)return;
if(!vis[i])vis[i]=,dfs(x+,maxn),vis[i]=;
}
}
int main()
{
freopen("holstein.in","r",stdin);
freopen("holstein.out","w",stdout);
scanf("%d",&m);
for(int i=;i<=m;i++)
{
scanf("%d",&aim[i]);
}
scanf("%d",&n);
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
scanf("%d",&a[i][j]);
}
}
for(int i=;i<=n;i++)
{
dfs(,i);
}
return ;
}

想了想,作为普适算法,每个点都得过,总会经历最坏情况的,于是打算直接写最裸最暴力的枚举,也就是枚举每种食物的状态,喂或者不喂。再加上最优化剪枝,代码如下

 
 /*
ID:fffgrdc1
PROB:holstein
LANG:C++
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
int n,m;
int vis[]={};
int temp[];
int aim[];
int a[][];
int ansn=,nown=,ans[];
void check()
{
memset(temp,,sizeof(temp));
for(int i=;i<=n;i++)
{
if(vis[i])
{
for(int j=;j<=m;j++)
{
temp[j]+=a[i][j];
}
}
}
for(int i=;i<=m;i++)
{
if(temp[i]<aim[i])return ;
}
ansn=nown;
int tempcnt=;
for(int i=;i<=n;i++)
if(vis[i])
ans[++tempcnt]=i;
return ;
}
void dfs(int x)
{
//printf("%d\n",x);
if(x==n)
{
check();
return;
}
vis[x+]=;nown++;
if(nown<ansn)
dfs(x+);
vis[x+]=;nown--;
dfs(x+);
}
int main()
{
freopen("holstein.in","r",stdin);
freopen("holstein.out","w",stdout);
scanf("%d",&m);
for(int i=;i<=m;i++)
{
scanf("%d",&aim[i]);
}
scanf("%d",&n);
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
scanf("%d",&a[i][j]);
}
}
dfs();
printf("%d",ansn);
for(int i=;i<=ansn;i++)
printf(" %d",ans[i]);
printf("\n");
return ;
}

果然秒过(极限数据0.011sec,第八组0.00sec)。。。。可能是我迭代加深写丑了。。。。再改改

												

USACO 2.1 Healthy Holsteins的更多相关文章

  1. USACO Section2.1 Healthy Holsteins 解题报告 【icedream61】

    holstein解题报告 --------------------------------------------------------------------------------------- ...

  2. USACO Healthy Holsteins

    首先看题目: Healthy HolsteinsBurch & Kolstad Farmer John prides himself on having the healthiest dair ...

  3. 洛谷 P1460 健康的荷斯坦奶牛 Healthy Holsteins

    P1460 健康的荷斯坦奶牛 Healthy Holsteins 题目描述 农民JOHN以拥有世界上最健康的奶牛为傲.他知道每种饲料中所包含的牛所需的最低的维他命量是多少.请你帮助农夫喂养他的牛,以保 ...

  4. P1460 健康的荷斯坦奶牛 Healthy Holsteins

    P1460 健康的荷斯坦奶牛 Healthy Holsteins 题目描述 农民JOHN以拥有世界上最健康的奶牛为傲.他知道每种饲料中所包含的牛所需的最低的维他命量是多少.请你帮助农夫喂养他的牛,以保 ...

  5. 【USACO 2.1】Healthy Holsteins

    /* TASK: holstein LANG: C++ URL: http://train.usaco.org/usacoprob2?a=SgkbOSkonr2&S=holstein SOLV ...

  6. USACO Healthy Holsteins DFS

    使用排列组合,遍历所有可能的情况C(1)+C(2)+C(3)……C(n)= 2^G种组合 数据规模不大,暴力过去最多也就是2^15 = 23768种情况 所以就暴力咯,不过还是Debug了一会 Sou ...

  7. USACO Section 2.1 Healthy Holsteins

    /* ID: lucien23 PROG: holstein LANG: C++ */ #include <iostream> #include <fstream> #incl ...

  8. 洛谷P1460 健康的荷斯坦奶牛 Healthy Holsteins

    题目描述 农民JOHN以拥有世界上最健康的奶牛为傲.他知道每种饲料中所包含的牛所需的最低的维他命量是多少.请你帮助农夫喂养他的牛,以保持它们的健康,使喂给牛的饲料的种数最少. 给出牛所需的最低的维他命 ...

  9. P1460 健康的荷斯坦奶牛 Healthy Holsteins (简单的dfs)

    题目描述 农民JOHN以拥有世界上最健康的奶牛为傲.他知道每种饲料中所包含的牛所需的最低的维他命量是多少.请你帮助农夫喂养他的牛,以保持它们的健康,使喂给牛的饲料的种数最少. 给出牛所需的最低的维他命 ...

随机推荐

  1. MySQL 5.6 Reference Manual-14.2 InnoDB Concepts and Architecture

    14.2 InnoDB Concepts and Architecture 14.2.1 MySQL and the ACID Model 14.2.2 InnoDB Multi-Versioning ...

  2. [翻译]内存一致性模型 --- memory consistency model

    I will just give the analogy with which I understand memory consistency models (or memory models, fo ...

  3. Mac使用ssh登录远程linux系统查看jetty日志及同时使用github工具

    转载请注明出处:http://www.houxiurong.com/?post=27 Mac默认是安装了ssh工具软件的. 先用mac的 终端工具生成 id_rsa 和id_rsa.pub 秘钥,生成 ...

  4. 避免关注底层硬件,Nvidia将机器学习与GPU绑定

    Nvidia释放的一组cuDNN的库,有效的实现了其与多种深度学习框架的整合.基于cuDNN,加速了代码的运行,同时让研究员避免去关心底层硬件性能. 关键字: 编程语言语音识别Nvidia 原文链接: ...

  5. YoC云上芯片家族迎来新成员

    Espressif 乐鑫信息科技(以下简称乐鑫科技)近日在上海召开发布会,发布其旗下最新的旗舰同时也是第二代Yun on Chip(简称YoC)云上芯片ESP32.YoC云上芯片是由YunOS牵头,联 ...

  6. js 目录树

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  7. mvvm模式和mvc模式 概述总结对比

    1.mvc模式简介: MVC的全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,是一种软件设计典范.例如: angular ...

  8. python tips:生成器的小问题

    在Python中,生成器和函数很像,都是在运行的过程中才会去确定各种变量的值,所以在很多情况下,会导致各种各样的问题. def generator_test1(): # 0...9 generator ...

  9. 不能使用一般 Request 集合

    request.querystring("id"),不能request("id")

  10. 在windows环境中关于 pycharm配置 anaconda 虚拟环境

    因为要在windows系统系统中练习tensorflow,所以需要配置一下环境(来回的开关机切换环境太麻烦了......) 首先安装anaconda3,我选择的版本是Anaconda3 5.1.0,对 ...