【 状压DP】 [UVA10817] Headmaster's Headache

书上例题,LRJ的位运算确实很厉害。顺带一提我的读入用的是大哥王SYK的,真是好用。

#include<cstdio>  
#include<algorithm>  
#include<cstring>  
#include<string>  
#include<iostream>  
#include<sstream>  
using namespace std;
const int INF = 1e9;
const int MAXN = 120;
const int MAXS = 8;
using namespace std;
int t,m,n;
int c[MAXN + 10];
int st[MAXN +10];
int d[MAXN +10][(1 << MAXS) +10][(1 << MAXS) +10];
int dp(int i,int s0,int s1,int s2){
	if(i == m + n)
		return s2 == (1 << t) - 1 ? 0 : INF;
		int &ans = d[i][s1][s2];
		if(ans >= 0) return ans;
		ans = INF;
		if(i >= m)
			ans = dp(i + 1,s0,s1,s2);
		int m0 = st[i] & s0,m1 = st[i] & s1;
		s0 ^= m0; s1= (s1 ^ m1) | m0; s2 |= m1;
		ans = min(ans,c[i] + dp(i + 1,s0,s1,s2));
		return ans;
}
int main()
{
	int x;
	string s;
    while(getline(cin, s))  
    {  
        stringstream ss(s);  
        ss >> t >> m >> n;  
        if(!(t + m + n)) break;  
		memset(st,0,sizeof(st));
		for(int i = 0; i < m + n; i++)
		{
			getline(cin, s);
			stringstream ss(s);
			ss >> c[i];
			while(ss >> x) 
				st[i] |= (1 << (x-1));
		}
		memset(d, -1, sizeof(d));
		printf("%d\n",dp(0,(1 << t) - 1,0,0));
	}
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值