您的位置:首页 > 其它

uva 10817——Headmaster's Headache

2015-12-06 21:34 330 查看
题意:某个学校有m个老师和n个求职者,需要讲授s个课程,已知每个人的工资c和能交的课程,求花费最小使得每门课程都至少有两个人教。

思路:状压dp,将每个老师要交的课程压缩成一个数,然后对于每门课,找到每个老师教与不交的最小状态即可。(因为INF还被坑了几次??)

code:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <sstream>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const int INF=9999999;
const int inf=-INF;
const int N=125;
const int M=2005;
const int mod=1000000007;
const double pi=acos(-1.0);

#define cls(x,c) memset(x,c,sizeof(x))
#define ft(i,s,n) for (int i=s;i<=n;i++)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lrt rt<<1
#define rrt rt<<1|1
#define middle int m=(r+l)>>1
#define lowbit(x) (x&-x)
#define pii pair<int,int>
#define mk make_pair
#define IN freopen("in.txt","r",stdin);
#define OUT freopen("out.txt","w",stdout);

int n,m,s,c
,st
,d
[1<<8][1<<8];

int dp(int i,int s0,int s1,int s2)
{
if (i==m+n) { if (s2==((1<<s)-1)) return 0; return INF;}

int& ans=d[i][s1][s2];
if (ans>=0) return ans;
ans=INF;
if (i>=m) ans=min(ans,dp(i+1,s0,s1,s2));
int m=st[i]&s0,m1=st[i]&s1;
//s0^=m0; s1=(s1^m1)|m0;s2|=m1;
ans=min(ans,c[i]+dp(i+1,s0^m,(s1^m1)|m,s2|m1));
return ans;
}
int main()
{
while (~scanf("%d %d %d",&s,&m,&n),s)
{
ft(i,0,n+m-1)
{
scanf("%d",&c[i]);
st[i]=0;
char ch=getchar();
while (ch!='\n')
{
if (ch>='1'&&ch<='9') {int t=ch-'1';st[i]=(st[i]|(1<<t));}
ch=getchar();
}
//cout<<st[i]<<endl;
}
cls(d,-1);
printf("%d\n",dp(0,(1<<s)-1,0,0));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息