• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            01背包問題

              1 #include <iostream>
              2 #include <vector>
              3 #include <algorithm>
              4 #define max(a,b) ((a)>(b))?(a):(b)
              5 using namespace std;
              6 //寫出來之后,嘗試把每一個(gè)for循環(huán)用for_each來替換。或者將公用的for流程用函數(shù)替代
              7 struct PrintResult 
              8 {
              9     void operator()(int i)
             10     {
             11         cout << i << " ";
             12     }
             13 }printResult;
             14 
             15 struct PrintVecResult 
             16 {
             17     void operator()(vector<int> vec)
             18     {
             19         for_each(vec.begin(), vec.end(), printResult);
             20         cout << endl;
             21     }
             22 }printVecResult;
             23 
             24 int knapsack(vector<int> &vecWeight, vector<int> &vecValue, int capacity)
             25 {
             26     int num = vecWeight.size();
             27     vector<vector<int> > f(num, vector<int>(capacity, 0));
             28     vector<int> result(num, 0);
             29 
             30     int j = 0;
             31     int i = 0;
             32     for (i = 1; i <= num; ++i)
             33     {
             34         for (j = 1; j <= capacity; ++j)
             35         {
             36             if (j >= vecWeight[i])
             37             {
             38                 f[i][j] = max(f[i-1][j], f[i-1][j-vecWeight[i]] + vecValue[i]);
             39             }
             40             else
             41             {
             42                 f[i][j] = f[i-1][j];
             43             }
             44         }
             45     }
             46     //打印f數(shù)組表
             47     for_each(f.begin(), f.end(), printVecResult);
             48     
             49     //打印背包所能容納的最大價(jià)值
             50     cout << f[num][capacity] << endl;
             51 
             52     //打印產(chǎn)生最大價(jià)值的背包中物品的編號(hào)
             53     
             54     for (j = capacity, i = num; i >= 1--i)
             55     {
             56         //result[i] = f[i][j] > f[i-1][j] ? 1 : 0; 
             57         if (f[i][j] > f[i-1][j])
             58         {
             59             result[i] = 1;
             60             j = j - vecWeight[i];
             61         }
             62         else
             63         {
             64             result[i] = 0;
             65         }
             66     }
             67     
             68     for (i = 1; i <= num; ++i)
             69     {
             70         if (1 == result[i])
             71         {
             72             cout << i << " ";
             73         }
             74     }
             75     return  f[num][capacity] ;
             76 }
             77 
             78 
             79 int main()
             80 {
             81     int num = 0;
             82     int capacity = 0;
             83     cin >> num;
             84     cin >> capacity;
             85 
             86     vector<int> weight;
             87     vector<int> value;
             88     weight.push_back(0);
             89     value.push_back(0);
             90 
             91     for (int i = 1; i <= num; ++i)
             92     {
             93         int tempWeight = 0;
             94         int tempValue = 0;
             95         cin >> tempWeight >> tempValue;
             96         weight.push_back(tempWeight);
             97         value.push_back(tempValue);
             98     }
             99 
            100     knapsack(weight, value, capacity);
            101 
            102     return 0;
            103 }

            posted on 2011-06-07 00:55 MrRightLeft 閱讀(321) 評(píng)論(1)  編輯 收藏 引用 所屬分類: C/C++

            評(píng)論

            # re: 01背包問題 2012-07-10 10:28 SunRise_at

            其實(shí)有個(gè)背包九講,講各種背包問題。。  回復(fù)  更多評(píng)論   

            <2012年7月>
            24252627282930
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            導(dǎo)航

            統(tǒng)計(jì)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            大蕉久久伊人中文字幕| 成人妇女免费播放久久久| 久久99精品国产麻豆| 久久亚洲国产最新网站| 热久久国产欧美一区二区精品| 国产一级持黄大片99久久| 久久精品中文无码资源站| 天天躁日日躁狠狠久久| 亚洲va国产va天堂va久久| 久久综合给合久久狠狠狠97色69| 久久精品中文无码资源站| 精品国产乱码久久久久久人妻| 久久久久av无码免费网| 久久久久久精品免费免费自慰 | 深夜久久AAAAA级毛片免费看| 久久五月精品中文字幕| 亚洲国产成人乱码精品女人久久久不卡 | 日本精品久久久久影院日本| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 国内精品久久久久久久久电影网 | 色88久久久久高潮综合影院| 久久99精品久久久久久hb无码| 国产美女久久精品香蕉69| 久久无码av三级| 亚洲欧美国产日韩综合久久| 久久天天躁狠狠躁夜夜avapp| 亚洲精品无码久久毛片 | 精品久久人人爽天天玩人人妻| 久久天堂AV综合合色蜜桃网| 国产精品天天影视久久综合网| 国产精品永久久久久久久久久| 无码乱码观看精品久久| 亚洲色大成网站WWW久久九九| 久久久久久久99精品免费观看| 欧美精品福利视频一区二区三区久久久精品 | 国产精品99久久久久久人| 欧美精品丝袜久久久中文字幕| 久久受www免费人成_看片中文| 久久香蕉国产线看观看猫咪?v| 久久精品国产亚洲Aⅴ蜜臀色欲| 91精品日韩人妻无码久久不卡|