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)

  1. 4
  2. 100 200 300 400
  3. 3
  4. 50 50 50 50
  5. 200 300 200 300
  6. 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)

  1. 2 1 3
  2.  
  3. 题目大意:就是说给奶牛喂食物,奶牛需要V种维生素每天,给出你每种维生素需要的量,有G种食物,给出你每种食物一勺含有每种维生素的量,同种食物每天最多喂一勺,问你想满足奶牛营养需求每天最少喂多少勺食物。输出勺数和哪几种食物(多组答案的时候输出字典序最小的)。
    思路:明显的搜索呀,枚举没种食物喂或者不喂,我想如果找到答案后面的搜索就可以省略,于是用的迭代加深搜索(就是枚举勺数进行搜索)。第一次没加可行性剪纸只过了七组,第二次加上只过了九组还是T了(第八组耗时0.55sec),代码如下
  1. /*
  2. ID:fffgrdc1
  3. PROB:holstein
  4. LANG:C++
  5. */
  6. #include<cstdio>
  7. #include<iostream>
  8. #include<cstring>
  9. #include<cstdlib>
  10. using namespace std;
  11. int n,m;
  12. int vis[]={};
  13. int temp[];
  14. int aim[];
  15. int a[][];
  16. void check(int ans)
  17. {
  18. memset(temp,,sizeof(temp));
  19. for(int i=;i<=n;i++)
  20. {
  21. if(vis[i])
  22. {
  23. for(int j=;j<=m;j++)
  24. {
  25. temp[j]+=a[i][j];
  26. }
  27. }
  28. }
  29. /*
  30. for(int i=1;i<=m;i++)
  31. {
  32. printf("%d ",temp[i]);
  33. }printf("\n");
  34. */
  35. for(int i=;i<=m;i++)
  36. {
  37. if(temp[i]<aim[i])return ;
  38. }
  39. printf("%d",ans);
  40. for(int i=;i<=n;i++)
  41. if(vis[i])printf(" %d",i);printf("\n");
  42. exit();
  43. }
  44. void dfs(int x,int maxn)
  45. {
  46. if(x==maxn)
  47. {
  48. check(maxn);
  49. return ;
  50. }
  51. for(int i=;i<=n;i++)
  52. {
  53. if(n-i+<maxn-x)return;
  54. if(!vis[i])vis[i]=,dfs(x+,maxn),vis[i]=;
  55. }
  56. }
  57. int main()
  58. {
  59. freopen("holstein.in","r",stdin);
  60. freopen("holstein.out","w",stdout);
  61. scanf("%d",&m);
  62. for(int i=;i<=m;i++)
  63. {
  64. scanf("%d",&aim[i]);
  65. }
  66. scanf("%d",&n);
  67. for(int i=;i<=n;i++)
  68. {
  69. for(int j=;j<=m;j++)
  70. {
  71. scanf("%d",&a[i][j]);
  72. }
  73. }
  74. for(int i=;i<=n;i++)
  75. {
  76. dfs(,i);
  77. }
  78. return ;
  79. }

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

  1.  
  1. /*
  2. ID:fffgrdc1
  3. PROB:holstein
  4. LANG:C++
  5. */
  6. #include<cstdio>
  7. #include<iostream>
  8. #include<cstring>
  9. #include<cstdlib>
  10. using namespace std;
  11. int n,m;
  12. int vis[]={};
  13. int temp[];
  14. int aim[];
  15. int a[][];
  16. int ansn=,nown=,ans[];
  17. void check()
  18. {
  19. memset(temp,,sizeof(temp));
  20. for(int i=;i<=n;i++)
  21. {
  22. if(vis[i])
  23. {
  24. for(int j=;j<=m;j++)
  25. {
  26. temp[j]+=a[i][j];
  27. }
  28. }
  29. }
  30. for(int i=;i<=m;i++)
  31. {
  32. if(temp[i]<aim[i])return ;
  33. }
  34. ansn=nown;
  35. int tempcnt=;
  36. for(int i=;i<=n;i++)
  37. if(vis[i])
  38. ans[++tempcnt]=i;
  39. return ;
  40. }
  41. void dfs(int x)
  42. {
  43. //printf("%d\n",x);
  44. if(x==n)
  45. {
  46. check();
  47. return;
  48. }
  49. vis[x+]=;nown++;
  50. if(nown<ansn)
  51. dfs(x+);
  52. vis[x+]=;nown--;
  53. dfs(x+);
  54. }
  55. int main()
  56. {
  57. freopen("holstein.in","r",stdin);
  58. freopen("holstein.out","w",stdout);
  59. scanf("%d",&m);
  60. for(int i=;i<=m;i++)
  61. {
  62. scanf("%d",&aim[i]);
  63. }
  64. scanf("%d",&n);
  65. for(int i=;i<=n;i++)
  66. {
  67. for(int j=;j<=m;j++)
  68. {
  69. scanf("%d",&a[i][j]);
  70. }
  71. }
  72. dfs();
  73. printf("%d",ansn);
  74. for(int i=;i<=ansn;i++)
  75. printf(" %d",ans[i]);
  76. printf("\n");
  77. return ;
  78. }
  1.  

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

  1.  

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. RAP、Mock.js、Vue.js、Webpack

    最近做项目使用的是RAP1的接口,但是昨天开始,RAP1 出现了问题,接口都不能用了. 所以补充一下Mock.js的用法,以便在这种突发的情况时候时自己通过Mock的方式来处理接口. npm init ...

  2. P1284 三角形牧场

    题目描述 和所有人一样,奶牛喜欢变化.它们正在设想新造型的牧场.奶牛建筑师Hei想建造围有漂亮白色栅栏的三角形牧场.她拥有N(3≤N≤40)块木板,每块的长度Li(1≤Li≤40)都是整数,她想用所有 ...

  3. RAP开发入门-运行过程简析(三)

    今天通过标准的RAP程序来简单分析下RAP的启动过程 1.新建一个标准的rap plugin-in 项目: 得到的项目结构大概如下: run confi..->..add bundle(配置好b ...

  4. LoadRunner时间戳函数web_save_timestamp_param

    举例:1520822348346(13位,毫秒级)   做时间戳的目的是为了JS缓存和防止CSRF,在LR中可以简单的使用下面这个函数 web_save_timestamp_param 来生成时间戳 ...

  5. javaScript 计算两个日期的天数相差

    一:计算两个日期相差的天数 1 <html> <head> <meta http-equiv="Content-Type" content=" ...

  6. trigger事件就是继承某一个类的事件.

    <html><head><script type="text/javascript" src="/jquery/jquery.js" ...

  7. chrome中自动回填表单解决

    input添加属性autocomplete="new-password"

  8. 算法46----移除K位数字

    一.题目:移除K位数字 给定一个以字符串表示的非负整数 num,移除这个数中的 k 位数字,使得剩下的数字最小. 注意: num 的长度小于 10002 且 ≥ k. num 不会包含任何前导零. 示 ...

  9. 【JavaScript框架封装】实现一个类似于JQuery的动画框架的封装

    // 动画框架 (function (xframe) { // 需要参与链式访问的(必须使用prototype的方式来给对象扩充方法) xframe.extend({}); // 不需要参与链式访问的 ...

  10. nyoj158-省赛来了

    省赛来了 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 一年一度的河南省程序设计大赛又要来了. 竞赛是要组队的,组队形式:三人为一队,设队长一名,队员两名. 现在问题就 ...