C语言 0-1背包 回溯法,01背包问题之回溯法

解01背包问题有很多种方法,就我知道的就有动态规划,回溯法,分支界限法这几种,下面就列出我的回溯法解法,以供参考

int capacity; //背包容量

int n;        //物品数

int weight[0..n];  //物品重量数组

int price[0..n];  //物品价值数组

int cur_weight;   //当前重量

int cur_price;    //当前价值

int best_price;   //当前最优值

int best_solution[0..n];  //当前最优解

int cur_solution[0..n];   //当前解

//估计 装入第i个物品后能得到的最大价值, 从而做为剪枝的依据

int upper_bound(int i)

{

//计算上界

int remain_capacity = capacity - cur_weight;

int  b = remain_capacity;

//按单位重量的价值  递减序 装入物品

while(i<=n && w[i]<=remain_capacity)

{

remain_capacity-=w[i];

b+=p[i];

i++;

}

//装满背包

if( i<=n )

b+=p[i]/w[i]*remain_capacity;  //准确的说这是一个上界,不是上确界

return b;

}

void dfs(int i)

{

//结束条件

if(i>n)

{

if(best_price >cur_price)       //到此为止了,有用往后找了

{

for(int j=1;j<=n;j++)

best_solution[j] =x[j];

}

return ;

}

//搜索左子树,要当前结点

if(cur_weight+weght[i]< = capacity)

{

cur_solution[i] = 1;

cur_weight += weight[i];

cur_price  += price[i];

dfs(i+1);           cur_weight   -= weight[i];

cur_price  -= price[i];

}

//搜索右子树,不要当前结点,即数组中下一个结点

if(upper_bound(u+1)>best_price)

{            cur_solution[i]=0;

dfs(i+1);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用回溯法解决01背包问题C语言代码示例: ```c #include <stdio.h> #define MAX_N 100 // 最大物品数量 #define MAX_WEIGHT 1000 // 背包的最大承重 int weight[MAX_N]; // 物品的重量数组 int value[MAX_N]; // 物品的价值数组 int max_value; // 最大总价值 int best_choice[MAX_N]; // 最佳选择情况数组 int current_weight; // 当前背包内物品的总重量 int current_value; // 当前背包内物品的总价值 void backtrack(int n, int index) { if (index == n) { // 已经遍历完所有物品 if (current_value > max_value) { // 更新最大总价值和最佳选择情况 max_value = current_value; for (int i = 0; i < n; i++) { best_choice[i] = (current_weight >> i) & 1; // 1表示选择该物品,0表示不选择 } } return; } if (current_weight + weight[index] <= MAX_WEIGHT) { // 选择当前物品 current_weight += weight[index]; current_value += value[index]; backtrack(n, index + 1); current_weight -= weight[index]; current_value -= value[index]; } backtrack(n, index + 1); // 不选择当前物品 } int main() { int n; // 物品数量 printf("请输入物品数量:"); scanf("%d", &n); printf("请依次输入每个物品的重量和价值:\n"); for (int i = 0; i < n; i++) { scanf("%d %d", &weight[i], &value[i]); } max_value = 0; current_weight = 0; current_value = 0; backtrack(n, 0); printf("最大总价值为:%d\n", max_value); printf("最佳选择情况为:"); for (int i = 0; i < n; i++) { printf("%d ", best_choice[i]); } printf("\n"); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值