Water-like pressure dp.
DP (x, s) = max{DP (x-1, S-{h})} + reward (if obtained) (H∈s). Time complexity O (n * 2^n)
----------------------------------------------------------------------------------
#include <bits/stdc++.h>#define REP (i, n) for (int i = 0; i < n; ++i)#define CLR (x, C) memset (x, C, sizeof (x))#define foreach (E, x) for (__typeof (X.begin ()) E = X.begin (); E! = X.end (); e++)#define B (i) (1 << (i)) using namespace std;const int MAXN = n;int dp[2][b (MAXN)], S[MAXN][MAXN], N;vector<int> OK[MAXN];vector< pair<int, int> > REW[MAXN];int main () {freopen ("test.in", "R", stdin);CLR (DP, 0);int b;cin >> n >> b;Rep (i, N) {ok[i].clear ();rew[i].clear ();}While (b--) {int p, K, A;scanf ("%d%d%d", &p, &k, &a); p--;Rew[p].push_back (Make_pair (k, a));}int cur = 0, p = 1;Rep (i, N) Rep (j, N) scanf ("%d", S[i] + j);Rep (j, N) {Dp[cur][b (j)] = s[j][0];foreach (it, rew[0]) if (s[j][0] >= it->first)Dp[cur][b (j)] + = it->second;}Rep (T, b (n)) {int cnt = 0;Rep (i, N) if (T & B (i)) cnt++;if (cnt--) ok[cnt].push_back (t);}for (int i = 1; i < n; ++i) {swap (cur, p);foreach (it, ok[i]) {Rep (j, N) if (b (j) & *it)Dp[cur][*it] = max (Dp[cur][*it], Dp[p][*it ^ b (j)] + s[j][i]);foreach (t, Rew[i]) if (Dp[cur][*it] >= t->first)Dp[cur][*it] + = t->second;}}cout << dp[cur][b (n)-1] << "\ n";return 0;}
----------------------------------------------------------------------------------
3446: [Usaco2014 Feb]cow Decathlon Time Limit:Ten Sec Memory Limit:MB
Submit:58 Solved:41
[Submit] [Status] [Discuss] Description
FJhave aN (1 <= n <= 20)Head cow, number1toN. FJprovideNdifferent skills for the cows to learn, each cow can only learn one skill, each skill must have a cow to learn. SectionIHead Cows learn the firstJDoor skills,FJGet the scoreS[I][J],1<=s[i][j]<=1000. In addition, there areB (1 <= b <= 20)a reward, sectionIThe format of a reward is:Pi,Ki,Ai, the meaning of the expression is: If you finish the study beforeKiThe total score after the skill (including the bonus score) is not less thanPi, thenFJyou'll get extra.Aipoints. SoFJHow can you arrange cow learning skills to make the final score the highest?
Input
first line, N and B. Next there are line B, three integers per line:Pi ,Ki ,Ai. 1 <= Pi <= 40000,1 <= Ai <=. Next there are n rows n columns of two-dimensional arrays, representing S[i][j]. 1 <= s[i][j] <=.
Output
An integer that is the maximum score.
Sample Input 3 1
2 7 6
5 1 7
2 2 4
4 2 1
Sample Output 17
Cows 1 learning Skills 1, Cows 2 learning skills 3, cows 3 learning skills 2. HINT
Source
Gold
Bzoj 3446: [Usaco2014 Feb]cow Decathlon (pressure DP)