usaco2.13Healthy Holsteins(dfs枚舉)

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
/*
    ID: your_id_here
    PROG: holstein
    LANG: C++
*/
#include <iostream>
#include<cstdio>
#include<stdlib.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int fe[20][30],vi[30],v,g,vv[30],f[30],mi,oo[30],o[30];
void dfs(int x,int de,int *w)
{
    int i,j;
    if(de>mi)
        return ;
    oo[de] = x;
    for(i = 1 ; i <= v ; i++)
        if(w[i]<vv[i])
        break;
    if(i==v+1)
    {
        if(de<mi)
        {
            mi = de;
            for(i = 1; i <= de ; i++)
                o[i] = oo[i];
        }
        return ;
    }
    for(i = x+1 ; i <= g ; i++)
    {
        for(j = 1; j <= v ; j++)
        w[j]+=fe[i][j];
        dfs(i,de+1,w);
        for(j = 1; j <= v ; j++)
        w[j]-=fe[i][j];
    }
}
int main()
{
    freopen("holstein.in","r",stdin);
    freopen("holstein.out","w",stdout);
    int i,j,k,kk[30];
    mi = 29;
    cin>>v;
    for(i = 1; i <= v ; i++)
        cin>>vv[i];
    cin>>g;
    for(i = 1; i <= g ; i++)
        for(j = 1; j <= v ; j++)
            cin>>fe[i][j];
    for(i = 1; i <= g ; i++)
    {
        for(j = 1 ; j <= v ; j++)
        kk[j] = fe[i][j];
        dfs(i,1,kk);
    }
    cout<<mi<<" ";
    for(i = 1; i < mi ; i++)
        cout<<o[i]<<" ";
    cout<<o[mi]<<endl;
    fclose(stdin);
    fclose(stdout);
    return 0;
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章