状压dp

感谢小助理的帮助

 

14564: Get Everything

时间限制: 1 Sec  内存限制: 128 MB
提交状态

题目描述

We have N locked treasure boxes, numbered 1 to N.
A shop sells M keys. The i-th key is sold for ai yen (the currency of Japan), and it can unlock bi of the boxes: Box ci1, ci2, ..., cibi. Each key purchased can be used any number of times.
Find the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print −1.

Constraints
·All values in input are integers.
·1≤N≤12
·1≤M≤103
·1≤ai≤105
·1≤bi≤N
·1≤ci1

 

输入

Input is given from Standard Input in the following format:

N M
a1 b1
c11 c12 ... c1b1
:
aM bM
cM1 cM2 ... cMbM

输出

Print the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print −1.

样例输入 Copy

【样例1】
2 3
10 1
1
15 1
2
30 2
1 2
【样例2】
12 1
100000 1
2
【样例3】
4 6
67786 3
1 3 4
3497 1
2
44908 3
2 3 4
2156 3
2 3 4
26230 1
2
86918 1
3

样例输出 Copy

【样例1】
25
【样例2】
-1
【样例3】
69942

提示

样例1解释
We can unlock all the boxes by purchasing the first and second keys, at the cost of 25 yen, which is the minimum cost required.
样例2解释
We cannot unlock all the boxes.

标签

状压DP

#include 
using namespace std;
typedef unsigned long long ull;
typedef long long ll;

const int inf=0x3f3f3f3f;
int n,m;

struct node{
     int cost,num;
}nd[100005];

int f[1005][(1<<12)+5];//1左移12位再加5

int main()
{
    cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        int b;
        scanf("%d%d",&nd[i].cost,&b);
        int t=0;
        for(int j=1;j<=b;j++)
        {
              int temp;
              scanf("%d",&temp);
              t|=(1<<(temp-1));///1本来就在第一个位,所以要到第temp位,只需要向左移动temp-1位
        }
        nd[i].num=t;
    }
    memset(f,inf,sizeof(f));
    f[0][0]=0;///很重要
    for(int i=1;i<=m;i++)
    {
        for(int j=0;j<(1<

 

你可能感兴趣的:(题解)