forked from Badhansen/UVa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11450 - Wedding shopping(bottom-up).cpp
More file actions
70 lines (57 loc) · 1.64 KB
/
11450 - Wedding shopping(bottom-up).cpp
File metadata and controls
70 lines (57 loc) · 1.64 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
11450 - Wedding shopping is an interesting dp problem
The main idea is to build a table of reachable in bottom up manner and
calculate the base case of first row. if i bought the
first item then check the other item can be bought.
In this manner i reached the last step so there is a solution if not
"no solution" .......
Thanks to Steven & Felix ....
*/
#include<bits/stdc++.h>
using namespace std;
int N, M, C;
int price[21][21];
int reachable[21][201];
int main ()
{
int i, j, g, k, money;
scanf("%d", &N);
while (N--){
scanf("%d%d", &M, &C);
for (i=0; i<C; i++){
scanf("%d", &price[i][0]);
for (j=1; j<=price[i][0]; j++){
scanf("%d", &price[i][j]);
}
}
memset (reachable, false, sizeof reachable);
for (g=1; g<=price[0][0]; g++){
if (M-price[0][g]>=0){
reachable[0][M-price[0][g]]=true;
}
}
for (g=1; g<C; g++){
for (money=0; money<M; money++){
if (reachable[g-1][money]){
for (k=1; k<=price[g][0]; k++){
if (money-price[g][k]>=0){
reachable[g][money-price[g][k]]=true;
}
}
}
}
}
int flag=0;
for (money=0; money<=M; money++){
if (reachable[C-1][money]==true){
flag=1;
printf ("%d\n", M-money);
break;
}
}
if (flag==0){
printf("no solution\n");
}
}
return 0;
}