Uva10817 Headmaster's Headache

https://odzkskevi.qnssl.com/b506a3c20adad78678917d1ff4c9b953?v=1508327485

 

【题解】

dp[i][S1][S2]表示前i个教师选/不选已经决策完,当前有一个老师教的课程为S1,两个老师教的课程为S2,还需要的最小价值

答案为dp[0][0][0]

转移即可

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <sstream>
 7 #include <vector>
 8 #include <string>
 9 #include <cmath> 
10 #define min(a, b) ((a) < (b) ? (a) : (b))
11 #define max(a, b) ((a) > (b) ? (a) : (b))
12 
13 inline void swap(int &a, int &b)
14 {
15     int tmp = a;a = b;b = tmp;
16 }
17 
18 inline void read(int &x)
19 {
20     x = 0;char ch = getchar(), c = ch;
21     while(ch < '0' || ch > '9')c = ch, ch = getchar();
22     while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar();
23     if(c == '-')x = -x;
24 }
25 
26 const int INF = 0x3f3f3f3f;
27 const int MAXN = 100 + 10;
28 const int MAXM = 20 + 5;
29 const int MAXS = 8;
30 
31 struct Edge
32 {
33     int u,v,nxt;
34     Edge(int _u, int _v, int _nxt){u = _u;v = _v;nxt = _nxt;}
35     Edge(){}
36 }edge[MAXN << 1];
37 int head[MAXN], cnt;
38 
39 inline void insert(int a, int b)
40 {
41     edge[++cnt] = Edge(a,b,head[a]);
42     head[a] = cnt;
43 }
44 
45 int m,n,s,c[MAXN + MAXM],a[MAXN + MAXM],dp[MAXN + MAXM][1 << MAXS][1 << MAXS],ma;
46 std::string ch;
47 
48 int DP(int i, int s0, int s1, int s2)
49 {
50     if(i > n + m) return s2 == ma - 1 ? 0 : INF;
51     if(dp[i][s1][s2] >= 0)return dp[i][s1][s2];
52     dp[i][s1][s2] = INF;
53     if(i > m) dp[i][s1][s2] = DP(i + 1, s0, s1, s2);
54     int tmp1 = s0 & a[i], tmp2 = s1 & a[i];
55     dp[i][s1][s2] = min(dp[i][s1][s2], DP(i + 1, s0^tmp1, (s1 ^ tmp2) | tmp1, s2 | tmp2) + c[i]);
56     return dp[i][s1][s2];
57 }
58 
59 int main()
60 {
61     while(scanf("%d %d %d", &s, &m, &n) != EOF && n + m + s)
62     {
63         std::cin.get(); 
64         memset(dp, -1, sizeof(dp));
65         memset(a, 0, sizeof(a));
66         int tmp;
67         for(register int i = 1;i <= m + n;++ i) 
68         {
69             getline(std::cin, ch);
70             std::stringstream cc(ch);
71             cc >> c[i];
72             a[i] = 0;
73             while(cc >> tmp)
74                 a[i] |= 1 << (tmp - 1);
75         }
76         ma = 1 << s;
77         printf("%d\n", DP(1, ma - 1, 0, 0));
78     }
79     return 0;
80 } 
UVA10817

 

posted @ 2017-10-23 08:14  嘒彼小星  阅读(244)  评论(0编辑  收藏  举报