Algorithm
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
struct fri {
int x, k, msk;
bool operator<(fri f) {
return k < f.k;
}
};
int const N = 101;
int n, m, b, M, all;
long long dp[(1 << 20)];
fri fris[N];
int main() {
scanf("%d %d %d", &n, &m, &b);
M = (1 << m) - 1;
for(int i = 0, m; i < n; ++i) {
scanf("%d %d %d", &fris[i].x, &fris[i].k, &m);
for(int j = 0, tmp; j < m; ++j) {
scanf("%d", &tmp);
--tmp;
fris[i].msk |= (1 << tmp);
}
all |= fris[i].msk;
}
if(__builtin_popcount(all) != m) {
puts("-1");
return 0;
}
sort(fris, fris + n);
for(int i = 0; i < (1 << m); ++i)
dp[i] = 2e18;
dp[0] = 0;
long long res = 2e18;
for(int i = 0; i < n; ++i) {
for(int j = 0; j < (1 << m); ++j)
if(dp[j] < 2e18)
dp[j | fris[i].msk] = min(dp[j | fris[i].msk], dp[j] + fris[i].x);
if(dp[M] < 2e18)
res = min(res, dp[M] + 1ll * b * fris[i].k);
}
if(res == 2e18)
puts("-1");
else
printf("%lld\n", res);
return 0;
}
Copy The Code &
Try With Live Editor
Input
2 2 1
100 1 1
2
100 2 1
1
100 1 1
2
100 2 1
1
Output
202
Demonstration
Codeforces Solution-Cunning Gena-Solution in C, C++, Java, Python