I haven't written it for a long time. I can't even write a DFS.
1 /* 2 ID: yingzho2 3 PROG: holstein 4 LANG: C++ 5 */ 6 #include <iostream> 7 #include <fstream> 8 #include <string> 9 #include <map>10 #include <vector>11 #include <set>12 #include <algorithm>13 #include <queue>14 #include <cmath>15 #include <list>16 #include <cstring>17 #include <cstdlib>18 #include <limits>19 #include <stack>20 21 using namespace std;22 23 ofstream fout ("holstein.out");24 ifstream fin ("holstein.in");25 26 int V, G;27 int target[30], input[30][30], cur[30], ans[30], tmp[30], size = 30;28 29 30 bool check() {31 for (int i = 0; i < V; ++i)32 if (cur[i] < target[i]) return false;33 return true;34 }35 36 void dfs(int x, int sum) {37 if (x > G) return;38 for (int i = 0; i < V; ++i) cur[i] += input[x][i];39 tmp[sum] = x;40 if (check()) {41 if (sum+1 < size) {42 size = sum + 1;43 for (int i = 0; i < V; ++i) ans[i] = tmp[i];44 }45 }46 else dfs(x+1, sum+1);47 for (int i = 0; i < V; ++i) cur[i] -= input[x][i];48 dfs(x+1, sum);49 }50 51 52 int main()53 {54 fin >> V;55 for (int i = 0; i < V; ++i) fin >> target[i];56 fin >> G;57 for (int i = 0; i < G; ++i) {58 for (int j = 0; j < V; ++j) {59 fin >> input[i][j];60 }61 }62 dfs(0, 0);63 fout << size << " ";64 for (int i = 0; i < size-1; ++i) fout << ans[i]+1 << " ";65 fout << ans[size-1]+1 << endl;66 return 0;67 }
Usaco section 2.1: Healthy Holsteins