当前位置:网站首页>Blue Bridge Cup: Candy

Blue Bridge Cup: Candy

2022-06-21 07:58:00 lsgoose

Title Description

The owners of the candy store have MM Kinds of sweets for sale . For ease of description , We will MM A flavor number 1∼ MM.

Xiaoming hopes to taste all kinds of sweets . Unfortunately, the boss doesn't sell candy alone , It is KK One bag for sale .

It's a good thing that the candy package says KK The taste of a candy , So Xiaoming can know the taste of candy in each package before buying it .

Given NN Pack candy , Please calculate how many bags Xiaoming should buy at least , You can taste all kinds of candy .

 

Output description

Output an integer for the answer . If Xiaoming can't taste all the flavors , Output −1.

I/o sample

Example

Input

6 5 3
1 1 2
1 2 3
1 1 3
2 3 5
5 4 2
5 1 2

Output

2

Operation limit

  • Maximum operation time :1s
  • Maximum running memory : 256M

It's clever , For the first time, I have encountered this kind of binary dynamic programming , Here, an array is used to store the status of each package ,

The code is as follows :

#include <bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f,N=1e2+7;
int bag[N];
int f[1<<20];

int main()
{
  //  Please enter your code here 
  int n,m,k;
  scanf("%d%d%d",&n,&m,&k);
  int all=(1<<m)-1;
  for(int i=0;i<=all;++i) f[i]=INF;
  for(int i=0;i<n;++i)
  {
    int tmp=0,state=0;
    for(int j=0;j<k;++j)
    {
      scanf("%d",&tmp);
      tmp--;
      state |= 1<<tmp;
    }
    bag[i]=state;
    f[state]=1;
  }
  for(int i=0;i<n;++i)
  {
    for(int j=0;j<=all;++j)
    {
      if(f[j]==INF) continue;
      f[j|bag[i]]=min(f[j|bag[i]],f[j]+1);
    }
  }
  if(f[all]==INF) cout<<-1<<endl;
  else cout<<f[all]<<endl;
  return 0;
}

原网站

版权声明
本文为[lsgoose]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202221505532357.html