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 Copy
Copy
3 3 10 60 2 2 4 70 8 7 9 50 2 3 9
Sample Output 1 Copy
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 Copy
Copy
3 3 10 100 3 1 4 100 1 5 9 100 2 6 5
Sample Output 2 Copy
Copy
-1
Buying all the books is still not enough to make his understanding levels of all the algorithms 1010 or higher.
Sample Input 3 Copy
Copy
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 Copy
Copy
1067
思路:因为数据较小,采用状压二进制暴力枚举对[1,1<<n)的每种情况进行枚举,1代表选择这本书,0代表不选择这本书,如果满足条件更新答案求最小值
code:
#include<iostream>
#include<cstring>
#include<cstdio>
#define INF 0x3f3f3f3f
using namespace std;
const int N=20;
int c[N];
int n,m,x;
int a[N][N];
int ans=INF;
int main()
{
cin>>n>>m>>x;
for(int i=1;i<=n;i++){
cin>>c[i];
for(int j=1;j<=m;j++){
cin>>a[i][j];
}
}
for(int i=1;i<(1<<n);i++){
int sum=0;//记录每种状态所花的钱
int res[N];//记录每种算法掌握程度
memset(res,0,sizeof(res));
for(int j=0;j<n;j++){
if(i&(1<<j)){//如果选择j+1本书
for(int k=1;k<=m;k++){
res[k]+=a[j+1][k];
}
sum+=c[j+1];
}
}
bool flag=0;
for(int j=1;j<=m;j++){
if(res[j]<x){
flag=1;
break;
}
}
if(!flag) ans=min(sum,ans);
}
if(ans==INF) puts("-1");
else cout<<ans<<endl;
return 0;
}