Bundles of Joy

Problem description
Bob’s Bakery is celebrating its grand opening! To commemorate this exciting occasion, they are offering a “Bundles of Joy” sale to encourage people to sample their full range of delectable desserts.
For example, you can buy the “Chocolate Cakes” bundle which includes chocolate layer cake and black forest cake for $20. Or you can buy the “Fruity Cakes” bundle which includes lemon pound cake and key lime cake, also for $20. They offer an even bigger bundle that includes a slice of each of these cakes for an even lower price of $38.
You want to try out each dessert they offer. So, you need to buy some bundles to ensure you get at least one of each dessert. Of course, your goal is to do this while minimizing the amount of money you spend on bundles. Finally, you make a few observations about the bundles they offer:
  • For any two bundles A and B, either every dessert in A is also in B, every dessert in B is also in A, or there is no dessert in both A and B.
  • The only way to buy an item individually is if it is in a bundle of size 1. Not all items are in such a bundle.
  • The pricing is not very well thought out. It may be cheaper to acquire items in a bundle B by buying some combination of other bundles rather than B itself.



Input
The first line contains a single integer T ≤ 50 indicating the number of test cases. The first line of each test case contains two integers n and m where n is the number of different types of desserts offered by Bob’s Bakery and m is the number of different bundles. Here, 1 ≤ n ≤ 100 and 1 ≤ m ≤ 150.
Then m lines follow, each describing a bundle. The ith such line begins with two positive integers pi and si. Here, 0 < pi ≤ 106 is the price of bundle i and 1 ≤ si ≤ n is the number of items in bundle i. The rest of this line consists of si distinct integers ranging from 1 to n, indicating what desserts are included in this bundle.
Each of the n items will appear in at least one bundle.


Output
The output for each test case is a single line containing the minimum cost of purchasing bundles to ensure you get at least one of each item. This value is guaranteed to fit in a 32-bit signed integer.


Sample Input
4
4 3
20 2 1 2
20 2 3 4
38 4 1 2 3 4
2 3
5 1 1
10 2 1 2
4 1 2
2 2
1 1 1
5 2 2 1
1 2
2 1 1
1 1 1
Sample Output
38
9
5
1
Problem Source
RCMC 2015

#include<iostream>
#include<vector>
#include<stdio.h>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
vector<int> cnt[200],types[200];
int cost[200],low[200],t,n,m;
void solve(int num){
    int sum=0;
    for(int i=0;i<types[num].size();i++){
        int good=types[num][i];
       // printf("%dlalala%d\n",good,low[good]);
        if(low[good]==-1){
            sum=cost[num];
            low[good]=num;
        }
        else if(low[good]!=num){
            sum+=cost[low[good]];
           // printf("%d %d %d %d\n",num,good,sum,low[good]);
            int tmp=low[good];
            for(int j=0;j<types[tmp].size();j++)
                low[types[tmp][j]]=num;
        }
    }
    //for(int i=1;i<=n;i++) printf("%d ",low[i]);
    //printf("%d  %d\n",num,sum);
    cost[num]=min(cost[num],sum);
}
int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        for(int i=0;i<=n;i++) cnt[i].clear(),low[i]=-1;
        for(int i=0;i<=m;i++) types[i].clear();
        for(int i=0;i<m;i++){
            int num,good;
            scanf("%d%d",&cost[i],&num);
            cnt[num].push_back(i);
            while(num--){
                scanf("%d",&good);
                types[i].push_back(good);
            }
        }
        for(int i=1;i<=n;i++) types[m].push_back(i);
        cost[m]=inf,cnt[n].push_back(m);
        /*for(int i=0;i<=m;i++){
            for(int j=0;j<types[i].size();j++)
                 printf("%d ",types[i][j]);
            printf("\n");
        }*/
        for(int i=1;i<=n;i++)
            for(int j=0;j<cnt[i].size();j++)
                solve(cnt[i][j]);
        printf("%d\n",cost[m]);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值