使用排列组合,遍历所有可能的情况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-7 ; const int M = 660000 ; const ll P = 10000000097ll ; const int INF = 0x3f3f3f3f ; const int MAX_N = 20 ; const int MAXSIZE = 101000000; int vv[30], aa[20][30]; int ans[30], a[30], cur[30]; int v, g; void dfs(int n, int w, int a[]){ int i, j; memset(cur, 0, sizeof(cur)); for(i = 1; i <= a[0]; ++i){ for(int j = 0; j < v; ++j){ cur[j] += aa[a[i]][j]; } } for(i = 0; i < v; ++i){ if(cur[i] < vv[i]) break; } if(i == v){ if(w < ans[0]){ /* for(int i = 1; i <= a[0]; ++i){ cout << a[i] + 1 << ‘ ‘; } cout << endl; */ for(i = 0; i <= a[0]; ++i){ ans[i] = a[i]; } return; } } for(int i = n + 1; i < g; ++i){ ++a[0]; a[a[0]] = i; dfs(i, w + 1, a); --a[0]; } } int main() { ofstream fout ("holstein.out"); ifstream fin ("holstein.in"); int i, j, k, t, n, s, c, w, q; fin >> v; for(i = 0; i < v; ++i){ fin >> vv[i]; } fin >> g; for(i = 0; i < g; ++i){ for(j = 0; j < v; ++j){ fin >> aa[i][j]; } } ans[0] = INF; for(i = 0; i < g; ++i){ a[0] = 1; a[1] = i; dfs(i, 1, a); } fout << ans[0]; for(i = 1; i <= ans[0]; ++i){ fout << ‘ ‘ << ans[i] + 1; } fout << endl; fin.close(); fout.close(); return 0; }
时间: 2024-11-08 22:50:50