uva 10817 Headmaster's Headache (DP + 位运算)

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.

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 <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <cstring>
using namespace std;

const int maxn = 150;
const int MM = 0x3f3f3f3f;
int dp[1<<17] , sum , s[10];
struct teacher{
	int money;
	vector<int> teach;
}T[maxn];
string line;
int S , M , N;

void initial(){
	for(int i = 0;i < 1<<17;i++){
		dp[i] = MM;
	}
	for(int i = 0 ;i < maxn;i++){
		T[i].money = 0;
		T[i].teach.clear();
	}
	memset(s, 0 , sizeof s);
	line.clear();
	sum = 0;
}

void readcase(){
	int C , subject;
	for(int i = 0;i < M+N;i++){
		cin >> C;
		getline(cin , line);
		if(line.length() > 0){
			stringstream ss;
			ss << line;
			while(ss >> subject){
				T[i].teach.push_back(subject);
			}
			if(i < M){
				sum += C;
			}
		}
		T[i].money = C;
	}
}

void computing(){
	for(int i = 0;i < M;i++){
		for(int j = 0;j < T[i].teach.size();j++){
			//cout << T[i].teach[j] << " ";
			s[T[i].teach[j]]++;
		}
	}
	int ini = 0;
	for(int i = 1;i <= 8;i++){
		if(s[i] > 1){
			ini = (ini|(1<<(i+7)));
		}else if(s[i] == 1){
			ini = (ini|(1<<(i-1)));
		}
	}
	//cout << ":" << ini << " " << sum << endl;
	dp[ini] = sum;
	for(int i = M;i < N+M;i++){
		for(int k = 1<<16;k >= ini;k--){
			if(dp[k] != MM){
				int tem = k;
				for(int j = 0;j < T[i].teach.size();j++){
					int course = T[i].teach[j];
					if(!(tem & (1<<(course-1))) && !(tem & (1<<(course+7)))){//这个地方检查了一个下午!!位运算判零用非号,直接==0竟然不行!
						tem = (tem|(1<<(course-1)));
					}else if((tem & (1<<(course-1))) != 0){
						tem = ((tem-(1<<(course-1)))|(1<<(course+7)));
					}
				}
				dp[tem] = min(dp[tem] , dp[k]+T[i].money);
			}
		}
	}
	int ans = 0;
	for(int i = 1;i <= S;i++){
		ans = (ans|(1<<(i+7)));
	}
	cout << dp[ans] << endl;
}

int main(){
	while(cin >> S >> M >> N && S){
		initial();
		readcase();
		computing();
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值