Algorithm, Data structure/Solved Algorithmic Problem

USACO 2.1 - Healthy Holsteins

JaykayChoi 2016. 7. 6. 07:55
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



출처: http://train.usaco.org/



Farmer John 은 소를 건강하게 키우기 위해 하루 비타민 권장량을 소에게 주고 싶어한다. Farmer John 은 소의 하루 비타민 권장량을 알고 있고, 각 사료에 들어있는 각 비타민의 함량을 알고 있다. 비타민은 정수 단위이고, 소는 각 사료 종류 중 한 가지씩만 먹는다고 할 때, 소에게 주어야 될 최소 사료 종류의 개수는 몇 개인지 구하시오.

input

1: 비타민 종류

2: 각 비타민의 하루 권장량

3: 사료 종류

4~ : 각 사료에 들어있는 비타민 함량

output

사료 종류 개수, 각 사료를 오름차순으로 출력.



재귀함수를 이용한 깊이 우선 탐색과, 시간 복잡도를 줄이기위해 답이 될 수 없는 현재 구한 최소 답보다 큰 사료 개수와 이미 소에게 준 사료의 번호보다 더 낮은 번호는 건너뛰는 가지치기 방법을 사용했습니다.




my solving

c++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <fstream>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
 
int numV, numG;
vector<int> vitaminRequirements;
vector<vector<int>> feeds;
set<int> ret;
 
bool whetherMeetVitaminRequirements(const vector<int>& fedVitamins)
{
    for (int i = 0; i < numV; i++)
    {
        if (fedVitamins[i] < vitaminRequirements[i])
            return false;
    }
    return true;
}
 
void feed(vector<int>& fedVitamins, int type, bool isSubtraction)
{
    for (int i = 0; i < numV; i++)
    {
        int vitamins = feeds[type][i];
        if (isSubtraction)
            vitamins *= -1;
        fedVitamins[i] += vitamins;
    }
}
 
set<int> chooseFeedType(vector<int>& fedVitamins, set<int>& feedingOrder)
{
    if (whetherMeetVitaminRequirements(fedVitamins))
        return feedingOrder;
 
    
    for (int i = 0; i < numG; i++)
    {
        set<int>::iterator it = feedingOrder.find(i);
        if (it != feedingOrder.end())
            continue;
        if (ret.empty() == false && feedingOrder.size() > ret.size())
            return ret;
        if (feedingOrder.empty() == false)
        {
            set<int>::iterator it = feedingOrder.end();
            it--;
            if (i < *it)
                continue;
        }
        feedingOrder.insert(i);
        feed(fedVitamins, i, false);
        set<int> candidate = chooseFeedType(fedVitamins, feedingOrder);
        if (ret.empty() || candidate.size() < ret.size())
            ret = candidate;
        feed(fedVitamins, i, true);
        feedingOrder.erase(i);
    }
    return ret;
}
 
int main()
{
    ifstream fin("holstein.in");
    ofstream fout("holstein.out");
 
    
    fin >> numV;
    
    for (int i = 0; i < numV; i++)
    {
        int num; 
        fin >> num;
        vitaminRequirements.push_back(num);
    }
 
    fin >> numG;
 
    for (int i = 0; i < numG; i++)
    {
        vector<int> temp;
        for (int i = 0; i < numV; i++)
        {
            int num;
            fin >> num;
            temp.push_back(num);
        }
        feeds.push_back(temp);
    }
 
    vector<int> temp1(numV, 0);
    set<int> temp2;
    set<int> ret = chooseFeedType(temp1, temp2);
 
    fout << ret.size();
    for (set<int>::iterator it = ret.begin(); it != ret.end(); it++)
        fout << " " << *it + 1;
    fout << endl;
 
 
    fin.close();
    fout.close();
    return 0;
}
cs