1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| #include <bits/stdc++.h> using namespace std;
const int N = 125, M = (1 << 8) + 5; int n, m, s, S[N], C[N], f[N][M][M];
int dfs(int x, int s0, int s1, int s2) { if (x == n + m) return s2 == (1 << s) - 1 ? 0 : 1000000000; int &ans = f[x][s1][s2]; if (ans != -1) return ans; ans = 1000000000; if (x >= m) ans = min(ans ,dfs(x + 1, s0, s1, s2)); int m0 = S[x] & s0, m1 = S[x] & s1; s0 ^= m0; s1 ^= m1; s1 |= m0; s2 |= m1; ans = min(ans, dfs(x + 1, s0, s1, s2) + C[x]); return ans; }
int main() {
int x; string line; while (getline(cin, line)) { stringstream ss(line); ss >> s >> m >> n; if (s == 0 && m == 0 && n == 0) break; for (int i = 0; i < n + m; i++) { getline(cin, line); stringstream ss(line); ss >> C[i]; S[i] = 0; while (ss >> x) S[i] = (S[i] | (1 <<(x - 1))); } memset(f, -1, sizeof f); printf("%d\n", dfs(0, (1 << s) - 1, 0, 0)); } return 0; }
|