USACO Healthy Holsteins DFS

时间:2023-03-08 20:50:09

使用排列组合,遍历所有可能的情况C(1)+C(2)+C(3)……C(n)= 2^G种组合

数据规模不大,暴力过去最多也就是2^15 = 23768种情况

所以就暴力咯,不过还是Debug了一会

Source Code:

/*
ID: wushuai2
PROG: holstein
LANG: C++
*/
//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0) using namespace std; typedef long long ll ;
typedef unsigned long long ull ;
typedef unsigned int uint ;
typedef unsigned char uchar ; template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;} const double eps = 1e- ;
const int M = ;
const ll P = 10000000097ll ;
const int INF = 0x3f3f3f3f ;
const int MAX_N = ;
const int MAXSIZE = ; int vv[], aa[][];
int ans[], a[], cur[];
int v, g; void dfs(int n, int w, int a[]){ int i, j;
memset(cur, , sizeof(cur));
for(i = ; i <= a[]; ++i){
for(int j = ; j < v; ++j){
cur[j] += aa[a[i]][j];
}
}
for(i = ; i < v; ++i){
if(cur[i] < vv[i]) break;
}
if(i == v){
if(w < ans[]){
/*
for(int i = 1; i <= a[0]; ++i){
cout << a[i] + 1 << ' ';
}
cout << endl;
*/
for(i = ; i <= a[]; ++i){
ans[i] = a[i];
}
return;
}
}
for(int i = n + ; i < g; ++i){
++a[];
a[a[]] = i;
dfs(i, w + , a);
--a[];
}
} int main() {
ofstream fout ("holstein.out");
ifstream fin ("holstein.in");
int i, j, k, t, n, s, c, w, q; fin >> v;
for(i = ; i < v; ++i){
fin >> vv[i];
}
fin >> g;
for(i = ; i < g; ++i){
for(j = ; j < v; ++j){
fin >> aa[i][j];
}
}
ans[] = INF;
for(i = ; i < g; ++i){
a[] = ;
a[] = i;
dfs(i, , a);
}
fout << ans[];
for(i = ; i <= ans[]; ++i){
fout << ' ' << ans[i] + ;
}
fout << endl; fin.close();
fout.close();
return ;
}