蓝桥杯--状态压缩DP
P1460 - [蓝桥杯2019初赛]糖果 - New Online Judge (ecustacm.cn)
题解参考:(9条消息) 蓝桥杯省赛[新手向题解] 2019 第十届 C/C++ A组 第九题 DP_weixin_33801856的博客-CSDN博客
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 1 << 20;
int n, m, k;
int f[N], w[N];
int main()
{
cin >> n >> m >> k;
memset(f, 0x3f, sizeof f);//求最小值我们就初始化为最大值
for(int i = 0; i < n; i ++ )
{
int st = 0;
for(int j = 0; j < k; j ++ )
{
int x; cin >> x;
x = pow(2, x - 1);
st |= x;//不可以直接或pow(2, x - 1)
//st |= 1 << x;
}
w[i] = st;
f[st] = 1;
}
for(int i = 0; i < n; i ++ )
{
int now = w[i];
if(now == 0x3f3f3f3f) continue;
for(int j = 0; j < (1 << m); j ++ )
f[now | j] = min(f[now | j], f[j] + 1);
}
int res = f[(1 << m) - 1];
if(res == 0x3f3f3f3f) res = -1;
cout << res << endl;
return 0;
}