USACO2.1.4--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
题解:把N种饲料当成是一个集合,那么题目要求就是求这个集合的子集,满足总数最少且字典序最小,并且能够达到牛所需的最低的维他命量。这样这个题目就可以用位运算来解决。每种饲料要么选取,要么不选取。即1和0表示。集合的非空子集总数为2^N-1个。1到2^N-1每个数代表一种方案,每个数用二进制表示,如果位置M等于1,那么说明次方案需要第M种饲料。位运算实在太神奇了,此题用位运算既简单有清晰。
View Code
/*
ID:spcjv51
PROG:holstein
LANG:C
*/
#include <stdio.h>
int v[30],ans[30];
int a[20][30];
int n,m,len;
int OK()
{
    int i;
    for(i=1; i<=n; i++)
        if(ans[i]<v[i]) return 0;
    return 1;
}
int main(void)
{
    freopen("holstein.in","r",stdin);
    freopen("holstein.out","w",stdout);
    int i,j,k,t,h,minlen,ll;
    scanf("%d",&n);
    for(i=1; i<=n; i++)
        scanf("%d",&v[i]);
    scanf("%d",&m);
    for(i=1; i<=m; i++)
        for(j=1; j<=n; j++)
            scanf("%d",&a[i][j]);
    t=1<<m;
    minlen=100000;
    for(i=1;i<t;i++)
    {
        j=i;
        k = 0;
        ll=0;
        memset(ans,0,sizeof(ans));
        while(j)
        {
            k++;
            if (j&1)
            {
                for(len=1; len<=n; len++)
                    ans[len]+=a[k][len];
                ll++;

            }
            j >>= 1;
        }
        if(ll<minlen&&OK())
        {
              h=i;
              minlen=ll;
        }
    }
    printf("%d",minlen);
    k=0;
    while(h)
    {
        k++;
        if(h&1)
            printf(" %d",k);
        h>>=1;
    }
    printf("\n");
    return 0;
}

 

posted on 2013-02-03 19:42  仗剑奔走天涯  阅读(201)  评论(0编辑  收藏  举报

导航