0-1背包问题 c语言_动态编程在C中的0-1背包问题

0-1背包问题 c语言

Here you will learn about 0-1 knapsack problem in C.

在这里,您将学习C语言中的0-1背包问题。

We are given n items with some weights and corresponding values and a knapsack of capacity W. The items should be placed in the knapsack in such a way that the total value is maximum and total weight should be less than knapsack capacity.

我们给了n个具有一定权重和相应值的项目以及一个容量为W的背包。这些项目应以总值最大且总重量小于背包容量的方式放置在背包中。

In this problem 0-1 means that we can’t put the items in fraction. Either put the complete item or ignore it. Below is the solution for this problem in C using dynamic programming.

在这个问题0-1中,意味着我们不能将项目分成零。 放入完整的项目或忽略它。 下面是使用动态编程在C中解决此问题的方法。

动态编程在C中的背包问题

动态编程的C背包问题程序 (
Program for Knapsack Problem in C Using Dynamic Programming)

#include<stdio.h>
 
int max(int a, int b) { return (a > b)? a : b; }
 
int knapSack(int W, int wt[], int val[], int n)
{
   int i, w;
   int K[n+1][W+1];
 
   for (i = 0; i <= n; i++)
   {
       for (w = 0; w <= W; w++)
       {
           if (i==0 || w==0)
               K[i][w] = 0;
           else if (wt[i-1] <= w)
                 K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]],  K[i-1][w]);
           else
                 K[i][w] = K[i-1][w];
       }
   }
 
   return K[n][W];
}
 
int main()
{
    int i, n, val[20], wt[20], W;
    
    printf("Enter number of items:");
    scanf("%d", &n);
    
    printf("Enter value and weight of items:\n");
    for(i = 0;i < n; ++i){
    	scanf("%d%d", &val[i], &wt[i]);
    }
 
    printf("Enter size of knapsack:");
    scanf("%d", &W);
    
    printf("%d", knapSack(W, wt, val, n));
    return 0;
}

Output

输出量

Enter number of items:3 Enter value and weight of items: 100 20 50 10 150 30 Enter size of knapsack:50 250

输入物品数量:3 输入物品的重量和重量: 100 20 50 10 150 30 输入背包尺寸:50 250

You can watch below video to learn knapsack problem easily.

您可以观看下面的视频轻松了解背包问题。

Source: http://www.geeksforgeeks.org/dynamic-programming-set-10-0-1-knapsack-problem/

资料来源: http : //www.geeksforgeeks.org/dynamic-programming-set-10-0-1-knapsack-problem/

翻译自: https://www.thecrazyprogrammer.com/2016/10/knapsack-problem-c-using-dynamic-programming.html

0-1背包问题 c语言

利用动态规划原理进行求解 0-1背包问题 已知背包的容量为b,有n种物件,其价格依次为w1,w2,...,wn;其容量依次为v1,v2,...,vn。 现要求在背包允许的容量内,装的物件价值达到最大,其数字模型为: max z=1 x1 + 6 x2 + 18 x3 + 22 x4 + 28 x5 1 x1 + 2 x2 + 5 x3 + 6 x4 + 7 x5 <=11 xi=0,1 i=1,2,3,4,5 S(i,j)=max{S(i-1,j),S(i-1,j-vi)+wi} S(0,j)=0 j>=0 S(i,j)=负无穷 j<0 i=1,w1=1,v1=1 S(1,1)=max{S(0,1),S(0,1-1)+1}=1 S(1,2)=max{S(0,2),S(0,2-1)+1}=1 S(1,3)=...=S(1,11)=1 i=2,w2=6,v2=2 S(2,1)=max{S(1,1),S(1,1-2)+6}=1 S(2,2)=max{S(1,1),S(1,2-2)+6}=6 S(2,3)=max{S(1,3),S(1,3-2)+6}=7 S(2,4)=...=S(2,11)=7 i=3,w3=18,v3=5 S(3,1)=max{S(2,1),S(2,1-5)+18}=1 S(3,2)=max{S(2,2),S(2,2-5)+18}=6 S(3,3)=max{S(2,3),S(2,3-5)+18}=7 S(3,4)=max{S(2,4),S(2,4-5)+18}=7 S(3,5)=max{S(2,5),S(2,5-5)+18}=18 S(3,6)=max{S(2,6),S(2,6-5)+18}=19 S(3,7)=max{S(2,7),S(2,7-5)+18}=24 S(3,8)=max{S(2,8),S(2,8-5)+18}=25 S(3,9)=S(3,10)=...=S(3,11)=25 i=4,w4=22,v4=6 S(4,1)=max{S(3,1),S(3,1-6)+22}=1 S(4,2)=max{S(3,2),S(3,2-6)+22}=6 S(4,3)=max{S(3,3),S(3,3-6)+22}=7 S(4,4)=max{S(3,4),S(3,4-6)+22}=7 S(4,5)=max{S(3,5),S(3,5-6)+22}=18 S(4,6)=max{S(3,6),S(3,6-6)+22}=22 S(4,7)=max{S(3,7),S(3,7-6)+22}=24 S(4,8)=max{S(3,7),S(3,8-6)+22}=38 S(4,9)=max{S(3,7),S(3,9-6)+22}=29 S(4,10)=max{S(3,7),S(3,10-6)+22}=29 S(4,11)=max{S(3,7),S(3,11-6)+22}=40 i=5,w5=28,v5=7 S(5,1)=max{S(4,1),S(4,1-7)+28}=1 S(5,2)=max{S(4,2),S(4,2-7)+28}=6 S(5,3)=max{S(4,3),S(4,3-7)+28}=7 S(5,4)=max{S(4,4),S(4,4-7)+28}=7 S(5,5)=max{S(4,5),S(4,5-7)+28}=18 S(5,6)=max{S(4,6),S(4,6-7)+28}=22 S(5,7)=max{S(4,7),S(4,7-7)+28}=28 S(5,8)=max{S(4,8),S(4,8-7)+28}=29 S(5,9)=max{S(4,9),S(4,9-7)+28}=34 S(5,10)=max{S(4,10),S(4,10-7)+28}=35 S(5,11)=max{S(4,11),S(4,11-7)+28}=40
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值