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));
}
}
思路:状压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));
}
}
相关文章推荐
- 基于Android中dp和px之间进行转换的实现代码
- Android中dip、dp、sp、pt和px的区别详解
- LFC1.0.0 版本发布
- Android px、dp、sp之间相互转换
- HP data protector软件学习1--基本角色与基本工作流程
- HP data protector软件学习2--软件组成与界面介绍
- android中像素单位dp、px、pt、sp的比较
- Android对px和dip进行尺寸转换的方法
- Android根据分辨率进行单位转换-(dp,sp转像素px)
- android 尺寸 dp,sp,px,dip,pt详解
- DP问题各种模型的状态转移方程
- POJ-1695-Magazine Delivery-dp
- nyoj-1216-整理图书-dp
- TYVJ1193 括号序列解题报告
- 对DP的一点感想
- TYVJ上一些DP的解题报告
- soj1005. Roll Playing Games
- 01背包问题
- LeetCode之Maximum Product Subarray
- DP Flow