Description
农民JOHN以拥有世界上最健康的奶牛为骄傲。他知道每种饲料中所包含的的牛所需的最低的维他命量是多少。请你帮助农夫喂养他的牛,以保持他们的健康,使喂给牛的饲料的种数最少。 给出牛所需的最低的维他命,输出喂给牛需要哪些种类的饲料,且所需的种类数最少。
Input
第1行:一个整数V(1<=V<=25),表示需要的维他命的种类数。 第2行:V个整数(1<=每个数<=1000),表示牛每天需要的维他命的最小量。 第3行:一个整数G(1<=G<=15),表示可用来喂牛的饲料的数量。下面G行,第i行表示编号为i饲料包含的各种维他命的量的多少。
Output
输出文件只有一行,包括: 牛必需的最小的饲料种数P 后面有P个数,表示所选择的饲料编号(按从小到大排列)。
Sample Input
4
100 200 300 400
3
50 50 50 50
200 300 200 300
900 150 389 399
Sample Output
2 1 3
这个题的G很小,所以只需要对所有情况枚举即可,然后取最小值
这里从0到2^G跑一遍,然后输出二进制数中'1'最少的那一个答案即可
#include <map>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <fstream>
#include <iostream>
#include <sstream>
#include <algorithm>
#define lowbit(a) (a&(-a))
#define _mid(a,b) ((a+b)/2)
#define _mem(a,b) memset(a,0,(b+3)<<2)
#define fori(a) for(int i=0;i<a;i++)
#define forj(a) for(int j=0;j<a;j++)
#define ifor(a) for(int i=1;i<=a;i++)
#define jfor(a) for(int j=1;j<=a;j++)
#define mem(a,b) memset(a,b,sizeof(a))
#define IN freopen("in.txt","r",stdin)
#define OUT freopen("out.txt","w",stdout)
#define IO do{\
ios::sync_with_stdio(false);\
cin.tie(0);\
cout.tie(0);}while(0)
#define mp(a,b) make_pair(a,b);
using namespace std;
typedef long long ll;
const int maxn = 50;
const int INF = 0x3f3f3f3f;
const int inf = 0x3f;
const double EPS = 1e-7;
const double Pi = acos(-1);
const int MOD = 1e9+7;
int a[maxn];
int b[maxn][maxn];
int num[maxn];
int v,g;
int getbit(int x,int i){return (x>>i)&1;}
int getsum(int x,int level){ //第x种情况的第level种维他命含量
int sum = 0;
for(int i=0;i < 15;i++)
if(getbit(x,i))
sum += b[i][level];
return sum;
}
int getone(int x){ //x的二进制中'1'的数量
int i=0;
while(x){
i += x&1;
x >>= 1;
}
return i;
}
bool juge(int x){
fori(v)
if(a[i]>getsum(x,i))
return false;
return true;
}
int main() {
//IN;
int res = INF;
cin >> v;
fori(v)
cin >> a[i];
cin >> g;
fori(g)
forj(v)
cin >> b[i][j];
fori(1<<g){
if(juge(i))
if(getone(res) > getone(i))
res = i;
}
cout << getone(res);
for(int i=1;res;i++){
if(res & 1)
cout <<" " << i;
res >>= 1;
}
cout << endl;
return 0;
}