您的位置:首页 > 其它

uva 10817

2014-05-12 21:31 295 查看
Problem D: Headmaster's Headache

Time limit: 2 seconds

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.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAX_N = 105;
const int INF = 7000000;
int S, M, N;
int ns1 = 0 , ns2 = 0;
int s[MAX_N], prize[MAX_N];
int dp[1 << 9][1 << 9];

void solve() {
dp[ns1][ns2] = prize[0];
dp[ns2][ns1] = prize[0];
for(int i = 1; i <= N; ++i) {
for(int s1 = (1 << S) - 1; s1 >= 0; --s1) {
for(int s2 = (1 << S) - 1; s2 >= 0; --s2) {
if(dp[s1][s2] != INF) {
dp[s1 | s[i]][s1 & s[i] | s2] =
min(dp[s1 | s[i]][s1 & s[i] | s2]
, dp[s1][s2] + prize[i]);

dp[s2 & s[i] | s1][s2 | s[i]] =
min(dp[s2 & s[i] | s1][s2 | s[i]]
, dp[s1][s2] + prize[i]);
}
}
}
}
}

int main()
{
//freopen("sw.in","r",stdin);
while(~scanf("%d%d%d", &S, &M, &N) && (S + M + N)) {
ns1 = 0; ns2 = 0;
for(int i = 0; i < (1 << S); ++i)
for(int j = 0; j < (1 << S); ++j) dp[i][j] = INF;
memset(prize, 0, sizeof(prize));
memset(s, 0, sizeof(s));

for(int i = 1; i <= M; ++i) {
int ch, v;
char c;
scanf("%d%d%c",&v, &ch, &c);
prize[0] += v;
ns2 |=  ns1 & (1 << (ch - 1));
ns1 |= 1 << (ch - 1);

while(c != '\n') {
scanf("%d%c", &ch, &c);
ns2 |=  ns1 & (1 << (ch - 1));
ns1 |= 1 << (ch - 1);
}

}

for(int i = 1; i <= N; ++i) {
int ch;
char c;
scanf("%d%d%c", &prize[i], &ch, &c);
s[i] |=  1 << (ch - 1);
while(c != '\n') {
scanf("%d%c", &ch, &c);
s[i] |= 1 << (ch - 1);
}
}

solve();
printf("%d\n", dp[(1 << S) - 1][(1 << S) - 1]);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: