UVA10817HeadmastersHeadache

xiaoxiao2021-02-28  116

//UVA10817HeadmastersHeadache #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<sstream> #include<string> using namespace std; const int MAXS = 8; const int MAXN = 120 + 5; const int INF = 1e9; int s, m, n; int cost[MAXN], st[MAXN], d[MAXN][1 << MAXS][1 << MAXS]; int dp(int i, int s0, int s1, int s2) { //printf("****, i = %d, m = %d, n = %d\n", i, m, n); if(i == m + n) return s2 == (1 << s) - 1 ? 0 : INF; int& ans = d[i][s1][s2]; if(ans >= 0) return ans; //记忆化,避免重复 ans = INF; if(i >= m) ans = dp(i + 1, s0, s1, s2); //此时的决策有两种,先考虑一种 int m0 = st[i] & s0, m1 = st[i] & s1; s0 ^= m0;//m0中的元素即将被添加到s1中,将s0中的m0部分除去 s1 = (s1 ^ m1) | m0;//m1将被加入s2,所以要从s1中除去 s2 |= m1; ans = min(ans, dp(i + 1, s0, s1, s2) + cost[i]); return ans; } int main() { string line; while(getline(cin, line)) { memset(d, -1, sizeof(d)); memset(st, 0, sizeof(st)); stringstream ss(line); ss >> s >> m >> n; if(s == 0) return 0; for(int i = 0; i < m + n; i++) { getline(cin, line); stringstream s1(line); s1 >> cost[i]; int x; while(s1 >> x) st[i] |= (1 << (x - 1)); } printf("%d\n", dp(0, (1 << s) - 1, 0, 0)); } return 0; } /* 2 2 2 10000 1 20000 2 30000 1 2 40000 1 2 0 0 0 */
转载请注明原文地址: https://www.6miu.com/read-80981.html

最新回复(0)