DP_knapsack
生活随笔
收集整理的這篇文章主要介紹了
DP_knapsack
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
動(dòng)態(tài)規(guī)劃法解0-1背包問(wèn)題
問(wèn)題描述:
有n個(gè)背包,重量依次為w1,w2, ... ,wn, 價(jià)值依次是v1,v2, ... ,vn, 現(xiàn)在有一個(gè)大背包,其容量是capacity,往其中裝小背包,要求得到的總價(jià)值最大,如何裝?
用value[i, w]表示有i個(gè)背包且總重量最大是w時(shí)的價(jià)值,那么當(dāng)考慮第i個(gè)背包時(shí),有兩種情況
1. 將此背包裝入大包,價(jià)值是 v[i] + value[i - 1, w- wi]
2. 此背包不裝入打包,價(jià)值是 value[i - 1, w]
取兩者中的最大值即可
代碼如下:
?
Code?1?class?Knapsack
?2?{
?3?????///?<summary>
?4?????///?0-1?Knapsack?problem?solution
?5?????///?</summary>
?6?????///?<param?name="w">the?weight?array</param>
?7?????///?<param?name="v">the?value?array</param>
?8?????///?<param?name="capacity">the?total?weight?that?allowed</param>
?9?????///?Note:?w?and?v?must?have?the?same?length
10?????///?let?v[i,w]?represents?for?the?max?value?of?the?first?i?knapsack?with?the?capacity?of?w
11?????public?void?Knapsack01(int[]?w,?int[]?v,?int?capacity)
12?????{
13?????????int?r?=?v.Length?+?1;
14?????????int?c?=?r;
15?
16?????????int[,]?vt?=?new?int[r,?c];?//?value?table
17?????????bool[,]?st?=?new?bool[r,?c];?//?selected?table
18?????????bool[]?res?=?new?bool[w.Length];
19?????????
20?????????//?initialization
21?????????for?(int?i?=?0;?i?<?r;?i++)
22?????????{
23?????????????vt[i,?0]?=?0;?//?the?value?with?a?capacity?of?0?is?0
24?????????????vt[0,i]?=?0;?//?the?value?of?0?knapsack?is?0
25?????????}
26?
27?????????//?note?the?index?i?and?j?start?from?1
28?????????//?so?you?can?see?w[i?-?1]?below?in?order?to?start?from?0?for?array?w
29?????????for?(int?i?=?1;?i?<?r;?i++)
30?????????{
31?????????????for?(int?j?=?1;?j?<=?capacity;?j++)
32?????????????{
33?????????????????int?unselValue?=?vt[i?-?1,?j];?//?unselected?current?knapsack
34?
35?????????????????if?(j?>=?w[i?-?1])
36?????????????????{
37?????????????????????int?selValue?=?v[i?-?1]?+?vt[i?-?1,?j?-?w[i?-?1]];
38?????????????????????if?(selValue?>=?unselValue)
39?????????????????????{
40?????????????????????????vt[i,?j]?=?selValue;
41?????????????????????????st[i,?j]?=?true;
42?????????????????????????if(selValue?>?unselValue)
43?????????????????????????????res[i?-?1]?=?true;
44?????????????????????????continue;
45?????????????????????}
46?????????????????}
47?
48?????????????????vt[i,?j]?=?unselValue;
49?????????????????st[i,?j]?=?false;
50?????????????}
51?????????}
52?
53?????????//?Output?the?element?in?array?w?being?selected
54?????????//for?(int?i?=?r?-?1;?i?>=?0&&?capacity?>=?0;?i--)
55?????????//{
56?????????//????if?(st[i,?capacity]?==?true)
57?????????//????{
58?????????//????????Console.WriteLine(w[i?-?1]);
59?????????//????????capacity?-=?w[i?-?1];
60?????????//????}
61?????????//}
62?
63?????????for(int?i?=?res.Length?-?1;?i?>=?0;?i--)
64?????????????if(res[i]?==?true)
65?????????????????Console.WriteLine(i);
66?????}
67?}
68?
?
轉(zhuǎn)載于:https://www.cnblogs.com/graphics/archive/2009/06/23/1509618.html
總結(jié)
以上是生活随笔為你收集整理的DP_knapsack的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: xp下安装sqlserver 2000的
- 下一篇: 郑州人民医院血细胞分析需要多少钱