Milk Measuring

Milk Measuring
Hal Burch

Farmer John must measure Q (1 <= Q <= 20,000) quarts of his finest milk and deliver it in one big bottle to a customer. He fills that bottle with exactly the number of quarts that the customer orders.

Farmer John has always been frugal. He is at the cow hardware store where he must purchase a set of pails with which to measure out Q quarts of milk from his giant milk tank. Since the pails each cost the same amount, your task is to figure out a minimal set of pails Farmer John can purchase in order to fill a bottle with exactly Q quarts of milk. Additionally, since Farmer John has to carry the pails home, given two minimal sets of pails he should choose the "smaller" one as follows: Sort the sets in ascending order. Compare the first pail in each set and choose the set with the smallest pail. If the first pails match, compare the second pails and choose from among those, else continue until the two sets differ. Thus the set {3, 5, 7, 100} should be chosen over {3, 6, 7, 8}.

To measure out milk, FJ may completely fill a pail from the tank and pour it into the bottle. He can never remove milk from the bottle or pour milk anywhere except into the bottle. With a one-quart pail, FJ would need only one pail to create any number of quarts in a bottle. Other pail combinations are not so convenient.

Determine the optimally small number of pails to purchase, given the guarantee that at least one solution is possible for all contest input data.

PROGRAM NAME: milk4

INPUT FORMAT

Line 1:The single integer Q
Line 2:A single integer P (1 <= P <= 100) which is the number of pails in the store
Lines 3..P+2:Each line contains a single integer pail_value (1 <= pail_value <= 10000), the number of quarts a pail holds

SAMPLE INPUT (file milk4.in)

16
3
3
5
7

OUTPUT FORMAT

The output is a single line of space separated integers that contains:

  • the minimum number of pails required to measure out the desired number of quarts, followed by:
  • a sorted list (from smallest to largest) of the capacity of each of the required pails

SAMPLE OUTPUT (file milk4.out)

2 3 5

 

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define Min(a,b) a<b?a:b
#define Max(a,b) a<b?b:a

int Quarts,PailNum;
int Pail[10000];
int Choose[10000];  //Pail序号 
int DP[20001];

int comp(const void *a,const void *b)
{
	return *(int *)a-*(int *)b;	
}

int DFSID(int Depth,int UsePail,int PrePail)
{
	int i,j,k;
	if(Depth==UsePail){
		memset(DP,0,sizeof(DP));
		DP[0]=1;
		for(i=0;i<UsePail;i++){
			for(j=Choose[i];j<=Quarts;j++)
				if(DP[j-Choose[i]])
					DP[j]=1;
			if(DP[Quarts])
				return 1;
		}
		return 0;
	}
	for(i=PrePail+1;i<PailNum;i++){
			Choose[Depth]=Pail[i];    //选择一种 
			if(DFSID(Depth+1,UsePail,i))
				return 1;
	}
	return 0;
}

int main()
{
	int i,j,k;
	FILE *fin,*fout;
	fin=fopen("milk4.in","r");
	fout=fopen("milk4.out","w");
	fscanf(fin,"%d %d",&Quarts,&PailNum);
	for(i=0;i<PailNum;i++)
		fscanf(fin,"%d",&Pail[i]);
	qsort(Pail,PailNum,sizeof(int),comp);
	for(i=0;i<PailNum;i++)
		if(DFSID(0,i+1,-1))
			break;
	fprintf(fout,"%d",i+1);
	for(j=0;j<i+1;j++)
		fprintf(fout," %d",Choose[j]);
	fprintf(fout,"\n");
	return 0;
}


 

DFSID分别从数量少的开始枚举,对每个枚举用DP,判断是否可行

前面接触过DFSID的

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值