1. 程式人生 > >Usaco Training Section 5.3 Milk Measuring

Usaco Training Section 5.3 Milk Measuring

有n種牛奶,問最少取多少種牛奶,使能組成q升,並輸出最小的一組解。(牛奶只能加,不能減)

寬搜會mle,所以直接id搜尋。

列舉取k種牛奶,然後dfs+check(dp)

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define inf 2147483647
#define mp make_pair
#define pii pair<int,int>
#define pb push_back
using namespace std;

inline int read(){
	int x=0;char c=getchar();
	while(c<'0'||c>'9') c=getchar();
	while(c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return x;
}

int m,n,k,a[105],b[105];
bool f[20005],ok;

inline bool check(){
	for(int i=1;i<=m;++i) f[i]=0;
	f[0]=1;
	for(int i=1;i<=k;++i){
		for(int j=0;j+a[b[i]]<=m;++j) if(f[j]) f[j+a[b[i]]]=1;
		if(f[m]) return 1;
	}
	return 0;
}

inline void dfs(int s){
	if(s==k){
		if(check()){
			ok=1;
			printf("%d ",k);
			for(int i=1;i<k;++i) printf("%d ",a[b[i]]);
			printf("%d\n",a[b[k]]);
		}
		return;
	}
	for(int i=b[s]+1;i<=n;++i){
		b[s+1]=i;
		dfs(s+1);
		if(ok) return;
	}
}

int main()
{
	freopen("milk4.in","r",stdin);
	freopen("milk4.out","w",stdout);
	m=read(),n=read();
	for(int i=1;i<=n;++i) a[i]=read();
	sort(a+1,a+n+1);
	for(k=1;k<=n;++k){
		if(!ok) dfs(0);
		else break;
	}
	return 0;
}