「杂题乱刷」P8687

题目链接

最典的状压 dp 了。

直接枚举每个状态然后用 01 背包的方式做即可。

时间复杂度 \(O(n2^m)\)

代码:

点击查看代码
/*
Tips:
你数组开小了吗?
你MLE了吗?
你觉得是贪心,是不是该想想dp?
一个小时没调出来,是不是该考虑换题?
*/
#include<bits/stdc++.h>
using namespace std;
#define map unordered_map
#define forl(i,a,b) for(register long long i=a;i<=b;i++)
#define forr(i,a,b) for(register long long i=a;i>=b;i--)
#define lc(x) x<<1
#define rc(x) x<<1|1
#define mid (l+r)>>1
#define cin(x) scanf("%lld",&x)
#define cout(x) printf("%lld",x)
#define lowbit(x) x&-x
#define pb push_back
#define pf push_front
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define endl '\n'
#define QwQ return 0;
#define ll long long
ll t;
ll sum,n,m,k,x,a[110],dp[1<<21];
void solve()
{
	cin>>n>>m>>k;
	forl(i,1,(1<<20)+10)
		dp[i]=1e18;
	forl(i,1,n)
	{
		sum=0;
		forl(j,1,k)
			cin>>x,sum|=1<<x-1;
		dp[sum]=1,a[i]=sum;
	}
	forl(i,0,(1<<m)-1)
		forl(j,1,n)
			dp[i|a[j]]=min(dp[i|a[j]],dp[i]+1);
	(dp[(1<<m)-1]==1e18)?cout<<-1:cout<<dp[(1<<m)-1];
}
int main()
{
	IOS;
	t=1;
	//cin>>t;
	while(t--)
		solve();
    /******************/
	/*while(L<q[i].l) */
	/*    del(a[L++]);*/
	/*while(L>q[i].l) */
	/*    add(a[--L]);*/
	/*while(R<q[i].r) */
	/*	  add(a[++R]);*/
	/*while(R>q[i].r) */
	/*    del(a[R--]);*/
    /******************/
	QwQ;
}
posted @ 2024-02-10 22:52  wangmarui  阅读(7)  评论(0编辑  收藏  举报