Question:
A total of S (S ≤ 8) courses, m in-service teachers, N job-seeking teachers.
Each instructor has his/her own salary requirements and can teach one or more courses.
It is required that in-service teachers should not be dismissed and asked how to hire candidates so that only two teachers in each course can teach and the total salary is minimized.
Analysis:
Because S is very small, you can use state compression.
DP (I, S1, S2) indicates that the previous I individual is taken into account. The set of courses taught by one person is S1, and at least two persons teach the set of S2.
In the recursion process, there is also a parameter S0, indicating the set of subjects that have not been taught.
Among them, the calculation of M0, M1, S0, S1 and S2 is in place, which is a good exercise.
1 # include <cstdio> 2 # include <string> 3 # include <iostream> 4 # include <cstring> 5 # include <sstream> 6 using namespace STD; 7 8 const int Maxs = 8; 9 const int maxn = 125; 10 const int INF = 1000000000; 11 int S, M, N, C [maxn], St [maxn], d [maxn] [1 <Maxs] [1 <Maxs]; 12 13 int dp (int I, int S0, int S1, int S2) 14 {15 if (I = m + n) return S2 = (1 <S)-1? 0: INF; 16 Int & Ans = d [I] [S1] [s2]; 17 if (ANS> = 0) return ans; 18 ans = inf; 19 if (I> = m) ans = dp (I + 1, S0, S1, S2); 20 int m0 = sT [I] & S0, m1 = sT [I] & S1; // m0 is a subject that the instructor can teach and is not taught yet. M1 is a set of 21 S0 ^ = M0 which can be taught by only one person; s1 = (S1 ^ M1) | M0; S2 | = m1; 22 ans = min (ANS, C [I] + dp (I + 1, S0, S1, s2); 23 return ans; 24} 25 26 int main () 27 {28 // freopen ("in.txt", "r", stdin); 29 int X; 30 string line; 31 While (Getline (CIN, line) 32 {33 stringstream SS (line); 34 SS >>> s >> M >> N; 35 if (S = 0) break; 36 37 memset (St, 0, sizeof (ST); 38 for (INT I = 0; I <m + N; ++ I) 39 {40 Getline (CIN, line); 41 stringstream SS (line); 42 SS> C [I]; 43 while (SS> X) st [I] | = (1 <(x-1); 44} 45 memset (D,-1, sizeof (d )); 46 printf ("% d \ n", dp (0, (1 <S)-1, 0, 0); 47} 48 49 return 0; 50}
Code Jun
10817 (shape pressure DP + Memory search) Headmaster's headache