【题解】UVA10817:Headmaster‘s Headache
@vjudge
记录有0、1、2个老师教的课的状态
s
0
,
s
1
,
s
2
s0,s1,s2
s0,s1,s2,并记录当前枚举到第几个老师
t
t
t
d
f
s
(
t
,
s
0
,
s
1
,
s
2
)
dfs(t,s0,s1,s2)
dfs(t,s0,s1,s2)
用记忆化,状态方面,
s
0
,
s
1
,
s
2
s0,s1,s2
s0,s1,s2中保存两个就好了,剩下一个用另外两个算出来,所以状态合理
Code:
#include <bits/stdc++.h>
#define maxn 310
#define maxm 210
using namespace std;
int kase, power[25], s, n, m, vis[maxm][maxn][maxn], dp[maxm][maxn][maxn], w[maxm], v[maxm];
int dfs(int t, int s0, int s1, int s2){
if (t > n + m) return s2 == power[s] - 1 ? 0 : 1e9;
if (vis[t][s1][s2] == kase) return dp[t][s1][s2];
vis[t][s1][s2] = kase;
int sum = 1e9;
if (t > m) sum = min(sum, dfs(t + 1, s0, s1, s2));
int x = s0 & v[t], y = s1 & v[t];
sum = min(sum, dfs(t + 1, s0 ^ x, s1 ^ y | x, s2 | y) + w[t]);
return dp[t][s1][s2] = sum;
}
int main(){
power[0] = 1;
for (int i = 1; i <= 10; ++i) power[i] = power[i - 1] << 1;
while (1){
scanf("%d%d%d", &s, &m, &n);
if (s == 0 && n == 0 && m == 0) break;
++kase;
for (int i = 1; i <= n + m; ++i){
scanf("%d", &w[i]);
v[i] = 0;
while (1){
char c = getchar();
if (isdigit(c)) v[i] |= power[(c ^ 48) - 1];
if (c == '\n') break;
}
}
printf("%d\n", dfs(1, power[s] - 1, 0, 0));
}
}
本文地址:https://blog.csdn.net/ModestCoder_/article/details/108564092