2.1.4---Healthy Holsteins

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

健康的好斯坦奶牛
  
        农民JOHN以拥有世界上最健康的奶牛为骄傲。他知道每种饲料中所包含的的牛所需的最低的维他命量是多少。请你帮助农夫喂养他的牛,以保持他们的健康,使喂给牛的饲料的种数最少。
  给出牛所需的最低的维他命,输出喂给牛需要哪些种类的饲料。

  

PROGRAM NAME: holstein

INPUT FORMAT
第1行:一个整数V(1<=V<=25),表示需要的维他命的种类数。
第2行:V个整数(1<=每个数<=1000),表示牛每天需要的维他命的最小量。
第3行:一个整数G(1<=G<=15),表示可用来喂牛的饲料的数量。下面G行,第i行表示编号为 i饲料包含的各种维他命的量的多少。
  
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
输出文件只有一行,包括:
牛必需的最小的饲料种数P 
后面有P个数,表示所选择的饲料编号(按从小到大排列)。 

 

SAMPLE OUTPUT (file holstein.out)

  2 1 3 

深搜
/*
ID:******
PROG:holstein
LANG:C++
*/

#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

typedef struct{
	int have[26];
}da;//每种饲料对应的营养供给

int symbol[16];//保存中间结果
int yes_no[16];//最终结果
da data[16];//存储所有饲料的营养供给
int num_health;//考虑的营养元素数
int num_type;//饲料种类数
int count_final = 10000;//记录结果中所需饲料数
da count,count_now;//count 为共需要的营养供给,count_now为当前营养供给量

void dfs(int type_th){//type_th标识目前考虑的饲料种类
        //先判断当前营养供给是否满足要求
	int sym = 1;
	for(int i = 0;i < num_health; i ++)
		if(count_now.have[i] < count.have[i]){
			sym = 0;
			break;
		}
		if(sym){//若满足则记录
			int temp = 0;
			for(int i = 0;i < num_type;i ++)
				if(symbol[i])
					temp ++;
			if(temp <= count_final){
				count_final = temp;
				memset(yes_no,0,sizeof(yes_no));
				for(int i = 0;i < num_type; i ++)
					if(symbol[i])
						yes_no[i] = 1;
			}
			return ;
		}
		if(type_th == num_type)//边界判断
			return;
		//考虑不需要的情况 
	symbol[type_th] = 0;
	dfs(type_th + 1);
	//考虑需要的情况
	for(int i = 0;i < num_health; i ++)
		count_now.have[i] += data[type_th].have[i];
	    symbol[type_th] = 1;
		dfs(type_th + 1);
        //复原现场
		symbol[type_th] = 0;
	for(int i = 0;i < num_health; i ++)
		count_now.have[i] -= data[type_th].have[i];
	
}
int main(){
	ifstream fin ("holstein.in");
	ofstream fout ("holstein.out");
	
	//数据输入
	fin >> num_health;//输入需求
	for(int i = 0; i < num_health;i ++)
		fin >> count.have[i];
	fin >> num_type;//输入饲料方案
	for(int i = 0; i < num_type; i ++)
		for(int j = 0;j < num_health;j ++)
			fin >> data[i].have[j];

	//开始搜索
	memset(symbol,0,sizeof(symbol));
	memset(count_now.have,0,sizeof(count_now.have));
	dfs(0);
	
	//输出结果
	cout << count_final;
	for(int i = 0; i < num_type;i ++)
		if(yes_no[i])
			cout <<" " << i+1 ;
	cout <<endl;
	return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值