CF417D——Cunning Gena(狀態壓縮DP)

D. Cunning Gena
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 nm and b (1 ≤ n ≤ 1001 ≤ m ≤ 201 ≤ 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 xiki and mi (1 ≤ xi ≤ 1091 ≤ ki ≤ 1091 ≤ 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 thei-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
題意:

有個笨蛋自己不會做題,但是想要獲得比賽的勝利,於是就想請外援幫助。但是外援不肯免費幫他,所以要好處。

笨蛋有n個朋友,第i個朋友的收費是Xi,並且要求笨蛋至少有Ki個顯示器,每個顯示器價格b,並且告訴你這個朋友會做的題目。

笨蛋要想做出所有的題目至少要花多少錢,不能完成輸出-1。

分析:

這題給的m比較小隻有20。所以可以用狀態壓縮,用一個int表示一個狀態。

如果將顯示器也加到狀態裏面肯定是存不下的,而且也沒有必要。

所以dp[i]表示,對於i狀態來說所需的最小費用。(沒有包含顯示器成本)

首先按照要求顯示器的數量,將所有的朋友排序,然後按照這個順序DP。


#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
using namespace std;

typedef long long LL;
const LL INF = 4e18;

struct node
{
    int x,k,s;
}f[105];
LL dp[ (1<<20)+10 ];

int cmp(node a,node b)
{
    return a.k<b.k;
}
int main()
{
    int t,n,m,b,x;
    memset(dp,-1,sizeof(dp));
    scanf("%d%d%d",&n,&m,&b);
    for(int i=0;i<n;i++)
    {
        scanf("%d%d",&f[i].x,&f[i].k);
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d",&x);
            f[i].s|=1<<(x-1);
        }
    }
    sort(f,f+n,cmp);

    int limit=(1<<m)-1;
    LL ans=INF;
    dp[0]=0;
    for(int i=0;i<n;i++)
    {
        for(int st=0;st<=limit;st++)
        {
            if(dp[st]==-1) continue;

            t=st|f[i].s;
            if(dp[t]!=-1)
                dp[t]=min(dp[t],dp[st]+f[i].x);
            else
                dp[t]=dp[st]+f[i].x;
        }

        if(dp[limit]!=-1)
            ans=min(ans,dp[limit]+1LL*f[i].k*b);
    }
    printf("%I64d\n",ans==INF ? -1:ans);

	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章