C - Skill Up
Time Limit: 2 sec / Memory Limit: 1024 MB
Score : 300300 points
Problem
Takahashi, who is a novice in competitive programming, wants to learn MM algorithms. Initially, his understanding level of each of the MM algorithms is 00.
Takahashi is visiting a bookstore, where he finds NN books on algorithms. The ii-th book (1≤i≤N1≤i≤N) is sold for CiCi yen (the currency of Japan). If he buys and reads it, his understanding level of the jj-th algorithm will increase by Ai,jAi,j for each jj (1≤j≤M1≤j≤M). There is no other way to increase the understanding levels of the algorithms.
Takahashi’s objective is to make his understanding levels of all the MM algorithms XX or higher. Determine whether this objective is achievable. If it is achievable, find the minimum amount of money needed to achieve it.
Constraints
All values in input are integers.
1≤N,M≤121≤N,M≤12
1≤X≤1051≤X≤105
1≤Ci≤1051≤Ci≤105
0≤Ai,j≤1050≤Ai,j≤105
Input
Input is given from Standard Input in the following format:
NN MM XX
C1C1 A1,1A1,1 A1,2A1,2 ⋯⋯ A1,MA1,M
C2C2 A2,1A2,1 A2,2A2,2 ⋯⋯ A2,MA2,M
⋮⋮
CNCN AN,1AN,1 AN,2AN,2 ⋯⋯ AN,MAN,M
Output
If the objective is not achievable, print -1; otherwise, print the minimum amount of money needed to achieve it.
Sample Input 1
3 3 10
60 2 2 4
70 8 7 9
50 2 3 9
Sample Output 1 Copy
120
Buying the second and third books makes his understanding levels of all the algorithms 1010 or higher, at the minimum cost possible.
Sample Input 2
3 3 10
100 3 1 4
100 1 5 9
100 2 6 5
Sample Output 2
-1
Buying all the books is still not enough to make his understanding levels of all the algorithms 1010 or higher.
Sample Input 3
8 5 22
100 3 7 5 3 1
164 4 5 2 7 8
334 7 2 7 2 9
234 4 7 2 8 2
541 5 4 3 3 6
235 4 8 6 9 7
394 3 6 1 6 2
872 8 4 3 7 2
Sample Output 3
1067
idea
二进制枚举,因为数据较小,采用状压二进制暴力枚举对[0,1<<n)的每种情况进行枚举,1代表选择这本书,0代表不选择这本书,如果满足条件更新答案求最小值。
example
假如有五本书,全买记作 11111 ,全不买记作 00000 。
so
01010 代表买第2本、第4本
11000 代表买第4本、第5本
Accepted Code
#include <iostream>
#include <cstring>
using namespace std;
int N,M,X,jishu=0,sum=0,flag=0,cost=0x3f3f3f3f,qian;
int b[20];
struct jgt{
int money;
int score[20];
}a[20];
int main()
{
cin>>N>>M>>X;
for(int i=0;i<N;i++)
{
cin>>a[i].money;
for(int j=0;j<M;j++)
{
cin>>a[i].score[j];
}
}
for(int i=0;i<(1<<N);i++) //二进制枚举从0000...(n个0)到1111...(n个1)的所有情况
{
qian=0,jishu=0;
memset(b,0,sizeof(b));
for(int k=0;k<N;k++) //此循环是为了里面的if计位数
{
if(i&(1<<k)) //i的第k位为1则返回1,否则返回0,对每一种情况中二进制位为1的记录上
{
qian+=a[k].money;
for(int j=0;j<M;j++) b[j]+=a[k].score[j];
}
}
for(int j=0;j<M;j++) if(b[j]>=X) jishu++;
if(jishu==M) cost=min(qian,cost);
}
if(cost==0x3f3f3f3f) printf("-1\n");
else printf("%d\n",cost);
return 0;
}