UVa 10817 Headmaster's Headache (状压DP+记忆化搜索)
搜索 39 DP UVa 记忆 状压
2023-09-11 14:17:18 时间
题意:一共有s(s ≤ 8)门课程,有m个在职教师,n个求职教师。每个教师有各自的工资要求,还有他能教授的课程,可以是一门或者多门。
要求在职教师不能辞退,问如何录用应聘者,才能使得每门课只少有两个老师教而且使得总工资最少。
析:用两个集合来表示状态,s1表示恰好有一个人教的科目,s2表示至少有两个人能教的科目。d(i, s1, s2),表示已经考虑了前 i 个人的最少花费。
这里把所有的人和科目都要从0开始标号,记忆化搜索,对于第 i 个人我们是选还是不选进行求解最小的花费。
剩下的搜索就好。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <stack> #include <sstream> using namespace std ; typedef long long LL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const double inf = 0x3f3f3f3f3f3f; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 120 + 5; const int mod = 1e9 + 7; const char *mark = "+-*"; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; int n, m; inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } inline int Min(int a, int b){ return a < b ? a : b; } inline int Max(int a, int b){ return a > b ? a : b; } int s; int c[maxn], st[maxn]; int d[maxn][1<<8][1<<8]; int dp(int i, int s0, int s1, int s2){ 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;// m0表示是第 i 个老师能教的科目,但是现在没教,m1表示第 i 个老师能教,并且已经有一个人教了, s0 ^= m0, s1 = (s1^m1)|m0; s2 |= m1; ans = Min(ans, c[i] + dp(i+1, s0, s1, s2));//选求职者 return ans; } int main(){ while(scanf("%d %d %d", &s, &m, &n) == 3){ if(!m && !n && !s) break; getchar(); string ss; memset(st, 0, sizeof(st)); for(int i = 0; i < n+m; ++i){ getline(cin, ss); stringstream sss(ss); int x; sss >> c[i]; while(sss >> x) st[i] |= (1<<x-1);//处理状态,注意要从0开始编号,所以要减去1 } memset(d, -1, sizeof(d)); int ans = dp(0, (1<<s)-1, 0, 0); printf("%d\n", ans); } return 0; }
相关文章
- Java实现 LeetCode 700 二叉搜索树中的搜索(遍历树)
- select、input[type='text']大小和搜索下拉 select美化
- 【二分】LeetCode 33. 搜索旋转排序数组【中等】
- Leetcode0450. 删除二叉搜索树中的节点(medium)
- paip.mysql fulltext 全文搜索.最佳实践.
- 点击文本框搜索,出现在下拉列表中
- 机器学习(十八):随机搜索和XGBoost
- 一种优化局部搜索能力的灰狼算法 -附代码
- 014-elasticsearch5.4.3【五】-搜索API【三】复合查询boolQuery、constantScoreQuery、disMaxQuery
- linux vim的全目录搜索 和 hostname的设置?