CF417D--- Cunning Gena(序列+像缩进dp)

A boy named Gena really wants to get to the “Russian Code Cup” finals, or at least get a t-shirt. But the offered problems are too complex, so he made an arrangement with his n friends that they will solve the problems for him.

The participants are offered m problems on the contest. For each friend, Gena knows what problems he can solve. But Gena’s friends won’t agree to help Gena for nothing: the i-th friend asks Gena xi rubles for his help in solving all the problems he can. Also, the friend agreed to write a code for Gena only if Gena’s computer is connected to at least ki monitors, each monitor costs b rubles.

Gena is careful with money, so he wants to spend as little money as possible to solve all the problems. Help Gena, tell him how to spend the smallest possible amount of money. Initially, there’s no monitors connected to Gena’s computer.

Input

The first line contains three integers n, m and b (1?≤?n?≤?100; 1?≤?m?≤?20; 1?≤?b?≤?109) — the number of Gena’s friends, the number of problems and the cost of a single monitor.

The following 2n lines describe the friends. Lines number 2i and (2i?+?1) contain the information about the i-th friend. The 2i-th line contains three integers xi, ki and mi (1?≤?xi?≤?109; 1?≤?ki?≤?109; 1?≤?mi?≤?m) — the desired amount of money, monitors and the number of problems the friend can solve. The (2i?+?1)-th line contains mi distinct positive integers — the numbers of problems that the i-th friend can solve. The problems are numbered from 1 to m.

Output

Print the minimum amount of money Gena needs to spend to solve all the problems. Or print -1, if this cannot be achieved.

Sample test(s)

Input

2 2 1

100 1 1

2

100 2 1

1

Output

202

Input

3 2 5

100 1 1

1

100 1 1

2

200 1 2

1 2

Output

205

Input

1 2 1

1 1 1

1

Output

-1

看到m那么小,就直接想到状压dp了,可是这里有一个monitors的限制,不能暴力枚举这个值

能够先把输入数据按每个人的monitors排序,这样从小到大枚举每个人,边递推边记录了答案即可

