<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Change Dir

    先知cd——熱愛生活是一切藝術(shù)的開始

    統(tǒng)計

    留言簿(18)

    積分與排名

    “牛”們的博客

    各個公司技術(shù)

    我的鏈接

    淘寶技術(shù)

    閱讀排行榜

    評論排行榜

    深入理解動態(tài)規(guī)劃的一系列問題(15)

    今天要說的 是0/1背包問題,問題非常經(jīng)典,即:給定一個袋子,容量為c,c∈N,同時有n個球A={a0,a1,…a(n-1)},每個球有一自己的價值v(ai),同時有一定的重量w(ai),目標(biāo)就是盡可能的裝球使得總價值最高,同時重量不能超出袋子的容量c。所以形式化表示這個問題即,對于Σwixi<=c,求最大max z=Σvixi。這個問題可以被看做一個多階段決策問題,對于每個ai,都要做決策xi來判斷是否要裝ai,階段標(biāo)示即n個物體A的下標(biāo),那么動態(tài)規(guī)劃方程如下:f(i,w)= 0 if i=-1 and 0≤w≤c,f(i,w)=-∞ if i=-1 and w<0,f(i,w)=max {xivi+f(i-1,w-xiwi)} if i≥0。目標(biāo)自然是計算f(n-1,c)咯。其實現(xiàn)在理解01背包,典型的思路就是能階段性考慮,遞歸求解。不妨設(shè)c=22,n=3,v={25,24,15},w={18,15,10}。那么結(jié)論就是只選擇第一個球,最終切價值最高~~當(dāng)然這個例子可能特殊了點,但是從公式上推理,我們知道這個遞歸方程是正確的。

    源碼如下:

       1: /*
       2:  * Copyright (C) 2013 changedi
       3:  *
       4:  * Licensed under the Apache License, Version 2.0 (the "License");
       5:  * you may not use this file except in compliance with the License.
       6:  * You may obtain a copy of the License at
       7:  *
       8:  * http://www.apache.org/licenses/LICENSE-2.0
       9:  *
      10:  * Unless required by applicable law or agreed to in writing, software
      11:  * distributed under the License is distributed on an "AS IS" BASIS,
      12:  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      13:  * See the License for the specific language governing permissions and
      14:  * limitations under the License.
      15:  */
      16: package com.jybat.dp;
      17:  
      18: import java.util.HashSet;
      19: import java.util.Set;
      20:  
      21: /**
      22:  * Knapsack01Problem.
      23:  * 
      24:  * @author zunyuan.jy
      25:  * 
      26:  * @since 2014年6月13日
      27:  */
      28: public class KS01 {
      29:  
      30:     private static int knapsackCapacity = 22;
      31:     private static int[] value = { 25, 24, 15 };
      32:     private static int[] weight = { 18, 15, 10 };
      33:     private static int n = value.length; // number of objects n=3
      34:     private static int highestIndex = n - 1; // items are indexed
      35:  
      36:     // from 0 through n-1
      37:  
      38:     private static Set<Integer> calculateDecisionSet(int objInd, int w) {
      39:         Set<Integer> decSet = new HashSet<Integer>();
      40:         decSet.add(new Integer(0)); // decision to not take object
      41:         // is always feasible
      42:         if (w >= weight[objInd]) { // check if there is enough space
      43:             // to take object
      44:             decSet.add(new Integer(1));
      45:         }
      46:         return decSet;
      47:     }
      48:  
      49:     public static double f(int currentObejctIndex, int weightToGive) {
      50:         if (currentObejctIndex == -1)
      51:             return 0.0;
      52:         Set<Integer> decisionSet = calculateDecisionSet(currentObejctIndex,
      53:                 weightToGive);
      54:         double max = 0.0;
      55:         for (int d : decisionSet) {
      56:             double tmp = d
      57:                     * value[currentObejctIndex]
      58:                     + f(currentObejctIndex - 1, weightToGive - d
      59:                             * weight[currentObejctIndex]);
      60:             if (tmp > max)
      61:                 max = tmp;
      62:         }
      63:         return max;
      64:     }
      65:  
      66:     /**
      67:      * @param args
      68:      */
      69:     public static void main(String[] args) {
      70:         System.out.println(KS01.f(2, 22));
      71:     }
      72:  
      73: }

    posted on 2014-06-16 13:02 changedi 閱讀(2486) 評論(6)  編輯  收藏 所屬分類: 算法

    評論

    # re: 深入理解動態(tài)規(guī)劃的一系列問題(15) 2014-06-16 14:25 IT前線

    很想繼續(xù)看完,太難呀

    http://www.itqx.net  回復(fù)  更多評論   

    # re: 深入理解動態(tài)規(guī)劃的一系列問題(15) 2014-06-22 09:58 手賺網(wǎng)-手機(jī)賺錢軟件排行,手機(jī)賺錢平臺,網(wǎng)絡(luò)賺錢http://www.szapk.cn

    輕松賺:http://www.szapk.cn/ruanjianzhongxin/30.html  回復(fù)  更多評論   

    # re: 深入理解動態(tài)規(guī)劃的一系列問題(15) 2014-06-27 17:22 e

    e  回復(fù)  更多評論   

    # re: 深入理解動態(tài)規(guī)劃的一系列問題(15) 2014-06-27 17:22 e

    真的可以評論啊  回復(fù)  更多評論   

    # re: 深入理解動態(tài)規(guī)劃的一系列問題(15)[未登錄] 2014-06-27 17:23 a

    真的可以評論啊啊啊!  回復(fù)  更多評論   

    # re: 深入理解動態(tài)規(guī)劃的一系列問題(15) 2014-07-15 02:29 自媒體

    有料  回復(fù)  更多評論   

    主站蜘蛛池模板: 直接进入免费看黄的网站| 国产亚洲精品无码拍拍拍色欲| 在线观看免费宅男视频| 久久精品国产亚洲AV无码娇色| 一级特黄特色的免费大片视频| 成人a视频片在线观看免费| 中文字幕无码精品亚洲资源网久久 | 午夜国产大片免费观看| 色天使亚洲综合一区二区| 成人免费毛片视频| 亚洲一级特黄特黄的大片| 真人做A免费观看| 亚洲人成777在线播放| 免费国产成人高清在线观看网站| 亚洲精品日韩专区silk| www.黄色免费网站| 亚洲av日韩综合一区久热| 免费日本黄色网址| 国产精品福利在线观看免费不卡| 久久久久亚洲AV综合波多野结衣| 大妹子影视剧在线观看全集免费| 亚洲日韩乱码中文无码蜜桃臀网站 | 国产成人精品免费视频大全麻豆| 亚洲伊人久久精品| 免费鲁丝片一级观看| japanese色国产在线看免费| 亚洲成av人在线视| 妞干网在线免费观看| 免费一级特黄特色大片| 久久精品国产亚洲77777| 热99re久久免费视精品频软件| 国产美女视频免费观看的网站| 亚洲AV无一区二区三区久久| 性色av免费观看| 免费观看在线禁片| 美女黄频视频大全免费的| 亚洲人成片在线观看| 亚洲成在人线av| 免费国产a国产片高清| 日韩成人免费在线| 怡红院亚洲红怡院在线观看|