【USACO 2.1.4】荷斯坦奶牛

【题目描述】

纪念“逝去”的Wecing


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

给出牛所需的最低的维他命量,输出喂给牛需要哪些种类的饲料,且所需的饲料剂量最少。

维他命量以整数表示,每种饲料最多只能对牛使用一次,数据保证存在解。

【格式】

INPUT FORMAT:(file holstein.in)

第1行:一个整数V(1<=V<=25),表示需要的维他命的种类数。

第2行:V个整数(1<=每个数<=1000),表示牛每天需要的每种维他命的最小量。

第3行:一个整数G(1<=G<=15),表示可用来喂牛的饲料的种数。

下面G行,第n行表示编号为n饲料包含的各种维他命的量的多少。

OUTPUT FORMAT:(file holstein.out)

输出文件只有一行,包括

牛必需的最小的饲料种数P

后面有P个数,表示所选择的饲料编号(按从小到大排列)。

如果有多个解,输出饲料序号最小的(即字典序最小)。

【分析】

由于饲料数量很少,可以根据其进行枚举,注意剪枝。

 1 #include <cstdlib>
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <cmath>
 6 #include <queue>
 7 #include <algorithm>
 8 const int maxn=25+10;
 9 using namespace std;
10 int need[maxn],n,m,Minl;
11 int V[maxn][maxn],ans;//各种饲料维生素含量 
12 int main()
13 {
14     int i,j,k;
15     //文件操作
16     freopen("holstein.in","r",stdin);
17     freopen("holstein.out","w",stdout); 
18     scanf("%d",&n);
19     for (i=1;i<=n;i++) scanf("%d",&need[i]);
20     scanf("%d",&m);
21     for (i=1;i<=m;i++)
22     for (j=1;j<=n;j++) scanf("%d",&V[i][j]);
23     Minl=0x7fffffff;
24     for (i=1;i<=(1<<m)-1;i++)//枚举 
25     {
26         int cnt=0,tot[maxn]={0};//统计饲料数量
27         for (k=0;k<m;k++) if (((1<<k)&i)==(1<<k)) cnt++;
28         if (cnt>Minl) continue;
29         for (k=0;k<m;k++) if (((1<<k)&i)==(1<<k)) for (j=1;j<=n;j++) tot[j]+=V[k+1][j];
30         
31         //检查是否合法
32         for (j=1;j<=n;j++) if (tot[j]<need[j]) goto w;
33         if (cnt<Minl || (cnt==Minl && ans>i)) {ans=i;Minl=cnt;}
34         w:continue;
35     }
36     printf("%d ",Minl);
37     for (k=0;k<m;k++) if (((1<<k)&ans)==(1<<k)) printf("%d ",k+1);
38     return 0;
39 }

 

posted @ 2014-06-14 20:54  TCtower  阅读(300)  评论(0编辑  收藏  举报