/*************************************************************************
    > File Name: CF417D.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年03月16日 星期一 12时33分11秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1.0);
const long long inf = (1LL << 60);
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

LL dp[(1 << 20) + 10];

struct node
{
    int sta;
    int cost;
    int num;
}fri[110];

int cmp (node a, node b)
{
    return a.num < b.num;
}

int main ()
{
    int n, m, b;
    while (~scanf("%d%d%d", &n, &m, &b))
    {
        for (int i = 0; i <= (1 << m); ++i)
        {
            dp[i] = inf;
        }
        dp[0] = 0;
        for (int i = 1; i <= n; ++i)
        {
            int cnt;
            fri[i].sta = 0;
            int x;
            scanf("%d%d%d", &fri[i].cost, &fri[i].num, &cnt);
            for (int j = 0; j < cnt; ++j)
            {
                scanf("%d", &x);
                fri[i].sta |= (1 << (x - 1));
            }
        }
        LL ans= inf;
        sort (fri + 1, fri + 1 + n, cmp);
        for (int i = 1; i <= n; ++i)
        {
            for (int j = 0; j < (1 << m); ++j)
            {
                dp[j | fri[i].sta] = min (dp[j] + fri[i].cost, dp[j | fri[i].sta]);
            }
            ans = min (ans, dp[(1 << m) - 1] + (LL)fri[i].num * b);
        }
        if (ans >= inf)
        {
            printf("-1\n");
        }
        else
        {
            cout << ans << endl;
        }
    }
    return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

时间: 2024-10-08 14:50:36

CF417D--- Cunning Gena(序列+像缩进dp)的相关文章

codeforce 417D Cunning Gena (状压DP)

原题地址:http://codeforces.com/problemset/problem/417/D 题意: Gena 为了解决m个问题,请他的朋友们帮忙,其中第i个朋友可以解决某mi个问题,需要花费xi卢布,并且要求安装至少ki个显示器,每个显示器需要b卢布. 问解决所有问题需要多少多少卢布 题解 考虑问题数目很小,可以状压DP. dp[S]表示当前状态的最优解,通过位运算进行转移. 考虑|没有逆运算,所以只能正推更新. 首先把朋友按照k排序,避免k的干扰. dp[][0]和dp[][1]分

Codeforces 417D Cunning Gena(状态压缩dp)

题目链接:Codeforces 417D Cunning Gena 题目大意:n个小伙伴.m道题目,每一个监视器b花费,给出n个小伙伴的佣金,所须要的监视器数,以及能够完毕的题目序号. 注意,这里仅仅要你拥有的监视器数量大于小伙伴须要的监视器数量就可以. 求最少花费多少金额能够解决全部问题. 解题思路:dp[i],i为一个二进制数.表示完毕这些题目的最小代价,可是这里要注意,由于有个监视器的数量.普通情况下要开一个二维的状态.可是2^20次方有一百万,再多一维的数组会超内存,所以我的做法是将每一

Cunning Gena CodeForces - 417D

Cunning Gena CodeForces - 417D 题意 先将小伙伴按需要的监视器数量排序.然后ans[i][j]表示前i个小伙伴完成j集合内题目所需最少钱.那么按顺序枚举小伙伴,用ans[i-1][j]更新ans[i][j]和ans[i][j | 第i个小伙伴能完成题目的集合](更新后一种时答案要加上第i个小伙伴的费用). 由于小伙伴已经按监视器数量排序了,对于每个枚举出的小伙伴i,可以试着将其作为最后一个要求助的小伙伴,那么此时监视器上花的钱就是这个小伙伴所需监视器数量*每个的费用

COdeforces#417D Cunning Gena(状压DP)

A boy named Gena really wants to get to the "Russian Code Cup" finals, or at least get a t-shirt. But the offered problems are too complex, so he made an arrangement with his n friends that they will solve the problems for him. The participants

CF417D--- Cunning Gena(排序+状压dp)

A boy named Gena really wants to get to the "Russian Code Cup" finals, or at least get a t-shirt. But the offered problems are too complex, so he made an arrangement with his n friends that they will solve the problems for him. The participants

Easy 2048 Again - ZOJ 3802 像缩进dp

Easy 2048 Again Time Limit: 2 Seconds      Memory Limit: 65536 KB Dark_sun knows that on a single-track road (which means once he passed this area, he cannot come back again), there are some underground treasures on each area of the road which has th

hdu 4521 小明系列问题——小明序列(线段树+DP或扩展成经典的LIS)

小明系列问题--小明序列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 1553    Accepted Submission(s): 457 Problem Description 大家都知道小明最喜欢研究跟序列有关的问题了,但是也就由于这样,小明差点儿已经玩遍各种序列问题了.可怜的小明苦苦地在各大站点上寻找着新的序列问题,但是找来

算法复习——求最长不下降序列长度(dp算法)

题目: 题目背景 161114-练习-DAY1-AHSDFZ T2 题目描述 有 N 辆列车,标记为 1,2,3,-,N.它们按照一定的次序进站,站台共有 K 个轨道,轨道遵从先进先出的原则.列车进入站台内的轨道后可以等待任意时间后出站,且所有列车不可后退.现在要使出站的顺序变为 N,N-1,N-2,-,1,询问 K 的最小值是多少. 例如上图中进站的顺序为 1,3,2,4,8,6,9,5,7,则出站的顺序变为 9,8,7,6,5,4,3,2,1. 输入格式 输入共 2 行.第 1 行包含 1 

CF 1150 D Three Religions——序列自动机优化DP

题目:http://codeforces.com/contest/1150/problem/D 老是想着枚举当前在给定字符串的哪个位置,以此来转移. 所以想对三个串分别建 trie 树,然后求出三个trie树上各选一个点的答案.那么从“在三个trie树的根,在给定字符串的0位置”开始扩展. 当然 TLE 了. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #