USACO SECTION 2.1 Healthy Holsteins

本文探讨如何通过算法帮助农民约翰最小化喂养奶牛的饲料数量,同时确保奶牛每天获得足够的维生素。通过输入奶牛每日所需维生素量、可用饲料类型及其维生素含量,输出最优化的饲料组合及所需饲料种类数量。
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: conicoc1
LANG: C
TASK: holstein
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

int V,G;
int Vitamin[25],Proposal[15][25];
int NowChoose[15];
int Queue[70000];
int rear=0,front=0;
int count=0;
int flag[70000];
int temp;

void Invert(int sum)
{
	int i=0;
	while(i<G)
	{
		NowChoose[i++]=sum%2;
		sum/=2;
	}
}

int change()
{
	int i,k=1;
	int sum=0;
	for(i=0;i<G;i++)
	{
		sum+=NowChoose[i]*k;
		k*=2;
	}
	return sum;
}

void Enqueue(int sum)
{
	Queue[rear++]=sum;
}

int Dequeue()
{
	return Queue[front++];
}

int judge()
{
	int sum=0;
	int i,j;
	for(i=0;i<V;i++)
	{
		for(j=0;j<G;j++)
		{
			if(NowChoose[j]==1)
				sum+=Proposal[j][i];
		}
		printf("%d\n",sum);
		if(sum<Vitamin[i])
			return 0;
		sum=0;
	}
		getchar();
	return 1;
}

int main()
{
	FILE *fin,*fout;
	fin=fopen("holstein.in","r");
	fout=fopen("holstein.out","w");
	
	
	memset(NowChoose,0,sizeof(NowChoose));
	memset(Queue,0,sizeof(Queue));
	memset(flag,0,sizeof(flag));
	
	int i,j;
	
	fscanf(fin,"%d",&V);
	for(i=0;i<V;i++)
	{
		fscanf(fin,"%d",&Vitamin[i]);
	}
	fscanf(fin,"%d",&G);
	for(i=0;i<G;i++)
	{
		for(j=0;j<V;j++)
		{
			fscanf(fin,"%d",&Proposal[i][j]);
		}
	}
	//数据读入完毕

	rear++;
	while(front!=rear)
	{
		temp=Dequeue();
		printf("%d:\n",temp);
		Invert(temp);
	
		if(judge())
			break;
		for(i=0;i<G;i++)
		{
			if(NowChoose[i]!=1)
			{
				NowChoose[i]++;
				temp=change();
				if(flag[temp]!=1)
				{
					Enqueue(temp);
					flag[temp]=1;
				}
				NowChoose[i]--;
			}
		}
	}
	
 	for(i=0;i<G;i++)
 	{
		if(NowChoose[i]==1)
			count++;
 	}
 	
 	fprintf(fout,"%d",count);
 	
 	for(i=0;i<G;i++)
 	{
		if(NowChoose[i]!=0)
			fprintf(fout," %d",i+1);
 	}
 	fprintf(fout,"\n");
 	
 	
 	return 0;
	
	
}


一开始没有看到每个饲料只能用一次。。用广搜的时候各种空间不够或者超时

注意数组和数字二进制转换的时候 while(i<G) 而不是至 while(sum!=0)

 

提一下分析里面 ,DFS的剪枝

 

设可以选择的最多饲料数为MaxSeeds (<=15)

     当前选择的饲料为CurSeed

最优解的位数:best

当前的位数:fcnt

while(CurSeed<=MaxSeeds&& fcnt+1<best)时才进行下一层的搜索

注意

fcnt+1<best

如果不满足该条件就不能满足答案最优(即字典序最小)

相当于当你找到一个最优解时

你把和这个最优解在同一层或下层的所有可能情况都排除了

 

其实用BFS层序遍历的话就不需要这样剪枝了啊= =

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值