标签:
转自:http://blog.csdn.net/tjyyyangyi/article/details/7929665
0-1背包问题
参考:
http://blog.csdn.net/liwenjia1981/article/details/5725579
http://blog.csdn.net/dapengbusi/article/details/7463968
动态规划解法
借个图 助于理解
从背包容量为0开始,1号物品先试,0,1,2,的容量都不能放.所以置0,背包容量为3则里面放4.这样,这一排背包容量为 4,5,6,....10的时候,最佳方案都是放4.假如1号物品放入背包.则再看2号物品.当背包容量为3的时候,最佳方案还是上一排的最价方案c为 4.而背包容量为5的时候,则最佳方案为自己的重量5.背包容量为7的时候,很显然是5加上一个值了。加谁??很显然是7-4=3的时候.上一排 c3的最佳方案是4.所以。总的最佳方案是5+4为9.这样.一排一排推下去。最右下放的数据就是最大的价值了。(注意第3排的背包容量为7的时候,最佳 方案不是本身的6.而是上一排的9.说明这时候3号物品没有被选.选的是1,2号物品.所以得9.)
- #include<stdio.h>
-
- int f[10][100];
- void package0_1(int *w,int *v,int n,int c)
- {
- int i,j;
-
- for(i=1;i<=n;i++)
- f[i][0] = 0;
- for(j=1;j<=c;j++)
- f[0][j] = 0;
-
- for(i=1;i<=n;i++)
- {
- for(j=1;j<=c;j++)
- {
-
- if(w[i] <= j && f[i-1][j-w[i]] + v[i] > f[i-1][j])
- {
- f[i][j] = f[i-1][j-w[i]] + v[i];
- }else
- f[i][j] = f[i-1][j];
- }
- }
- printf("最大价值: %d \n",f[n][c]);
- }
-
- void getResult(int n,int c,int *res,int *v,int *w)
- {
- int i,j;
- j = c;
- for(i=n;i>=1;i--)
- {
- if(f[i][j] != f[i-1][j])
- {
- res[i] = 1;
- j = j - w[i];
- }
- }
- }
-
- void main()
- {
- int w[6] = {0,2,2,6,5,4};
- int v[6] = {0,6,3,5,4,6};
- int res[5] = {0,0,0,0,0};
- int n = 5;
- int c = 10;
- int i,j;
- package0_1(w,v,n,c);
- for(i=0;i<=n;i++)
- {
- for(j=0;j<=c;j++)
- printf("%2d ",f[i][j]);
- printf("\n");
- }
- getResult(n,c,res,v,w);
- printf("放入背包的物品为: \n");
- for(i=1;i<=n;i++)
- if(res[i] == 1)
- printf("%d ",i);
- }
0-1背包的递归解法
- #include<stdio.h>
-
- int maxNum[6];
- int maxValue=0;
- int w[6] = {0,2,2,6,5,4};
- int v[6] = {0,6,3,5,4,6};
- int num = 5;
- int cap = 10;
-
- void package01(int *flag,int n,int c,int nowValue)
- {
- int i;
- if(n == 0 || c == 0)
- {
- if(nowValue > maxValue)
- {
- for(i=0;i<6;i++)
- maxNum[i] = flag[i];
- maxValue = nowValue;
- }
- return;
- }
-
- if(c >= w[n])
- {
- flag[n] = 1;
- package01(flag, n-1, c-w[n], nowValue+v[n]);
- }
- flag[n] = 0;
- package01(flag, n-1, c, nowValue);
- }
-
- void main()
- {
- int flag[6] = {0,0,0,0,0,0};
- int i;
- package01(flag,num,cap,0);
- for(i=1;i<=num;i++)
- maxNum[i] == 1 ? printf("第%d号货物装了包中 \n",i) : 0;
- printf("最大价值为:%d \n",maxValue);
- }
完全背包问题
与0-1背包问题区别在每个物品有无限多个。
- #include<stdio.h>
-
- int f[10][100];
- void package0_1(int *w,int *v,int n,int c)
- {
- int i,j,k;
-
- for(i=1;i<=n;i++)
- f[i][0] = 0;
- for(j=1;j<=c;j++)
- f[0][j] = 0;
-
- for(i=1;i<=n;i++)
- {
- for(j=1;j<=c;j++)
- {
-
- k = j/w[i];
- if( k>0 && (f[i-1][j- k * w[i]] + k * v[i] > f[i-1][j]))
- {
- f[i][j] = f[i-1][j- k * w[i]] + k * v[i] ;
- }else
- f[i][j] = f[i-1][j];
- }
- }
- printf("最大价值: %d \n",f[n][c]);
- }
-
- void getResult(int n,int c,int *res,int *v,int *w)
- {
- int i,j;
- j = c;
- for(i=n;i>=1;i--)
- {
- while(f[i][j] > f[i-1][j])
- {
- res[i] ++;
- j = j - w[i];
- }
- }
- }
-
- void main()
- {
- int w[6] = {0,4,6,6,3,6};
- int v[6] = {0,1,1,1,2,1};
- int res[5] = {0,0,0,0,0};
- int n = 5;
- int c = 10;
- int i,j;
- package0_1(w,v,n,c);
- for(i=0;i<=n;i++)
- {
- for(j=0;j<=c;j++)
- printf("%2d ",f[i][j]);
- printf("\n");
- }
- getResult(n,c,res,v,w);
- printf("放入背包的物品为: \n");
- for(i=1;i<=n;i++)
- if(res[i] >= 1)
- printf("放入了第%d号物品%d个\n ",i,res[i]);
- }
部分背包问题
与0-1背包的区别:装入的可以不是整个装入,理解为“装沙”。其余要求一样。
用贪心法求解
- #include<stdio.h>
-
- void package_part(int *w,int *v,double *p,int n,int c,int *flag)
- {
- int i,j,temp;
- double tempD,totalValue = 0.0;
-
-
- for(i=0;i<n;i++)
- {
- p[i] = (double)v[i] / (double)w[i];
- flag[i] = i;
- }
-
- for(i=0;i<n;i++)
- {
- for(j=n-1;j>i;j--)
- {
- if(p[j] > p[j-1])
- {
- temp = flag[j];
- flag[j] = flag[j-1];
- flag[j-1] = temp;
-
- tempD = p[j];
- p[j] = p[j-1];
- p[j-1] = tempD;
- }
- }
- }
-
- for(i=0;i<n;i++)
- {
- if(c >= w[flag[i]])
- {
- totalValue += v[flag[i]];
- c -= w[flag[i]];
- printf("第%d号物品整个放入\n",flag[i]);
- }else
- {
- totalValue += p[flag[i]] * (double)c / (double) w[flag[i]];
- printf("第%d号物品放入了%f\n",flag[i],(double)c / (double) w[flag[i]]);
- break;
- }
- }
- printf("总价值为:%f",totalValue);
- }
-
- void main()
- {
- int w[5] = {4,6,6,3,6};
- int v[5] = {1,1,1,2,1};
- double p[5] = {0,0,0,0,0};
- int flag[5];
- int n = 5;
- int c = 10;
- package_part(w,v,p,n,c,flag);
- }
背包问题-C语言实现
标签:
原文地址:http://www.cnblogs.com/stephen-init/p/4313143.html