分组背包。
1 #include <stdio.h> 2 #include <string.h> 3 4 #define mymax(a, b) (a>b) ? a:b 5 6 typedef struct { 7 int p, v; 8 } shoe_st; 9 10 shoe_st shoes[15][105]; 11 int dp[15][10005]; 12 int len[15]; 13 14 int main() { 15 int n, m, k, g, p, v; 16 int i, j, flg, t; 17 18 while (scanf("%d%d%d",&n,&m,&k) != EOF) { 19 memset(len, 0, sizeof(len)); 20 for (i=1; i<=n; ++i) { 21 scanf("%d%d%d", &g, &p, &v); 22 len[g]++; 23 shoes[g][len[g]].p = p; 24 shoes[g][len[g]].v = v; 25 } 26 memset(dp, -1, sizeof(dp)); 27 dp[0][0] = 0; 28 flg = 1; 29 for (i=1; i<=k; ++i) { 30 if (len[i] == 0) { 31 flg = 0; 32 break; 33 } 34 for (t=1; t<=len[i]; ++t) { 35 for (j=m; j>=shoes[i][t].p; --j) { 36 if (dp[i][j-shoes[i][t].p] != -1) { 37 dp[i][j] = mymax(dp[i][j], dp[i][j-shoes[i][t].p]+shoes[i][t].v); 38 } 39 if (dp[i-1][j-shoes[i][t].p] != -1) { 40 dp[i][j] = mymax(dp[i][j], dp[i-1][j-shoes[i][t].p]+shoes[i][t].v); 41 } 42 } 43 } 44 } 45 if (flg) { 46 t = -1; 47 for (j=0; j<=m; ++j) { 48 if (dp[k][j] > t) 49 t = dp[k][j]; 50 } 51 if (t > -1) 52 printf("%d ", t); 53 else 54 printf("Impossible "); 55 } else { 56 printf("Impossible "); 57 } 58 } 59 60 return 0; 61 }