1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
int main()
{
int n, m;
scanf("%d %d", &n, &m);
// read
int dp[1 << n];
fill(dp, dp + (1 << n), INT_MAX);
bool ok[1 << n] = {false};
dp[0] = 0;
ok[0] = true;
for (int i = 0; i < m; i++) {
int a, b;
scanf("%d %d", &a, &b);
int mask = 0;
for (int j = 0; j < b; j++) {
int num;
scanf("%d", &num);
num--;
mask |= (1 << num);
}
// printf("mask %d\n", mask);
dp[mask] = min(dp[mask], a); // for each combination, use the min val one
ok[mask] = true;
}
// solve
int ans[1 << n];
fill(ans, ans + (1 << n), INT_MAX);
for (int i = 0; i < (1 << n); i++) {
if (ok[i] == true) {
// printf("ok %d\n", i);
ans[i] = min(ans[i], dp[i]);
for (int j = 0; j < (1 << n); j++) {
if (ans[j] != INT_MAX) {
int target = (i | j);
// printf("%d %d %d %d\n", i, j, dp[i], ans[j]);
ans[target] = min(ans[target], dp[i] + ans[j]);
}
}
}
}
printf("%d\n", ans[(1 << n) - 1] == INT_MAX ? -1 : ans[(1 << n) - 1]);
return 0;
}
|