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 (1 ~ 25)说明有 V 种维他命,后给出 V (1 ~ 1000)个数,代表每一种维他命的最低满足量。后给出 G(1 ~ 15) 种食物选择,每种食物都有 V 种维他命的量 Vi (1 ~ 1000),选择最少的食物种树 P,使总和能不少于最低满足量。

 

   思路:

   直接用二进制枚举,0代表没选上,1代表选上。最多也就选择15种,故从从 1 枚举到pow(2,n)判断即可,加上剪枝。DFS会爆栈。

 

   AC:

/*    
TASK:holstein    
LANG:C++    
ID:sum-g1    
*/
#include<stdio.h>
#include<string.h>
#include<math.h>

int tra[30],num[30],kind[20][30],sum[30],fin[30],now[30];
int n,m,k,minnum = 20;

int rel(int *nn,int *ff,int len)   //相同选择时,判断哪个更小
{
    for(int i = 1;i <= len;i++)
    {
        if(nn[i] > ff[i]) return 0;
        if(nn[i] < ff[i]) return 1;
    }
}

int trans(int ans)
{
    int s = 0,k = 0;
    while(ans)
    {
        k++;
        tra[k] = ans % 2;
        ans /= 2;
        if(tra[k])
        {
            s++;
            now[s] = k;
            for(int i = 1;i <= n;i++)
                sum[i] += kind[k][i];
        }
        if(s > minnum) return 0;   
//当出现选择大于当前最小时,直接返回,不必再判断
    }
    for(int i = 1;i <= n;i++)
        if(sum[i] < num[i]) return 0;
//若出现总和小于所需量时,也返回不必再判断
    if(s < minnum)
    {
        minnum = s;
        for(int i = 1;i <= s;i++)
            fin[i] = now[i];
    }
    if(s == minnum)
    {
        if(rel(now,fin,s))
        {
            for(int i = 1;i <= s;i++)
                fin[i] = now[i];
        }
    }
    return 1;
}

int main()
{
    freopen("holstein.in","r",stdin);        
    freopen("holstein.out","w",stdout);    
    scanf("%d",&n);
    for(int i = 1;i <= n;i++)
        scanf("%d",&num[i]);
    scanf("%d",&m);
    for(int i = 1;i <= m;i++)
        for(int j = 1;j <= n;j++)
        scanf("%d",&kind[i][j]);
    for(int i = 1;i <= pow(2,m);i++)
    {
        memset(tra,0,sizeof(tra));
        memset(sum,0,sizeof(sum));
        if(!trans(i)) continue;
    }
    printf("%d",minnum);
    for(int i = 1;i <= minnum;i++)
        printf(" %d",fin[i]);
    printf("\n");
    return 0;
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值