UVA10817 Headmaster's Headache
6057 ワード
変換元:https://blog.csdn.net/qq_29869107/article/details/52059968テーマ大意m教師、n求職者、共同でs科目を募集します.要求:各課程には2人の科が先生を務めなければならず、教師は採用しなければならない.問題を解く構想は直接状態遷移方程式を与え,具体的な意味はコードを参照する.d[i][s1][s2] = min(d[i+1][s1’][s2’]+c[i], d[i+1][s1][s2]); The headmaster of Spring Field School is considering employing some new teachers for certain subjects. There are a number of teachers applying for the posts. Each teacher is able to teach one or more subjects. The headmaster wants to select applicants so that each subject is taught by at least two teachers, and the overall cost is minimized. Input The input consists of several test cases. The format of each of them is explained below: The first line contains three positive integers S, M and N. S (≤ 8) is the number of subjects, M (≤ 20) is the number of serving teachers, and N (≤ 100) is the number of applicants. Each of the following M lines describes a serving teacher. It first gives the cost of employing him/her (10000 ≤ C ≤ 50000), followed by a list of subjects that he/she can teach. The subjects are numbered from 1 to S. You must keep on employing all of them. After that there are N lines, giving the details of the applicants in the same format. Input is terminated by a null case where S = 0. This case should not be processed. Output For each test case, give the minimum cost to employ the teachers under the constraints. Sample Input 2 2 2 10000 1 20000 2 30000 1 2 40000 1 2 0 0 0 Sample Output 60000
#include"stdafx.h"
#include
#include
#include
#include
#include
using namespace std;
// ,
const int maxn = 100 + 20 + 5;
const int maxs = 8;
const int INF = 1000000000;
int m, n, s, c[maxn], st[maxn], d[maxn][1 << maxs][1 << maxs];
//c[maxn] ,st[maxn] ,d[][][]
//s0 ,s1 ,s2 , 1
int dp(int i, int s0, int s1, int s2)
{
if (i == m + n) return s2 == (1 << s) - 1 ? 0 : INF;//s2 1 。0
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,m1 i
s0 ^= m0;// s0, i , 0
s1 = (s1^m1) | m0;//s1 , m1 , s0 m1
s2 |= m1;// s1 s2
ans = min(ans, c[i] + dp(i + 1, s0, s1, s2));//
return ans;
}
int main()
{
int x;
string line;
while (getline(cin, line))
{
stringstream ss(line);
ss >> s >> m >> n;
if (s == 0) break;
//
for (int i = 0; i < m + n; i++)
{
getline(cin, line);
stringstream ss(line);
ss >> c[i];
st[i] = 0;
while (ss >> x)st[i] |= (1 << (x - 1));// st
}
memset(d, -1, sizeof(d));
cout << dp(0, (1 << s) - 1, 0, 0) << endl;// , s0 1,
}
return 0;
}