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
题目大致意思就是 要从g中喂养plan中挑选出最少feedtype数,满足一天的营养需求。因为最多是15个feedtype,所以最多也就2^15种方案。
我一开始想到的是BFS,将当前还需要的营养需求这个向量state[0..v-1]作为状态结点,从小到大选定一种feedtype,计算出喂下这个feedtype后的状态,作为父节点扩展出的子结点加入到队列中。父节点扩展完,队列的头指针++。一直这样搜索下去,到第一个(0,0,0,0,……,0)结点的时候,得到了我们的最优解。但是很可惜,最后一个test没有过,到目前还不知道如何解决。。。
官方标准的做法是用DFS,将g种feedtype作为一个向量[0...g-1],向量中每一项都可以取0(不选这种feedtype)或者取1(选这种feedtype)。只要把所有的0,1取值都枚举一边就可以了。中间要注意剪枝,如果在第k步时得到了当前的一个解但是这个解中feedtype的个数比目前最优解的个数要多,则return。
BFS部分通过的代码:
/*
ID: gjj50201
LANG: C++
TASK: holstein
*/
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <stack>
using namespace std;
struct node
{
int state[25];
int path;
int father;
};
int v,g, a[15][25],cur;
node que[10000];
int head = 0 ,tail = 0;
int book[15] = {0};
int main(){
int flag;
freopen("holstein.in","r",stdin);
freopen("holstein.out","w",stdout);
cin>>v;
for(int i=0;i<v;i++)
cin>>que[tail].state[i];
que[tail].path = 0;
que[tail].father = -1;
tail++;
cin>>g;
for(int i=0;i<g;i++)
for(int j=0;j<v;j++)
cin>>a[i][j];
while(head<tail){
cout<<head<<endl;
for(int i=0;i<g;i++){
if(que[head].path <i+1){
cout<<"i="<<i<<endl;
flag = v;
for(int j=0;j<v;j++){
if(que[head].state[j]>0 && que[head].state[j] - a[i][j] >0)
que[tail].state[j] = que[head].state[j] - a[i][j];
else{
que[tail].state[j] = 0;
flag --;
}
}
cout<<"------"<<endl;
for(int j=0;j<v;j++)
cout<<que[tail].state[j]<<" ";
cout<<endl;
que[tail].path = i+1;
que[tail].father = head;
if(flag == 0) //全为0了
break;
tail ++;
}
if(flag == 0)
break;
}
if(flag == 0)
break;
head++;
}
cur = tail;
int ans = 0;
stack <int> s;
while(que[cur].father != -1){
s.push(que[cur].path);
ans++;
cur = que[cur].father;
}
cout<<ans;
while(!s.empty()){
cout<<" "<<s.top();
s.pop();
}
cout<<endl;
return 0;
}
/*
ID: gjj50201
LANG: C++
TASK: holstein
*/
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <algorithm>
using namespace std;
int v,g, a[15][25];
int need[25];
int cur = 0, minn;
int ans[15],tmp[15];
int ok(){
for(int i=0;i<v;i++)
if(need[i]>0)
return 0;
return 1;
}
void dfs(int k){
if(ok() && cur < minn){
minn = cur;
for(int i=0;i<v;i++)
ans[i] = tmp[i];
}
if(k>=g)
return;
for(int i=0;i<v;i++)
need[i] = need[i] - a[k][i];
tmp[cur] = k;
cur++;
dfs(k+1);
cur--;
for(int i=0;i<v;i++)
need[i] = need[i] + a[k][i];
dfs(k+1);
}
int main(){
freopen("holstein.in","r",stdin);
freopen("holstein.out","w",stdout);
cin>>v;
for(int i=0;i<v;i++)
cin>>need[i];
cin>>g;
minn = 9999999;
for(int i=0;i<g;i++)
for(int j=0;j<v;j++)
cin>>a[i][j];
dfs(0);
cout<<minn;
//cout<<ans[0]<<endl;
for(int i=0;i<minn;i++)
cout<<" "<<ans[i]+1;
cout<<endl;
return 0;
}