USACO 2.1 Healthy Holsteins
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的更多相关文章
- USACO Section2.1 Healthy Holsteins 解题报告 【icedream61】
holstein解题报告 --------------------------------------------------------------------------------------- ...
- USACO Healthy Holsteins
首先看题目: Healthy HolsteinsBurch & Kolstad Farmer John prides himself on having the healthiest dair ...
- 洛谷 P1460 健康的荷斯坦奶牛 Healthy Holsteins
P1460 健康的荷斯坦奶牛 Healthy Holsteins 题目描述 农民JOHN以拥有世界上最健康的奶牛为傲.他知道每种饲料中所包含的牛所需的最低的维他命量是多少.请你帮助农夫喂养他的牛,以保 ...
- P1460 健康的荷斯坦奶牛 Healthy Holsteins
P1460 健康的荷斯坦奶牛 Healthy Holsteins 题目描述 农民JOHN以拥有世界上最健康的奶牛为傲.他知道每种饲料中所包含的牛所需的最低的维他命量是多少.请你帮助农夫喂养他的牛,以保 ...
- 【USACO 2.1】Healthy Holsteins
/* TASK: holstein LANG: C++ URL: http://train.usaco.org/usacoprob2?a=SgkbOSkonr2&S=holstein SOLV ...
- USACO Healthy Holsteins DFS
使用排列组合,遍历所有可能的情况C(1)+C(2)+C(3)……C(n)= 2^G种组合 数据规模不大,暴力过去最多也就是2^15 = 23768种情况 所以就暴力咯,不过还是Debug了一会 Sou ...
- USACO Section 2.1 Healthy Holsteins
/* ID: lucien23 PROG: holstein LANG: C++ */ #include <iostream> #include <fstream> #incl ...
- 洛谷P1460 健康的荷斯坦奶牛 Healthy Holsteins
题目描述 农民JOHN以拥有世界上最健康的奶牛为傲.他知道每种饲料中所包含的牛所需的最低的维他命量是多少.请你帮助农夫喂养他的牛,以保持它们的健康,使喂给牛的饲料的种数最少. 给出牛所需的最低的维他命 ...
- P1460 健康的荷斯坦奶牛 Healthy Holsteins (简单的dfs)
题目描述 农民JOHN以拥有世界上最健康的奶牛为傲.他知道每种饲料中所包含的牛所需的最低的维他命量是多少.请你帮助农夫喂养他的牛,以保持它们的健康,使喂给牛的饲料的种数最少. 给出牛所需的最低的维他命 ...
随机推荐
- C - Valera and Fruits
Problem description Valera loves his garden, where n fruit trees grow. This year he will enjoy a gre ...
- axis2服务器搭建
一. axis2服务器搭建 简单起见, axis2r搭建采用较为简单的一种方式, 即将服务类和services.xml打成.aar包发布. 1. 下载部署axis2 http://axis.apach ...
- outlook 2010 搜索不到邮件
打开outlook 2010 文件, 选项, 加载项, 转到 windows search eamil indexer(打勾) 关闭outlook 控制面板, 索引选项, 高级, 重建索引
- 杭电 1021 Fibonacci Again
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1021 解题思路:根据之前发现斐波那契数列的规律,即为f(n)能被3整除当且仅当n能被4整除. 于是联想 ...
- What's Dead & Exploded in Swift's exception stack?
The Swift compiler marks function arguments for a number of reasons, mostly related to internal opti ...
- CentOS7 使用 firewalld 打开关闭 防火墙 与 端口!!
1.firewalld的基本使用 启动: systemctl start firewalld 关闭: systemctl stop firewalld 查看状态: systemctl status f ...
- 转载:html特殊字符 编码css3 content:&quot;我是特殊符号&quot;
项目中用到的一些特殊字符和图标 html代码 <div class="cross"></div> css代码 .cross{ width: 20px; he ...
- JS 封装一个对数组去重的函数
var zz = [1,2,3,5,5,7,8,6,6]; var se = []; d(se); function d(attr){ for(var i = 1;i<zz.length;i++ ...
- PHP迭代器的内部执行过程
下面我们来了解如何实现一个自定义的迭代器,然后再开始慢慢理解迭代器的内部工作原理.先来看一个官方的例子: class myIterator implements Iterator { private ...
- IDEA使用GsonFormat完成JSON和JavaBean之间的转换
原文地址:https://www.leshalv.net/posts/12625/ 前言: 之前处理接口传来的一堆数据,用jsonObject很难受,后面就用gosn来弄,配合这个工具体验很好. 转: ...