USACO 2.1.4 Healthy Holsteins
题意:
给奶牛喂营养素,一共有V种维生素需求,然后有G勺喂养(每勺只喂一次),并给出每勺喂养所获得的对应维生素量,
给出至少V种维生素所要达到的需求,求出最少的勺数,如果勺子数相同,勺子编号尽量小
解法:
一共G勺(G<=15),每勺只有0和1两种情况,那么枚举每一种方案,一共2^15种方案
/* ID: lsswxr1 PROG: holstein LANG: C++ */ #include <iostream> #include <vector> #include <map> #include <list> #include <set> #include <deque> #include <stack> #include <queue> #include <algorithm> #include <cmath> #include <cctype> #include <cstdio> #include <iomanip> #include <cmath> #include <cstdio> #include <string> #include <cstring> #include <fstream> using namespace std; #define USACO #ifdef USACO #define cin fin #define cout fout #endif ////////////////////////////////////////////////////////////////////////// ///宏定义 const int INF = 1000000000; const int MAXN = 40001; const int maxn = MAXN; ///全局变量 和 函数 int V, G; int states[maxn]; int feedWays[30][30]; int needs[30]; struct solution { vector<int> feeds; int nums; bool operator < (const solution& T) const { if (nums != T.nums) { return nums < T.nums; } else { for (int i = 0; i < nums; i++) { if (feeds[i] != T.feeds[i]) { return feeds[i] < T.feeds[i]; } } } } }; solution sol[maxn]; //数组开小了错了一次,最多可能有2^15个方案 int main() { #ifdef USACO ofstream fout ("holstein.out"); ifstream fin ("holstein.in"); #endif while (cin >> V) { for (int i = 0; i < maxn; i++) { sol[i].feeds.clear(); sol[i].nums = 0; } //输入需求 for (int i = 0; i < V; i++) { int num; cin >> num; needs[i] = num; } //输入喂养方案 cin >> G; for (int i = 0; i < G; i++) { for (int j = 0; j < V; j++) { cin >> feedWays[i][j]; } } int p = pow((double)2, G); //G写成了V。。 int cnt = 0; int getFeeds[30]; for (int i = 0; i < p; i++) { for (int pos = 0; pos < V; pos++) { getFeeds[pos] = 0; } solution temp; temp.feeds.clear(); temp.nums = 0; for (int j = 0; j < G; j++) //G写成了V { if (((1 << j) & i) != 0) { temp.nums++; temp.feeds.push_back(j + 1); for (int k = 0; k < V; k++) { getFeeds[k] += feedWays[j][k]; } } } bool flag = true; for (int j = 0; j < V; j++) { if (getFeeds[j] < needs[j]) { flag = false; break; } } if (flag) { sol[cnt++] = temp; //SOL数组开小了。。。 } } sort(sol, sol + cnt); cout << sol[0].nums; for (int i = 0; i < sol[0].nums; i++) { cout << " " << sol[0].feeds[i]; } cout << endl; } ///结束 return 0; }