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以拥有世界上最健康的奶牛为傲.他知道每种饲料中所包含的牛所需的最低的维他命量是多少.请你帮助农夫喂养他的牛,以保持它们的健康,使喂给牛的饲料的种数最少. 给出牛所需的最低的维他命 ...
随机推荐
- Hadoop MapReduce编程 API入门系列之wordcount版本5(九)
这篇博客,给大家,体会不一样的版本编程. 代码 package zhouls.bigdata.myMapReduce.wordcount1; import java.io.IOException; i ...
- Npgsql使用入门(三)【批量导入数据】
Program.cs代码: class Program { static void Main(string[] args) { var test = new PgBulkCopyHelper<S ...
- SqlServer备份恢复
备份 (Transact SQL) | Microsoft Docs https://docs.microsoft.com/zh-cn/sql/t-sql/statements/backup- ...
- 【JavaScript从入门到放弃】JS基础-01
作为一个前端开发人员,JS是我们行走江湖吃饭的家伙.基本上一个前端人员能值多少大洋,就看JS了.虽然各种框架层出不穷,但是归根结底学好原生JS才是硬道理. 学习任何新东西其实都遵循 10000 小时成 ...
- 第一天:java与mysql的连接工具类
第一天:java与mysql的连接工具类 java最新版马上就要收费,这无疑是这门语言的衰败起始,毕竟在中国收费便难发展,例如c#,但是毕业设计已经选好用java来写一个动态网站, 这已经是一个事实, ...
- 2-SAT 小结
PS:今天(2014.10.27)准备PPT,明天在组合数学课上与大家一起分享一下2-SAT.我以为是一件简单的事情.但是,当我看了自己这篇博客以后,发现居然还是不懂.很多资料不全,也没仔细讲.整理了 ...
- CSS字体代码
宋体 SimSun 黑体 SimHei 微软雅黑 Microsoft YaHei 微软正黑体 Microsoft JhengHei 新宋体 NSimSun 新细明体 PMingLiU 细明体 Ming ...
- python 跑服务器,访问自己制作的简单页面
1 python 跑服务器,访问自己制作的简单页面 2 # win+b出现一个网址http:/0.0.1:5000/复制到浏览器查看# http://127.0.0.1:5000/home 做这个首 ...
- Centos 7 安装图形化界面
Centos 7 默认是没有图形化界面的,但我们很多人在习惯了 Windows 的图形化界面之后,总是希望有一个图形化界面从而方便我们使用,这里介绍一下 CentOS7安装图形化桌面系统的方法. 一. ...
- anaconda下jieba和wordcloud安装
1.在anaconda交互环境下安装jieba,输入命令: pip install jieba 2.在https://pypi.python.org/pypi/wordcloud下载wordclou ...