Advertisement
Guest User

Untitled

a guest
Sep 9th, 2017
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <cstdio>
  2.  
  3. typedef unsigned int ui_t;
  4. const ui_t maxlen = 128;
  5. struct object {
  6.     ui_t weight, value;
  7. } objects[maxlen];
  8. ui_t objnr, sacwg, cost[maxlen][maxlen];
  9.  
  10. ui_t maxcost(ui_t a, ui_t b)
  11. {
  12.     return a > b ? a : b;
  13. }
  14.  
  15. void process()
  16. {
  17.     for (ui_t i = 1; i <= objnr; i++) {
  18.         for (ui_t j = 1; j <= sacwg; j++) {
  19.             if (objects[i].weight > j) {
  20.                 cost[i][j] = cost[i - 1][j];
  21.             } else {
  22.                 cost[i][j] = maxcost(cost[i - 1][j], cost[i - 1][j - objects[i].weight] + objects[i].value);
  23.             }
  24.         }
  25.     }
  26. }
  27.  
  28. int main()
  29. {
  30.     freopen("dp_rucsac_1_0.in", "r", stdin);
  31.     freopen("dp_rucsac_1_0.out", "w", stdout);
  32.     scanf("%u %u", &sacwg, &objnr);
  33.     for (ui_t i = 1; i <= objnr; i++) {
  34.         scanf("%u %u", &(objects[i].weight), &(objects[i].value));
  35.     }
  36.     process();
  37.     printf("%u", cost[objnr][sacwg]);
  38.     fclose(stdin);
  39.     fclose(stdout);
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement