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
SAMPLE OUTPUT (file holstein.out)
2 1 3思路:由于测试数据小,可以直接枚举各种可能,用二进制表示各种可能,各个位数上1代表喂养,0代表不喂养即可
/*
ID: youqihe1
PROG: holstein
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
#include<algorithm>
using namespace std;
struct food
{
int vit[30];
int choose;
}A[20];
void binary(int n,char C[])
{
int i=0;
char B[30];
memset(B,'\0',sizeof(B));
while(n)
{
int k=n%2;
B[i++]=k+'0';
n/=2;
}
int t=0,j;
for(j=i-1;j>=0;j--,t++)
C[j]=B[t];
}
struct CHO
{
int sum;
int val;
}choose[65536];
int S=0;
int pow(int a,int n)
{
if(n==0)
return 1;
int b=a;
while(--n)
a*=b;
return a;
}
bool cmp(CHO a,CHO b)
{
if(a.sum==b.sum)
return a.val<b.val;
else
return a.sum<b.sum;
}
int main() {
FILE *fin = fopen ("holstein.in", "r");
FILE *fout = fopen ("holstein.out", "w");
food need;
int V,G,i,j,k;
fscanf(fin,"%d",&V);
for(i=0;i<V;i++)
fscanf(fin,"%d",&need.vit[i]);
fscanf(fin,"%d",&G);
for(i=0;i<G;i++)
{
for(j=0;j<V;j++)
fscanf(fin,"%d",&A[i].vit[j]);
}
for(i=1;i<pow(2,G);i++)
{
char C[20];
memset(C,'\0',sizeof(C));
binary(i,C);
int len=strlen(C);
food N;
for(j=0;j<V;j++)
N.vit[j]=0;
for(j=0;j<len;j++)
{
if(C[len-j-1]=='1')
{
for(k=0;k<V;k++)
N.vit[k]+=A[j].vit[k];
}
}
int flag=1;
for(k=0;k<V;k++)
if(N.vit[k]<need.vit[k])
{
flag=0;
break;
}
if(flag)
{
int hehe=0;
for(k=0;k<len;k++)
if(C[k]=='1')
hehe++;
choose[S++].sum=hehe;
choose[S-1].val=i;
}
}
sort(choose,choose+S,cmp);
int CC[20];
int SS=0;
char C[20];
memset(C,'\0',sizeof(C));
binary(choose[0].val,C);
int len=strlen(C);
for(i=len-1;i>=0;i--)
{
if(C[i]=='1')
CC[SS++]=len-i;
}
//printf("%d",SS);
fprintf(fout,"%d",SS);
for(i=0;i<SS;i++)
//printf(" %d",CC[i]);
fprintf(fout," %d",CC[i]);
fprintf(fout,"\n");
return 0;
}