• <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 //寫出來之后,嘗試把每一個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     //打印背包所能容納的最大價值
             50     cout << f[num][capacity] << endl;
             51 
             52     //打印產(chǎn)生最大價值的背包中物品的編號
             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 閱讀(328) 評論(1)  編輯 收藏 引用 所屬分類: C/C++

            評論

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

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

            <2011年5月>
            24252627282930
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            導(dǎo)航

            統(tǒng)計

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            色综合色天天久久婷婷基地 | 国产精品久久久久乳精品爆| 久久久久AV综合网成人| 国产精品九九九久久九九 | 日日狠狠久久偷偷色综合0| 性欧美大战久久久久久久| 国色天香久久久久久久小说| 国产精品18久久久久久vr| 久久AⅤ人妻少妇嫩草影院| 精品人妻伦九区久久AAA片69| 97久久香蕉国产线看观看| 性做久久久久久免费观看| 精品熟女少妇av免费久久| 国产免费久久精品99re丫y| 久久精品这里热有精品| 亚洲中文字幕无码久久综合网| 久久免费线看线看| 久久av无码专区亚洲av桃花岛| 久久黄视频| 国产精品无码久久四虎| 久久久国产精品亚洲一区| 色综合久久夜色精品国产| 丁香五月综合久久激情| 奇米影视7777久久精品| 久久久中文字幕日本| 日本久久久久久中文字幕| 久久99精品久久久久久久不卡 | 久久se精品一区二区影院| 久久久久久久亚洲Av无码| 亚洲精品乱码久久久久久久久久久久 | 亚洲午夜福利精品久久| 一本久久a久久精品综合夜夜| 久久久久亚洲AV无码麻豆| 久久亚洲精品中文字幕| 久久精品国产2020| 要久久爱在线免费观看| 亚洲国产成人精品久久久国产成人一区二区三区综 | 狠狠色丁香久久婷婷综合_中 | 国产精品一久久香蕉国产线看| 亚洲人成网亚洲欧洲无码久久| 18禁黄久久久AAA片|