UVA - 10817 Headmaster's Headache(状压DP)

解决UVA-10817题目,通过动态规划算法找到最少工资支出方案,确保每门课程都有至少两名教师教授。在职教师必须被雇佣。

题目链接:https://vjudge.net/problem/UVA-10817

题意:某校有n个教师和m个求职者,已知每人的工资和能教的课程集合,要求支付最少的工资使得每门课都至少有两名教师教学,

在职教师必须招聘。

AC代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int INF=1<<30;
int s,m,n;
int st[130],c[130];
int d[130][300][300];
int dp(int i,int s0,int s1,int s2){//第i个人 没有教过的集合s0 一个人教过的s1 两个人教过的s2
	if(i==m+n)   return	s2==(1<<s)-1? 0:INF;
	int &ans=d[i][s1][s2];  //  记忆化搜索 
	if(ans>=0)	return ans;
	int ans=INF;
	if(i>=m) ans=dp(i+1,s0,s1,s2);
	int m0=s0&st[i],m1=st[i]&s1;
	s0=s0^m0;
	s1=(s1^m1)|m0;
	s2=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)!=EOF&&s){
		getchar();
		memset(st,0,sizeof(st));
		memset(c,0,sizeof(c));
		memset(d,-1,sizeof(d));
		char ch[130];
		for(int i=0;i<m;i++){
			gets(ch);
			sscanf(ch,"%d",&c[i]);
			int len=strlen(ch);
			for(int j=6;j<len;j++)//6代表为工资与空格所占位数 
				if(isdigit(ch[j]))
				{
					int x;
					sscanf(ch+j,"%d",&x);
					st[i]=st[i]|(1<<(x-1));
				}
		}
		for(int i=m;i<m+n;i++){
			gets(ch);
			sscanf(ch,"%d",&c[i]);
			int len=strlen(ch);
			for(int j=6;j<len;j++)
				if(isdigit(ch[j]))
				{
					int x;
					sscanf(ch+j,"%d",&x);
					st[i]=st[i]|(1<<(x-1));
				}
		}
		int ans=dp(0,(1<<s)-1,0,0);
		printf("%d\n",ans);
	}
} 

  

 

转载于:https://www.cnblogs.com/djh0709/p/9588436.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值