user-0 提问: 类似01背包问题，求最优解决方案

回复:
user: user-1
content: 贴个动规的，如果想求所有最优解用回溯也可以啊  #include <stdio.h>                                                               #include <stdlib.h>                                                                                                                                               int knapsack(int num, int capacity, int *weight_arr, int *value_arr, int *is_picked) {                                                                                    int i = 0;                                                                       int j = 0;                                                                       int **array = (int **)calloc(num + 1, sizeof(int *));                            int **prev_capacity = (int **)calloc(num + 1, sizeof(int *));                    i = 0;                                                                           for (; i < num + 1; ++i) {                                                           array[i] = (int *)calloc(capacity + 1, sizeof(int));                             prev_capacity[i] = (int *)calloc(capacity + 1, sizeof(int));                 }                                                                                                                                                                 i = 1;                                                                           for (; i < num + 1; ++i) {                                                           j = 1;                                                                           for (; j < capacity + 1; ++j) {                                                      if (weight_arr[i - 1] > j) {                                                         array[i][j] = array[i - 1][j];                                                   prev_capacity[i][j] = j;                                                     } else {                                                                             if (array[i - 1][j] > array[i - 1][j - weight_arr[i - 1]] + value_arr[i - 1]) {                     array[i][j] = array[i - 1][j];                                                   prev_capacity[i][j] = j;                                                     } else {                                                                             array[i][j] = array[i - 1][j - weight_arr[i - 1]] + value_arr[i - 1];                     prev_capacity[i][j] = j - weight_arr[i - 1];                                 }                                                                            }                                                                            }                                                                            }                                                                                                                                                                 i = num;                                                                         j = capacity;                                                                    for (; i > 0; --i) {                                                                 if (prev_capacity[i][j] != j) {                                                      is_picked[i - 1] = 1;                                                            j = prev_capacity[i][j];                                                     }                                                                            }                                                                                                                                                                 int ret = array[num][capacity];                                                  i = 0;                                                                           for (; i < num + 1; ++i) {                                                           free(array[i]);                                                                  free(prev_capacity[i]);                                                      }                                                                                free(array);                                                                     free(prev_capacity);                                                             return ret;                                                                  }                                                                                                                                                                 int main()                                                                       {                                                                                    int capacity = 10;                                                               int num = 6;                                                                     int weight_arr[] = {2, 3, 1, 4, 6, 5};                                           int value_arr[] = {5, 6, 5, 1, 19, 7};                                           int *is_picked = (int *)calloc(num, sizeof(int));                                                                                                                 int i = 0;                                                                       printf("<<<<<input<<<<<\n");                                                     for (; i < num; ++i) {                                                               printf("%d:weight=%d value=%d\n", i, weight_arr[i], value_arr[i]);           }                                                                                printf(">>>>>\n");                                                               printf("max weight:%d\n", capacity);                                                                                                                              int ret = knapsack(num, capacity, weight_arr, value_arr, is_picked);             i = 0;                                                                           printf("<<<<<output<<<<<\n");                                                    for (; i < num; ++i) {                                                               if (is_picked[i] == 1) {                                                             printf("%d:weight=%d value=%d\n", i, weight_arr[i], value_arr[i]);           }                                                                            }                                                                                printf(">>>>>\n");                                                               printf("max value:%d\n", ret);                                                                                                                                    free(is_picked);                                                                 return 0;                                                                    }

user: user-2
content: 怎么算最优方案没定义？

user: user-3
content: 就是啊，什么算最优 我们以前将01背包时用的就是动态规划算法