价值和重量调换过来进行完全背包,注意输出格式
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
int S, N;
struct Set {
int cnt, a[15];
bool operator < (const Set &t) const {
if (cnt != t.cnt) return cnt > t.cnt;
for (int i = cnt; i >= 1; i++) {
if (a[i] == t.a[i]) continue;
return a[i] > t.a[i];
}
return false;
}
}s[15];
int dp[1010];
int main() {
while (~scanf("%d", &S) && S) {
scanf("%d", &N);
for (int i = 1; i <= N; i++) {
scanf("%d", &s[i].cnt);
for (int j = 1; j <= s[i].cnt; j++) scanf("%d", &s[i].a[j]);
}
sort(s+1, s+1+N);
int id = 1, ans = -1;
for (int k = 1; k <= N; k++) {
memset(dp, INF, sizeof(dp));
dp[0] = 0;
for (int i = 1; i <= s[k].cnt; i++) {
for (int j = s[k].a[i]; j <= 1005; j++) {
dp[j] = min(dp[j], dp[j-s[k].a[i]]+1);
}
}
int t = 1;
while (dp[t] <= S) t++;
t--;
if (ans <= t) {
id = k;
ans = t;
}
}
printf("max coverage =%4d :", ans);
for (int i = 1; i <= s[id].cnt; i++) printf("%3d", s[id].a[i]);
printf("\n");
}
return 0;
}