1 Star 0 Fork 1

吕培栋/DP-Book

forked from mirrors_tianyicui/DP-Book 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
pack.lyx 69.19 KB
一键复制 编辑 原始数据 按行查看 历史
Tianyi Cui 提交于 2011-09-28 18:43 . pack RC1
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497
#LyX 2.0 created this file. For more info see http://www.lyx.org/
\lyxformat 413
\begin_document
\begin_header
\textclass scrartcl
\begin_preamble
\usepackage{datetime}
\renewcommand{\dateseparator}{-}
\usepackage[BoldFont,SlantFont,CJKsetspaces,CJKchecksingle]{xeCJK}
\setCJKmainfont[BoldFont=STSong,ItalicFont=STKaiti]{STSong}
\setCJKsansfont[BoldFont=STHeiti]{STHeiti}
\setCJKmonofont{STFangsong}
\parindent 2em
\end_preamble
\use_default_options true
\maintain_unincluded_children false
\language chinese-simplified
\language_package default
\inputencoding auto
\fontencoding global
\font_roman default
\font_sans default
\font_typewriter default
\font_default_family default
\use_non_tex_fonts true
\font_sc false
\font_osf false
\font_sf_scale 100
\font_tt_scale 100
\graphics none
\default_output_format default
\output_sync 0
\bibtex_command default
\index_command default
\paperfontsize default
\spacing single
\use_hyperref true
\pdf_title "背包问题九讲"
\pdf_author "崔添翼"
\pdf_bookmarks true
\pdf_bookmarksnumbered true
\pdf_bookmarksopen true
\pdf_bookmarksopenlevel 2
\pdf_breaklinks true
\pdf_pdfborder true
\pdf_colorlinks true
\pdf_backref section
\pdf_pdfusetitle true
\papersize a4paper
\use_geometry false
\use_amsmath 1
\use_esint 1
\use_mhchem 1
\use_mathdots 1
\cite_engine basic
\use_bibtopic false
\use_indices false
\paperorientation portrait
\suppress_date false
\use_refstyle 1
\index Index
\shortcut idx
\color #008000
\end_index
\secnumdepth 3
\tocdepth 3
\paragraph_separation indent
\paragraph_indentation default
\quotes_language english
\papercolumns 1
\papersides 1
\paperpagestyle default
\tracking_changes false
\output_changes false
\html_math_output 0
\html_css_as_file 0
\html_be_strict false
\end_header
\begin_body
\begin_layout Title
背包问题九讲 2.0 RC1
\end_layout
\begin_layout Author
崔添翼 (Tianyi Cui)
\begin_inset Foot
status open
\begin_layout Plain Layout
a.k.a.
dd_engi
\end_layout
\end_inset
\end_layout
\begin_layout Date
\begin_inset ERT
status open
\begin_layout Plain Layout
\backslash
yyyymmdddate
\backslash
today
\end_layout
\end_inset
\begin_inset Foot
status open
\begin_layout Plain Layout
Build
\begin_inset ERT
status open
\begin_layout Plain Layout
\backslash
pdfdate
\end_layout
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Standard
本文题为《背包问题九讲》,从属于《动态规划的思考艺术》系列。
\end_layout
\begin_layout Standard
这系列文章的
\begin_inset CommandInset href
LatexCommand href
name "第一版"
target "http://love-oriented.com/pack/"
\end_inset
于2007年下半年使用EmacsMuse制作,以HTML格式发布到网上,转载众多,有一定影响力。
\end_layout
\begin_layout Standard
2011年9月,本系列文章由原作者用LaTeX重新制作并全面修订,您现在看到的是2.0 alpha版本,修订历史及最新版本请访问
\begin_inset CommandInset href
LatexCommand href
target "https://github.com/tianyicui/pack"
\end_inset
查阅。
\end_layout
\begin_layout Standard
本文版权归原作者所有,采用
\begin_inset CommandInset href
LatexCommand href
name "CC BY-NC-SA"
target "http://creativecommons.org/licenses/by-nc-sa/3.0/"
\end_inset
协议发布。
\end_layout
\begin_layout Standard
\begin_inset CommandInset toc
LatexCommand tableofcontents
\end_inset
\end_layout
\begin_layout Section
\begin_inset CommandInset label
LatexCommand label
name "sec:01背包问题"
\end_inset
01背包问题
\end_layout
\begin_layout Subsection
题目
\end_layout
\begin_layout Standard
\begin_inset Formula $N$
\end_inset
件物品和一个容量为
\begin_inset Formula $V$
\end_inset
的背包。放入第
\begin_inset Formula $i$
\end_inset
件物品耗费的费用是
\begin_inset Formula $C_{i}$
\end_inset
\begin_inset Foot
status open
\begin_layout Plain Layout
也即占用背包的空间容量,后文统一称之为“费用(cost)”
\end_layout
\end_inset
,得到的价值是
\begin_inset Formula $W_{i}$
\end_inset
。求解将哪些物品装入背包可使价值总和最大。
\end_layout
\begin_layout Subsection
基本思路
\end_layout
\begin_layout Standard
这是最基础的背包问题,特点是:每种物品仅有一件,可以选择放或不放。
\end_layout
\begin_layout Standard
用子问题定义状态:即
\begin_inset Formula $F[i,v]$
\end_inset
表示前
\begin_inset Formula $i$
\end_inset
件物品恰放入一个容量为
\begin_inset Formula $v$
\end_inset
的背包可以获得的最大价值。则其状态转移方程便是:
\end_layout
\begin_layout Standard
\begin_inset Formula
\[
F[i,v]=\mathrm{max}\{F[i-1,v],F[i-1,v-C_{i}]+W_{i}\}
\]
\end_inset
\end_layout
\begin_layout Standard
这个方程非常重要,基本上所有跟背包相关的问题的方程都是由它衍生出来的。所以有必要将它详细解释一下:“将前
\begin_inset Formula $i$
\end_inset
件物品放入容量为
\begin_inset Formula $v$
\end_inset
的背包中”这个子问题,若只考虑第
\begin_inset Formula $i$
\end_inset
件物品的策略(放或不放),那么就可以转化为一个只和前
\begin_inset Formula $i-1$
\end_inset
件物品相关的问题。如果不放第
\begin_inset Formula $i$
\end_inset
件物品,那么问题就转化为“前
\begin_inset Formula $i-1$
\end_inset
件物品放入容量为
\begin_inset Formula $v$
\end_inset
的背包中”,价值为
\begin_inset Formula $F[i-1,v]$
\end_inset
;如果放第
\begin_inset Formula $i$
\end_inset
件物品,那么问题就转化为“前
\begin_inset Formula $i-1$
\end_inset
件物品放入剩下的容量为
\begin_inset Formula $v-C_{i}$
\end_inset
的背包中”,此时能获得的最大价值就是
\begin_inset Formula $F[i-1,v-C_{i}]$
\end_inset
再加上通过放入第
\begin_inset Formula $i$
\end_inset
件物品获得的价值
\begin_inset Formula $W_{i}$
\end_inset
\end_layout
\begin_layout Standard
伪代码如下:
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $F[0,0..V]\,\leftarrow0$
\end_inset
\end_layout
\begin_layout LyX-Code
for
\begin_inset Formula $i\,\leftarrow1$
\end_inset
to
\begin_inset Formula $N$
\end_inset
\end_layout
\begin_layout LyX-Code
for
\begin_inset Formula $v\,\leftarrow0$
\end_inset
to
\begin_inset Formula $C_{i}-1$
\end_inset
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $F[i,v]\,\leftarrow F[i-1,v]$
\end_inset
\end_layout
\begin_layout LyX-Code
for
\begin_inset Formula $v\,\leftarrow C_{i}$
\end_inset
to
\begin_inset Formula $V$
\end_inset
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $F[i,v]\,\leftarrow\mathrm{max}\{F[i-1,v],F[i-1,v-C_{i}]+W_{i}\}$
\end_inset
\end_layout
\begin_layout Subsection
优化空间复杂度
\end_layout
\begin_layout Standard
以上方法的时间和空间复杂度均为
\begin_inset Formula $O(VN)$
\end_inset
,其中时间复杂度应该已经不能再优化了,但空间复杂度却可以优化到
\begin_inset Formula $O(V)$
\end_inset
\end_layout
\begin_layout Standard
先考虑上面讲的基本思路如何实现,肯定是有一个主循环
\begin_inset Formula $i\text{\,}\leftarrow1\ldots N$
\end_inset
,每次算出来二维数组
\begin_inset Formula $F[i,0\ldots V]$
\end_inset
的所有值。那么,如果只用一个数组
\begin_inset Formula $F[0\ldots V]$
\end_inset
,能不能保证第
\begin_inset Formula $i$
\end_inset
次循环结束后
\begin_inset Formula $F[v]$
\end_inset
中表示的就是我们定义的状态
\begin_inset Formula $F[i,v]$
\end_inset
呢?
\begin_inset Formula $F[i,v]$
\end_inset
是由
\begin_inset Formula $F[i-1,v]$
\end_inset
\begin_inset Formula $F[i-1,v-C_{i}]$
\end_inset
两个子问题递推而来,能否保证在推
\begin_inset Formula $F[i,v]$
\end_inset
时(也即在第
\begin_inset Formula $i$
\end_inset
次主循环中推
\begin_inset Formula $F[v]$
\end_inset
时)能够取用
\begin_inset Formula $F[i-1,v]$
\end_inset
\begin_inset Formula $F[i-1,v-C_{i}]$
\end_inset
的值呢?
\end_layout
\begin_layout Standard
事实上,这要求在每次主循环中我们以
\begin_inset Formula $v\,\leftarrow V\ldots0$
\end_inset
的递减顺序计算
\begin_inset Formula $F[v]$
\end_inset
,这样才能保证计算
\begin_inset Formula $F[v]$
\end_inset
\begin_inset Formula $F[v-C_{i}]$
\end_inset
保存的是状态
\begin_inset Formula $F[i-1,v-C_{i]}$
\end_inset
的值。伪代码如下:
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $F[0..V]\text{\,\leftarrow}0$
\end_inset
\end_layout
\begin_layout LyX-Code
for
\begin_inset Formula $i\,\leftarrow1$
\end_inset
to
\begin_inset Formula $N$
\end_inset
\end_layout
\begin_layout LyX-Code
for
\begin_inset Formula $v\,\leftarrow V$
\end_inset
to
\begin_inset Formula $C_{i}$
\end_inset
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $F[v]\,\leftarrow\mathrm{max}\{F[v],F[v-C_{i}]+W_{i}\}$
\end_inset
\end_layout
\begin_layout Standard
其中的
\begin_inset Formula $F[v]\,\leftarrow\mathrm{max}\{F[v],F[v-C_{i}]+W_{i}\}$
\end_inset
一句,恰就对应于我们原来的转移方程,因为现在的
\begin_inset Formula $F[v-C_{i}]$
\end_inset
就相当于原来的
\begin_inset Formula $F[i-1,v-C_{i}]$
\end_inset
。如果将
\begin_inset Formula $v$
\end_inset
的循环顺序从上面的逆序改成顺序的话,那么则成了
\begin_inset Formula $F[i,v]$
\end_inset
\begin_inset Formula $F[i,v-C_{i}]$
\end_inset
推导得到,与本题意不符。
\end_layout
\begin_layout Standard
事实上,使用一维数组解01背包的程序在后面会被多次用到,所以这里抽象出一个处理一件01背包中的物品过程,以后的代码中直接调用不加说明。
\end_layout
\begin_layout LyX-Code
def
\begin_inset Formula $\mathsf{ZeroOnePack}$
\end_inset
(
\begin_inset Formula $F,C,W$
\end_inset
)
\end_layout
\begin_layout LyX-Code
for
\begin_inset Formula $v\,\leftarrow V$
\end_inset
to
\begin_inset Formula $C$
\end_inset
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $ $
\end_inset
\begin_inset Formula $F[v]\,\leftarrow\mathrm{max}(F[v],f[v-C]+W)$
\end_inset
\end_layout
\begin_layout Standard
有了这个过程以后,01背包问题的伪代码就可以这样写:
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $F[0..V]\text{\,\leftarrow}0$
\end_inset
\end_layout
\begin_layout LyX-Code
for
\begin_inset Formula $i\,\leftarrow1$
\end_inset
to
\begin_inset Formula $N$
\end_inset
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $\mathsf{ZeroOnePack}$
\end_inset
(
\begin_inset Formula $F,C_{i},W_{i}$
\end_inset
)
\end_layout
\begin_layout Subsection
初始化的细节问题
\end_layout
\begin_layout Standard
我们看到的求最优解的背包问题题目中,事实上有两种不太相同的问法。有的题目要求“恰好装满背包”时的最优解,有的题目则并没有要求必须把背包装满。一种区别这两种问法的
实现方法是在初始化的时候有所不同。
\end_layout
\begin_layout Standard
如果是第一种问法,要求恰好装满背包,那么在初始化时除了
\begin_inset Formula $F[0]$
\end_inset
\begin_inset Formula $0$
\end_inset
,其它
\begin_inset Formula $F[1..V]$
\end_inset
均设为
\begin_inset Formula $-\infty$
\end_inset
,这样就可以保证最终得到的
\begin_inset Formula $F[V]$
\end_inset
是一种恰好装满背包的最优解。
\end_layout
\begin_layout Standard
如果并没有要求必须把背包装满,而是只希望价格尽量大,初始化时应该将
\begin_inset Formula $F[0..V]$
\end_inset
全部设为
\begin_inset Formula $0$
\end_inset
\end_layout
\begin_layout Standard
这是为什么呢?可以这样理解:初始化的
\begin_inset Formula $F$
\end_inset
数组事实上就是在没有任何物品可以放入背包时的合法状态。如果要求背包恰好装满,那么此时只有容量为
\begin_inset Formula $0$
\end_inset
的背包可以在什么也不装且价值为
\begin_inset Formula $0$
\end_inset
的情况下被“恰好装满”,其它容量的背包均没有合法的解,属于未定义的状态,应该被赋值为-∞了。如果背包并非必须被装满,那么任何容量的背包都有一个合法解“什么都不装
”,这个解的价值为
\begin_inset Formula $0$
\end_inset
,所以初始时状态的值也就全部为
\begin_inset Formula $0$
\end_inset
了。
\end_layout
\begin_layout Standard
这个小技巧完全可以推广到其它类型的背包问题,后面不再对进行状态转移之前的初始化进行讲解。
\end_layout
\begin_layout Subsection
一个常数优化
\end_layout
\begin_layout Standard
上面伪代码中的
\end_layout
\begin_layout LyX-Code
for
\begin_inset Formula $i\,\leftarrow1$
\end_inset
to
\begin_inset Formula $N$
\end_inset
\end_layout
\begin_layout LyX-Code
for
\begin_inset Formula $v\,\leftarrow V$
\end_inset
to
\begin_inset Formula $C_{i}$
\end_inset
\end_layout
\begin_layout Standard
中第二重循环的下限可以改进。它可以被优化为
\end_layout
\begin_layout LyX-Code
for
\begin_inset Formula $i\,\leftarrow1$
\end_inset
to
\begin_inset Formula $N$
\end_inset
\end_layout
\begin_layout LyX-Code
for
\begin_inset Formula $v\,\leftarrow V$
\end_inset
to
\begin_inset Formula $\mathrm{max}(V-\Sigma_{i}^{N}W_{i},C_{i})$
\end_inset
\end_layout
\begin_layout Standard
这个优化之所以成立的原因请读者自己思考。(提示:使用二维的转移方程思考较易。)
\end_layout
\begin_layout Subsection
小结
\end_layout
\begin_layout Standard
01背包问题是最基本的背包问题,它包含了背包问题中设计状态、方程的最基本思想。另外,别的类型的背包问题往往也可以转换成01背包问题求解。故一定要仔细体会上面基本
思路的得出方法,状态转移方程的意义,以及空间复杂度怎样被优化。
\end_layout
\begin_layout Section
\begin_inset CommandInset label
LatexCommand label
name "sec:完全背包问题"
\end_inset
完全背包问题
\end_layout
\begin_layout Subsection
题目
\end_layout
\begin_layout Standard
\begin_inset Formula $N$
\end_inset
种物品和一个容量为
\begin_inset Formula $V$
\end_inset
的背包,每种物品都有无限件可用。放入第
\begin_inset Formula $i$
\end_inset
种物品的费用是
\begin_inset Formula $C_{i}$
\end_inset
,价值是
\begin_inset Formula $W_{i}$
\end_inset
。求解:将哪些物品装入背包,可使这些物品的耗费的费用总和不超过背包容量,且价值总和最大。
\end_layout
\begin_layout Subsection
基本思路
\end_layout
\begin_layout Standard
这个问题非常类似于01背包问题,所不同的是每种物品有无限件。也就是从每种物品的角度考虑,与它相关的策略已并非取或不取两种,而是有取
\begin_inset Formula $0$
\end_inset
件、取
\begin_inset Formula $1$
\end_inset
件、取
\begin_inset Formula $2$
\end_inset
件……直至取
\begin_inset Formula $\left\lfloor V/C_{i}\right\rfloor $
\end_inset
件等许多种。
\end_layout
\begin_layout Standard
如果仍然按照解01背包时的思路,令
\begin_inset Formula $F[i,v]$
\end_inset
表示前
\begin_inset Formula $i$
\end_inset
种物品恰放入一个容量为
\begin_inset Formula $v$
\end_inset
的背包的最大权值。仍然可以按照每种物品不同的策略写出状态转移方程,像这样:
\end_layout
\begin_layout Standard
\begin_inset Formula
\[
F[i,v]=\mathrm{max}\{F[i-1,v-kC_{i}]+kW_{i}\:|\:0\leq kC_{i}\leq v\}
\]
\end_inset
\end_layout
\begin_layout Standard
这跟01背包问题一样有
\begin_inset Formula $O(VN)$
\end_inset
个状态需要求解,但求解每个状态的时间已经不是常数了,求解状态
\begin_inset Formula $F[i,v]$
\end_inset
的时间是
\begin_inset Formula $O(\frac{v}{C_{i}})$
\end_inset
,总的复杂度可以认为是
\begin_inset Formula $O(NV\Sigma{\frac{{V}}{C_{i}}})$
\end_inset
,是比较大的。
\end_layout
\begin_layout Standard
将01背包问题的基本思路加以改进,得到了这样一个清晰的方法。这说明01背包问题的方程的确是很重要,可以推及其它类型的背包问题。但我们还是要试图改进这个复杂度。
\end_layout
\begin_layout Subsection
\begin_inset CommandInset label
LatexCommand label
name "sub:一个简单有效的优化"
\end_inset
一个简单有效的优化
\end_layout
\begin_layout Standard
完全背包问题有一个很简单有效的优化,是这样的:若两件物品
\begin_inset Formula $i$
\end_inset
\begin_inset Formula $j$
\end_inset
满足
\begin_inset Formula $C_{i}\leq C_{j}$
\end_inset
\begin_inset Formula $W_{i}\geq W_{j}$
\end_inset
,则将可以将物品
\begin_inset Formula $j$
\end_inset
直接去掉,不用考虑。
\end_layout
\begin_layout Standard
这个优化的正确性是显然的:任何情况下都可将价值小费用高的
\begin_inset Formula $j$
\end_inset
换成物美价廉的
\begin_inset Formula $i$
\end_inset
,得到的方案至少不会更差。对于随机生成的数据,这个方法往往会大大减少物品的件数,从而加快速度。然而这个并不能改善最坏情况的复杂度,因为有可能特别设计的数据可以一
件物品也去不掉。
\end_layout
\begin_layout Standard
这个优化可以简单的
\begin_inset Formula $O(N^{2})$
\end_inset
地实现,一般都可以承受。另外,针对背包问题而言,比较不错的一种方法是:首先将费用大于
\begin_inset Formula $V$
\end_inset
的物品去掉,然后使用类似计数排序的做法,计算出费用相同的物品中价值最高的是哪个,可以
\begin_inset Formula $O(V+N)$
\end_inset
地完成这个优化。这个不太重要的过程就不给出伪代码了,希望你能独立思考写出伪代码或程序。
\end_layout
\begin_layout Subsection
转化为01背包问题求解
\end_layout
\begin_layout Standard
01背包问题是最基本的背包问题,我们可以考虑把完全背包问题转化为01背包问题来解。
\end_layout
\begin_layout Standard
最简单的想法是,考虑到第
\begin_inset Formula $i$
\end_inset
种物品最多选
\begin_inset Formula $\left\lfloor V/C_{i}\right\rfloor $
\end_inset
件,于是可以把第i种物品转化为
\begin_inset Formula $\left\lfloor V/C_{i}\right\rfloor $
\end_inset
件费用及价值均不变的物品,然后求解这个01背包问题。这样的做法完全没有改进时间复杂度,但这种方法也指明了将完全背包问题转化为01背包问题的思路:将一种物品拆成多
件只能选
\begin_inset Formula $0$
\end_inset
件或
\begin_inset Formula $1$
\end_inset
件的01背包中的物品。
\end_layout
\begin_layout Standard
更高效的转化方法是:把第
\begin_inset Formula $i$
\end_inset
种物品拆成费用为
\begin_inset Formula $C_{i}2^{k}$
\end_inset
、价值为
\begin_inset Formula $W_{i}2^{k}$
\end_inset
的若干件物品,其中
\begin_inset Formula $k$
\end_inset
取遍满足
\begin_inset Formula $C_{i}2^{k}\leq V$
\end_inset
的非负整数。
\end_layout
\begin_layout Standard
这是二进制的思想。因为,不管最优策略选几件第
\begin_inset Formula $i$
\end_inset
种物品,其件数写成二进制后,总可以表示成若干个
\begin_inset Formula $2^{k}$
\end_inset
件物品的和。这样一来就把每种物品拆成
\begin_inset Formula $O(\mathrm{log}\left\lfloor V/C_{i}\right\rfloor )$
\end_inset
件物品,是一个很大的改进。
\end_layout
\begin_layout Subsection
\begin_inset Formula $O(VN)$
\end_inset
的算法
\end_layout
\begin_layout Standard
这个算法使用一维数组,先看伪代码:
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $F[0..V]\text{\,\leftarrow}0$
\end_inset
\end_layout
\begin_layout LyX-Code
for
\begin_inset Formula $i\,\leftarrow1$
\end_inset
to
\begin_inset Formula $N$
\end_inset
\end_layout
\begin_layout LyX-Code
for
\begin_inset Formula $v\,\leftarrow C_{i}$
\end_inset
to
\begin_inset Formula $V$
\end_inset
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $ $
\end_inset
\begin_inset Formula $F[v]\,\leftarrow\mathrm{max}(F[v],F[v-C_{i}]+W_{i})$
\end_inset
\end_layout
\begin_layout Standard
你会发现,这个伪代码与01背包问题的伪代码只有
\begin_inset Formula $v$
\end_inset
的循环次序不同而已。
\end_layout
\begin_layout Standard
为什么这个算法就可行呢?首先想想为什么01背包中要按照
\begin_inset Formula $v$
\end_inset
递减的次序来循环。让
\begin_inset Formula $v$
\end_inset
递减是为了保证第
\begin_inset Formula $i$
\end_inset
次循环中的状态
\begin_inset Formula $F[i,v]$
\end_inset
是由状态
\begin_inset Formula $F[i-1,v-C_{i}]$
\end_inset
递推而来。换句话说,这正是为了保证每件物品只选一次,保证在考虑“选入第
\begin_inset Formula $i$
\end_inset
件物品”这件策略时,依据的是一个绝无已经选入第
\begin_inset Formula $i$
\end_inset
件物品的子结果
\begin_inset Formula $F[i-1,v-C_{i}]$
\end_inset
。而现在完全背包的特点恰是每种物品可选无限件,所以在考虑“加选一件第
\begin_inset Formula $i$
\end_inset
种物品”这种策略时,却正需要一个可能已选入第
\begin_inset Formula $i$
\end_inset
种物品的子结果
\begin_inset Formula $F[i,v-C_{i}]$
\end_inset
,所以就可以并且必须采用
\begin_inset Formula $v$
\end_inset
递增的顺序循环。这就是这个简单的程序为何成立的道理。
\end_layout
\begin_layout Standard
值得一提的是,上面的伪代码中两层for循环的次序可以颠倒。这个结论有可能会带来算法时间常数上的优化。
\end_layout
\begin_layout Standard
这个算法也可以由另外的思路得出。例如,将基本思路中求解
\begin_inset Formula $F[i,v-C_{i}]$
\end_inset
的状态转移方程显式地写出来,代入原方程中,会发现该方程可以等价地变形成这种形式:
\end_layout
\begin_layout Standard
\begin_inset Formula
\[
F[i,v]=\mathrm{max(}{F[i-1,v],F[i,v-C_{i}]+W_{i}})
\]
\end_inset
\end_layout
\begin_layout Standard
将这个方程用一维数组实现,便得到了上面的伪代码。
\end_layout
\begin_layout Standard
最后抽象出处理一件完全背包类物品的过程伪代码:
\end_layout
\begin_layout LyX-Code
def
\begin_inset Formula $\mathsf{CompletePack}$
\end_inset
(
\begin_inset Formula $F,C,W$
\end_inset
)
\end_layout
\begin_layout LyX-Code
for
\begin_inset Formula $v\,\leftarrow C$
\end_inset
to
\begin_inset Formula $V$
\end_inset
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $F[v]\,\leftarrow\mathrm{max}\{F[v],f[v-C]+W\}$
\end_inset
\end_layout
\begin_layout Subsection
小结
\end_layout
\begin_layout Standard
完全背包问题也是一个相当基础的背包问题,它有两个状态转移方程。希望读者能够对这两个状态转移方程都仔细地体会,不仅记住,也要弄明白它们是怎么得出来的,最好能够自己
想一种得到这些方程的方法。
\end_layout
\begin_layout Standard
事实上,对每一道动态规划题目都思考其方程的意义以及如何得来,是加深对动态规划的理解、提高动态规划功力的好方法。
\end_layout
\begin_layout Section
\begin_inset CommandInset label
LatexCommand label
name "sec:多重背包问题"
\end_inset
多重背包问题
\end_layout
\begin_layout Subsection
题目
\end_layout
\begin_layout Standard
\begin_inset Formula $N$
\end_inset
种物品和一个容量为
\begin_inset Formula $V$
\end_inset
的背包。第
\begin_inset Formula $i$
\end_inset
种物品最多有
\begin_inset Formula $M_{i}$
\end_inset
件可用,每件耗费的空间是
\begin_inset Formula $C_{i}$
\end_inset
,价值是
\begin_inset Formula $W_{i}$
\end_inset
。求解将哪些物品装入背包可使这些物品的耗费的空间总和不超过背包容量,且价值总和最大。
\end_layout
\begin_layout Subsection
基本算法
\end_layout
\begin_layout Standard
这题目和完全背包问题很类似。基本的方程只需将完全背包问题的方程略微一改即可。
\end_layout
\begin_layout Standard
因为对于第
\begin_inset Formula $i$
\end_inset
种物品有
\begin_inset Formula $M_{i}+1$
\end_inset
种策略:取
\begin_inset Formula $0$
\end_inset
件,取
\begin_inset Formula $1$
\end_inset
件……取
\begin_inset Formula $M_{i}$
\end_inset
件。令
\begin_inset Formula $F[i,v]$
\end_inset
表示前
\begin_inset Formula $i$
\end_inset
种物品恰放入一个容量为
\begin_inset Formula $v$
\end_inset
的背包的最大价值,则有状态转移方程:
\end_layout
\begin_layout Standard
\begin_inset Formula
\[
F[i\text{,}v]=\mathrm{max}\{F[i-1,v-k*C_{i}]+k*W_{i}\:|\:0\leq k\leq M_{i}\}
\]
\end_inset
\end_layout
\begin_layout Standard
复杂度是
\begin_inset Formula $O(V\Sigma M_{i})$
\end_inset
\end_layout
\begin_layout Subsection
转化为01背包问题
\end_layout
\begin_layout Standard
另一种好想好写的基本方法是转化为01背包求解:把第
\begin_inset Formula $i$
\end_inset
种物品换成
\begin_inset Formula $M_{i}$
\end_inset
件01背包中的物品,则得到了物品数为
\begin_inset Formula $\Sigma M_{i}$
\end_inset
的01背包问题。直接求解之,复杂度仍然是
\begin_inset Formula $O(V\Sigma M_{i})$
\end_inset
\end_layout
\begin_layout Standard
但是我们期望将它转化为01背包问题之后,能够像完全背包一样降低复杂度。
\end_layout
\begin_layout Standard
仍然考虑二进制的思想,我们考虑把第
\begin_inset Formula $i$
\end_inset
种物品换成若干件物品,使得原问题中第
\begin_inset Formula $i$
\end_inset
种物品可取的每种策略——取
\begin_inset Formula $0\ldots M_{i}$
\end_inset
件——均能等价于取若干件代换以后的物品。另外,取超过
\begin_inset Formula $M_{i}$
\end_inset
件的策略必不能出现。
\end_layout
\begin_layout Standard
方法是:将第
\begin_inset Formula $i$
\end_inset
种物品分成若干件01背包中的物品,其中每件物品有一个系数。这件物品的费用和价值均是原来的费用和价值乘以这个系数。令这些系数分别为
\begin_inset Formula $1,2,2^{2}\ldots2^{k-1},M_{i}-2^{k}+1$
\end_inset
,且
\begin_inset Formula $k$
\end_inset
是满足
\begin_inset Formula $M_{i}-2^{k}+1>0$
\end_inset
的最大整数。例如,如果
\begin_inset Formula $M_{i}$
\end_inset
\begin_inset Formula $13$
\end_inset
,则相应的
\begin_inset Formula $k=3$
\end_inset
,这种最多取
\begin_inset Formula $13$
\end_inset
件的物品应被分成系数分别为
\begin_inset Formula $1,2,4,6$
\end_inset
的四件物品。
\end_layout
\begin_layout Standard
分成的这几件物品的系数和为
\begin_inset Formula $M_{i}$
\end_inset
,表明不可能取多于
\begin_inset Formula $M_{i}$
\end_inset
件的第
\begin_inset Formula $i$
\end_inset
种物品。另外这种方法也能保证对于
\begin_inset Formula $0\ldots M_{i}$
\end_inset
间的每一个整数,均可以用若干个系数的和表示。这里算法正确性的证明可以分
\begin_inset Formula $0\ldots2^{k-1}$
\end_inset
\begin_inset Formula $2^{k}\ldots M_{i}$
\end_inset
两段来分别讨论得出,希望读者自己思考尝试一下。
\end_layout
\begin_layout Standard
这样就将第i种物品分成了
\begin_inset Formula $O(\mathrm{log}M_{i})$
\end_inset
种物品,将原问题转化为了复杂度为
\begin_inset Formula $O(V\Sigma\mathrm{{log}M_{i})}$
\end_inset
的01背包问题,是很大的改进。
\end_layout
\begin_layout Standard
下面给出
\begin_inset Formula $O(\mathrm{log}M)$
\end_inset
时间处理一件多重背包中物品的过程:
\end_layout
\begin_layout LyX-Code
def
\begin_inset Formula $\mathsf{MultiplePack}$
\end_inset
(
\begin_inset Formula $F$
\end_inset
,
\begin_inset Formula $C$
\end_inset
,
\begin_inset Formula $W$
\end_inset
,
\begin_inset Formula $M$
\end_inset
)
\end_layout
\begin_layout LyX-Code
if
\begin_inset Formula $C\cdot M\geq V$
\end_inset
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $\mathsf{CompletePack}$
\end_inset
(
\begin_inset Formula $F$
\end_inset
,
\begin_inset Formula $C$
\end_inset
,
\begin_inset Formula $W$
\end_inset
)
\end_layout
\begin_layout LyX-Code
return
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $k\,\leftarrow1$
\end_inset
\end_layout
\begin_layout LyX-Code
while
\begin_inset Formula $k<M$
\end_inset
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $\mathsf{ZeroOnePack}$
\end_inset
(
\begin_inset Formula $kC$
\end_inset
,
\begin_inset Formula $kW$
\end_inset
)
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $M\text{\,\leftarrow}M-k$
\end_inset
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $k\,\leftarrow2k$
\end_inset
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $\mathsf{ZeroOnePack}$
\end_inset
(
\begin_inset Formula $C\cdot M$
\end_inset
,
\begin_inset Formula $W\cdot M$
\end_inset
)
\end_layout
\begin_layout Standard
希望你仔细体会这个伪代码,如果不太理解的话,不妨翻译成程序代码以后,单步执行几次,或者头脑加纸笔模拟一下,以加深理解。
\end_layout
\begin_layout Subsection
可行性问题
\begin_inset Formula $O(VN)$
\end_inset
的算法
\end_layout
\begin_layout Standard
当问题是“每种有若干件的物品能否填满给定容量的背包”,只须考虑填满背包的可行性,不需考虑每件物品的价值时,多重背包问题同样有
\begin_inset Formula $O(VN)$
\end_inset
复杂度的算法。
\end_layout
\begin_layout Standard
例如,可以使用单调队列的数据结构,优化基本算法的状态转移方程,使每个状态的值可以以均摊
\begin_inset Formula $O(1)$
\end_inset
的时间求解。
\begin_inset Foot
status open
\begin_layout Plain Layout
我最初了解到这个方法是在楼天成的“男人八题”幻灯片上。
\end_layout
\end_inset
\end_layout
\begin_layout Standard
下面介绍一种实现较为简单的
\begin_inset Formula $O(VN)$
\end_inset
复杂度解多重背包问题的算法。它的基本思想是这样的:设
\begin_inset Formula $F[i,j]$
\end_inset
表示“用了前
\begin_inset Formula $i$
\end_inset
种物品填满容量为
\begin_inset Formula $j$
\end_inset
的背包后,最多还剩下几个第
\begin_inset Formula $i$
\end_inset
种物品可用”,如果
\begin_inset Formula $F[i,j]=-1$
\end_inset
则说明这种状态不可行,若可行应满足
\begin_inset Formula $0\leq F[i,j]\leq M_{i}$
\end_inset
\end_layout
\begin_layout Standard
递推求
\begin_inset Formula $F[i,j]$
\end_inset
的伪代码如下:
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $F[0,1\ldots V]\,\leftarrow-1$
\end_inset
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $F[0,0]\,\leftarrow0$
\end_inset
\end_layout
\begin_layout LyX-Code
for
\begin_inset Formula $i\,\leftarrow1$
\end_inset
to
\begin_inset Formula $N$
\end_inset
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $ $
\end_inset
for
\begin_inset Formula $j\,\leftarrow0$
\end_inset
to
\begin_inset Formula $V$
\end_inset
\end_layout
\begin_layout LyX-Code
if
\begin_inset Formula $F[i-1][j]\geq0$
\end_inset
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $F[i][j]=M_{i}$
\end_inset
\end_layout
\begin_layout LyX-Code
else
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $F[i][j]=-1$
\end_inset
\end_layout
\begin_layout LyX-Code
for
\begin_inset Formula $j\,\leftarrow0$
\end_inset
to
\begin_inset Formula $V-C_{i}$
\end_inset
\end_layout
\begin_layout LyX-Code
if
\begin_inset Formula $F[i][j]>0$
\end_inset
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $F[i][j+C_{i}]\,\leftarrow\mathrm{max}\{F[i][j+C_{i}],F[i][j]-1\}$
\end_inset
\end_layout
\begin_layout Standard
最终
\begin_inset Formula $F[N][0\ldots V]$
\end_inset
便是多重背包可行性问题的答案。
\end_layout
\begin_layout Subsection
小结
\end_layout
\begin_layout Standard
在这一讲中,我们看到了将一个算法的复杂度由
\begin_inset Formula $O(V\Sigma M_{i})$
\end_inset
改进到
\begin_inset Formula $O(V\Sigma\mathrm{{log}M_{i})}$
\end_inset
的过程,还知道了存在复杂度为
\begin_inset Formula $O(VN)$
\end_inset
的算法。
\end_layout
\begin_layout Standard
希望你特别注意“拆分物品”的思想和方法,自己证明一下它的正确性,并将完整的程序代码写出来。
\end_layout
\begin_layout Section
混合三种背包问题
\end_layout
\begin_layout Subsection
问题
\end_layout
\begin_layout Standard
如果将前面
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:01背包问题"
\end_inset
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:完全背包问题"
\end_inset
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:多重背包问题"
\end_inset
中的三种背包问题混合起来。也就是说,有的物品只可以取一次(01背包),有的物品可以取无限次(完全背包),有的物品可以取的次数有一个上限(多重背包)。应该怎么求解
呢?
\end_layout
\begin_layout Subsection
01背包与完全背包的混合
\end_layout
\begin_layout Standard
考虑到01背包和完全背包中给出的伪代码只有一处不同,故如果只有两类物品:一类物品只能取一次,另一类物品可以取无限次,那么只需在对每个物品应用转移方程时,根据物品
的类别选用顺序或逆序的循环即可,复杂度是
\begin_inset Formula $O(VN)$
\end_inset
。伪代码如下:
\end_layout
\begin_layout LyX-Code
for
\begin_inset Formula $i\,\leftarrow1$
\end_inset
to
\begin_inset Formula $N$
\end_inset
\end_layout
\begin_layout LyX-Code
if 第
\begin_inset Formula $i$
\end_inset
件物品属于01背包
\end_layout
\begin_layout LyX-Code
for
\begin_inset Formula $v\,\leftarrow V$
\end_inset
to
\begin_inset Formula $C_{i}$
\end_inset
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $F[v]\,\leftarrow\mathrm{max}(F[v],F[v-C_{i}]+W_{i})$
\end_inset
\end_layout
\begin_layout LyX-Code
else if 第
\begin_inset Formula $i$
\end_inset
件物品属于完全背包
\end_layout
\begin_layout LyX-Code
for
\begin_inset Formula $v\,\leftarrow C_{i}$
\end_inset
to
\begin_inset Formula $V$
\end_inset
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $F[v]\,\leftarrow\mathrm{max}(F[v],F[v-C_{i}]+W_{i})$
\end_inset
\end_layout
\begin_layout Subsection
再加上多重背包
\end_layout
\begin_layout Standard
如果再加上最多可以取有限次的多重背包式的物品,那么利用单调队列,也可以给出均摊
\begin_inset Formula $O(VN)$
\end_inset
的解法。
\end_layout
\begin_layout Standard
但如果不考虑单调队列算法的话,用将每个这类物品分成
\begin_inset Formula $\text{O(\mathrm{log}}M_{i})$
\end_inset
个01背包的物品的方法也已经很优了。
\end_layout
\begin_layout Standard
最清晰的写法是调用我们前面给出的三个过程。
\end_layout
\begin_layout LyX-Code
for
\begin_inset Formula $i\,\leftarrow1$
\end_inset
to
\begin_inset Formula $N$
\end_inset
\end_layout
\begin_layout LyX-Code
if 第
\begin_inset Formula $i$
\end_inset
件物品属于01背包
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $\mathsf{ZeroOnePack}$
\end_inset
(
\begin_inset Formula $F$
\end_inset
,
\begin_inset Formula $C_{i}$
\end_inset
,
\begin_inset Formula $W_{i}$
\end_inset
)
\end_layout
\begin_layout LyX-Code
else if 第
\begin_inset Formula $i$
\end_inset
件物品属于完全背包
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $\mathsf{CompletePack}$
\end_inset
(
\begin_inset Formula $F$
\end_inset
,
\begin_inset Formula $C_{i}$
\end_inset
,
\begin_inset Formula $W_{i}$
\end_inset
)
\end_layout
\begin_layout LyX-Code
else if 第
\begin_inset Formula $i$
\end_inset
件物品属于多重背包
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $\mathsf{MultiplePack}$
\end_inset
(
\begin_inset Formula $F$
\end_inset
,
\begin_inset Formula $C_{i}$
\end_inset
,
\begin_inset Formula $W_{i}$
\end_inset
,
\begin_inset Formula $N_{i}$
\end_inset
)
\end_layout
\begin_layout Standard
在最初写出这三个过程的时候,可能完全没有想到它们会在这里混合应用。我想这体现了编程中抽象的威力。如果你一直就是以这种“抽象出过程”的方式写每一类背包问题的,也非
常清楚它们的实现中细微的不同,那么在遇到混合三种背包问题的题目时,一定能很快想到上面简洁的解法,对吗?
\end_layout
\begin_layout Subsection
小结
\end_layout
\begin_layout Standard
有人说,困难的题目都是由简单的题目叠加而来的。这句话是否公理暂且存之不论,但它在本讲中已经得到了充分的体现。本来01背包、完全背包、多重背包都不是什么难题,但将
它们简单地组合起来以后就得到了这样一道一定能吓倒不少人的题目。但只要基础扎实,领会三种基本背包问题的思想,就可以做到把困难的题目拆分成简单的题目来解决。
\end_layout
\begin_layout Section
二维费用的背包问题
\end_layout
\begin_layout Subsection
问题
\end_layout
\begin_layout Standard
二维费用的背包问题是指:对于每件物品,具有两种不同的费用,选择这件物品必须同时付出这两种费用。对于每种费用都有一个可付出的最大值(背包容量)。问怎样选择物品可以
得到最大的价值。
\end_layout
\begin_layout Standard
设第
\begin_inset Formula $i$
\end_inset
件物品所需的两种费用分别为
\begin_inset Formula $C_{i}$
\end_inset
\begin_inset Formula $D_{i}$
\end_inset
。两种费用可付出的最大值(也即两种背包容量)分别为
\begin_inset Formula $V$
\end_inset
\begin_inset Formula $U$
\end_inset
。物品的价值为
\begin_inset Formula $W_{i}$
\end_inset
\end_layout
\begin_layout Subsection
算法
\end_layout
\begin_layout Standard
费用加了一维,只需状态也加一维即可。设
\begin_inset Formula $F[i,v,u]$
\end_inset
表示前
\begin_inset Formula $i$
\end_inset
件物品付出两种费用分别为
\begin_inset Formula $v$
\end_inset
\begin_inset Formula $u$
\end_inset
时可获得的最大价值。状态转移方程就是:
\end_layout
\begin_layout Standard
\begin_inset Formula
\[
F[i,v,u]=\mathrm{max\{F[i-1,v,u],F[i-1,v-C_{i},u-D_{i}]+W_{i}\}}
\]
\end_inset
\end_layout
\begin_layout Standard
如前述优化空间复杂度的方法,可以只使用二维的数组:当每件物品只可以取一次时变量
\begin_inset Formula $v$
\end_inset
\begin_inset Formula $u$
\end_inset
采用逆序的循环,当物品有如完全背包问题时采用顺序的循环,当物品有如多重背包问题时拆分物品。
\end_layout
\begin_layout Standard
这里就不再给出伪代码了,相信有了前面的基础,读者应该能够自己实现出这个问题的程序。
\end_layout
\begin_layout Subsection
物品总个数的限制
\end_layout
\begin_layout Standard
有时,“二维费用”的条件是以这样一种隐含的方式给出的:最多只能取
\begin_inset Formula $U$
\end_inset
件物品。这事实上相当于每件物品多了一种“件数”的费用,每个物品的件数费用均为
\begin_inset Formula $1$
\end_inset
,可以付出的最大件数费用为
\begin_inset Formula $U$
\end_inset
。换句话说,设
\begin_inset Formula $F[v,u]$
\end_inset
表示付出费用
\begin_inset Formula $v$
\end_inset
、最多选
\begin_inset Formula $u$
\end_inset
件时可得到的最大价值,则根据物品的类型(01、完全、多重)用不同的方法循环更新,最后在
\begin_inset Formula $f[0\ldots V,0\ldots U]$
\end_inset
范围内寻找答案。
\end_layout
\begin_layout Subsection
二维整数域
\begin_inset Formula $N^{2}$
\end_inset
上的背包问题
\end_layout
\begin_layout Standard
另一种看待二维背包问题的思路是:将它看待成
\begin_inset Formula $N^{2}$
\end_inset
域上的背包问题。也就是说,背包的容量以及每件物品的费用都是一个二维向量。而常见的一维背包问题则是自然数域上的背包问题。所以说,一维背包的种种思想方法,往往可以应
用于二位背包问题的求解中,因为只是数域扩大了而已。
\end_layout
\begin_layout Standard
作为这种思想的练习,你可以尝试将后文中提到的“子集和问题”扩展到二维,并试图用同样的复杂度解决。
\end_layout
\begin_layout Subsection
小结
\end_layout
\begin_layout Standard
当发现由熟悉的动态规划题目变形得来的题目时,在原来的状态中加一维以满足新的限制是一种比较通用的方法。希望你能从本讲中初步体会到这种方法。
\end_layout
\begin_layout Section
\begin_inset CommandInset label
LatexCommand label
name "sec:分组的背包问题"
\end_inset
分组的背包问题
\end_layout
\begin_layout Subsection
问题
\end_layout
\begin_layout Standard
\begin_inset Formula $N$
\end_inset
件物品和一个容量为
\begin_inset Formula $V$
\end_inset
的背包。第
\begin_inset Formula $i$
\end_inset
件物品的费用是
\begin_inset Formula $C_{i}$
\end_inset
,价值是
\begin_inset Formula $W_{i}$
\end_inset
。这些物品被划分为
\begin_inset Formula $K$
\end_inset
组,每组中的物品互相冲突,最多选一件。求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大。
\end_layout
\begin_layout Subsection
算法
\end_layout
\begin_layout Standard
这个问题变成了每组物品有若干种策略:是选择本组的某一件,还是一件都不选。也就是说设
\begin_inset Formula $F[k,v]$
\end_inset
表示前
\begin_inset Formula $k$
\end_inset
组物品花费费用
\begin_inset Formula $v$
\end_inset
能取得的最大权值,则有:
\end_layout
\begin_layout Standard
\begin_inset Formula
\[
F[k,v]=\mathrm{max}\{F[k-1,v],F[k-1,v-C_{i}]+W_{i}\:|\:\mathrm{item}\: i\in\mathrm{group}\: k\}
\]
\end_inset
\end_layout
\begin_layout Standard
使用一维数组的伪代码如下:
\end_layout
\begin_layout LyX-Code
for
\begin_inset Formula $k\,\leftarrow1$
\end_inset
to
\begin_inset Formula $K$
\end_inset
\end_layout
\begin_layout LyX-Code
for
\begin_inset Formula $v\,\leftarrow V$
\end_inset
to
\begin_inset Formula $0$
\end_inset
\end_layout
\begin_layout LyX-Code
for all item
\begin_inset Formula $i$
\end_inset
in group
\begin_inset Formula $k$
\end_inset
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $F[v]\,\leftarrow\mathrm{max}\{F[v],F[v-C_{i}]+W_{i}\}$
\end_inset
\end_layout
\begin_layout Standard
这里三层循环的顺序保证了每一组内的物品最多只有一个会被添加到背包中。
\end_layout
\begin_layout Standard
另外,显然可以对每组内的物品应用
\begin_inset CommandInset ref
LatexCommand ref
reference "sub:一个简单有效的优化"
\end_inset
中的优化。
\end_layout
\begin_layout Subsection
小结
\end_layout
\begin_layout Standard
分组的背包问题将彼此互斥的若干物品称为一个组,这建立了一个很好的模型。不少背包问题的变形都可以转化为分组的背包问题(例如
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:有依赖的背包问题"
\end_inset
),由分组的背包问题进一步可定义“泛化物品”的概念,十分有利于解题。
\end_layout
\begin_layout Section
\begin_inset CommandInset label
LatexCommand label
name "sec:有依赖的背包问题"
\end_inset
有依赖的背包问题
\end_layout
\begin_layout Subsection
简化的问题
\end_layout
\begin_layout Standard
这种背包问题的物品间存在某种“依赖”的关系。也就是说,物品
\begin_inset Formula $i$
\end_inset
依赖于物品
\begin_inset Formula $j$
\end_inset
,表示若选物品
\begin_inset Formula $i$
\end_inset
,则必须选物品
\begin_inset Formula $j$
\end_inset
。为了简化起见,我们先设没有某个物品既依赖于别的物品,又被别的物品所依赖;另外,没有某件物品同时依赖多件物品。
\end_layout
\begin_layout Subsection
算法
\end_layout
\begin_layout Standard
这个问题由NOIP2006中“金明的预算方案”一题扩展而来。遵从该题的提法,将不依赖于别的物品的物品称为“主件”,依赖于某主件的物品称为“附件”。由这个问题的简
化条件可知所有的物品由若干主件和依赖于每个主件的一个附件集合组成。
\end_layout
\begin_layout Standard
按照背包问题的一般思路,仅考虑一个主件和它的附件集合。可是,可用的策略非常多,包括:一个也不选,仅选择主件,选择主件后再选择一个附件,选择主件后再选择两个附件…
…无法用状态转移方程来表示如此多的策略。事实上,设有
\begin_inset Formula $n$
\end_inset
个附件,则策略有
\begin_inset Formula $2^{n}+1$
\end_inset
个,为指数级。
\end_layout
\begin_layout Standard
考虑到所有这些策略都是互斥的(也就是说,你只能选择一种策略),所以一个主件和它的附件集合实际上对应于
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:分组的背包问题"
\end_inset
中的一个物品组,每个选择了主件又选择了若干个附件的策略对应于这个物品组中的一个物品,其费用和价值都是这个策略中的物品的值的和。但仅仅是这一步转化并不能给出一个好
的算法,因为物品组中的物品还是像原问题的策略一样多。
\end_layout
\begin_layout Standard
再考虑对每组内的物品应用
\begin_inset CommandInset ref
LatexCommand ref
reference "sub:一个简单有效的优化"
\end_inset
中的优化。我们可以想到,对于第
\begin_inset Formula $k$
\end_inset
个物品组中的物品,所有费用相同的物品只留一个价值最大的,不影响结果。所以,可以对主件
\begin_inset Formula $k$
\end_inset
的“附件集合”先进行一次01背包,得到费用依次为
\begin_inset Formula $0\text{\ldots}V-C_{k}$
\end_inset
所有这些值时相应的最大价值
\begin_inset Formula $F_{k}[0\ldots V-C_{k}]$
\end_inset
。那么,这个主件及它的附件集合相当于
\begin_inset Formula $V-C_{k}+1$
\end_inset
个物品的物品组,其中费用为
\begin_inset Formula $v$
\end_inset
的物品的价值为
\begin_inset Formula $F_{k}[v-C_{k}]+W_{k}$
\end_inset
\begin_inset Formula $v$
\end_inset
的取值范围是
\begin_inset Formula $C_{k}\leq v\leq V$
\end_inset
\end_layout
\begin_layout Standard
也就是说,原来指数级的策略中,有很多策略都是冗余的,通过一次01背包后,将主件
\begin_inset Formula $k$
\end_inset
及其附件转化为
\begin_inset Formula $V-C_{k}+1$
\end_inset
个物品的物品组,就可以直接应用
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:分组的背包问题"
\end_inset
的算法解决问题了。
\end_layout
\begin_layout Subsection
较一般的问题
\end_layout
\begin_layout Standard
更一般的问题是:依赖关系以图论中“森林”
\begin_inset Foot
status open
\begin_layout Plain Layout
即多叉树的集合
\end_layout
\end_inset
的形式给出。也就是说,主件的附件仍然可以具有自己的附件集合。限制只是每个物品最多只依赖于一个物品(只有一个主件)且不出现循环依赖。
\end_layout
\begin_layout Standard
解决这个问题仍然可以用将每个主件及其附件集合转化为物品组的方式。唯一不同的是,由于附件可能还有附件,就不能将每个附件都看作一个一般的01背包中的物品了。若这个附
件也有附件集合,则它必定要被先转化为物品组,然后用分组的背包问题解出主件及其附件集合所对应的附件组中各个费用的附件所对应的价值。
\end_layout
\begin_layout Standard
事实上,这是一种树形动态规划,其特点是,在用动态规划求每个父节点的属性之前,需要对它的各个儿子的属性进行一次动态规划式的求值。这已经触及到了“泛化物品”的思想。
看完
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:泛化物品"
\end_inset
后,你会发现这个“依赖关系树”每一个子树都等价于一件泛化物品,求某节点为根的子树对应的泛化物品相当于求其所有儿子的对应的泛化物品之和。
\end_layout
\begin_layout Subsection
小结
\end_layout
\begin_layout Standard
NOIP2006的那道背包问题我做得很失败,写了上百行的代码,却一分未得。后来我通过思考发现通过引入“物品组”和“依赖”的概念可以加深对这题的理解,还可以解决它
的推广问题。用物品组的思想考虑那题中极其特殊的依赖关系:物品不能既作主件又作附件,每个主件最多有两个附件,可以发现一个主件和它的两个附件等价于一个由四个物品组成
的物品组,这便揭示了问题的某种本质。
\end_layout
\begin_layout Standard
后来,我在《背包问题九讲》第一版中总结此事时说:“失败不是什么丢人的事情,从失败中全无收获才是。”之后的NOIP2007的比赛中,我得了满分。
\end_layout
\begin_layout Section
\begin_inset CommandInset label
LatexCommand label
name "sec:泛化物品"
\end_inset
泛化物品
\end_layout
\begin_layout Subsection
定义
\end_layout
\begin_layout Standard
考虑这样一种物品,它并没有固定的费用和价值,而是它的价值随着你分配给它的费用而变化。这就是泛化物品的概念。
\end_layout
\begin_layout Standard
更严格的定义之。在背包容量为
\begin_inset Formula $V$
\end_inset
的背包问题中,泛化物品是一个定义域为
\begin_inset Formula $0\ldots V$
\end_inset
中的整数的函数
\begin_inset Formula $h$
\end_inset
,当分配给它的费用为
\begin_inset Formula $v$
\end_inset
时,能得到的价值就是
\begin_inset Formula $h(v)$
\end_inset
\end_layout
\begin_layout Standard
这个定义有一点点抽象,另一种理解是一个泛化物品就是一个数组
\begin_inset Formula $h[0\ldots V]$
\end_inset
,给它费用
\begin_inset Formula $v$
\end_inset
,可得到价值
\begin_inset Formula $h[v]$
\end_inset
\end_layout
\begin_layout Standard
一个费用为
\begin_inset Formula $c$
\end_inset
价值为
\begin_inset Formula $w$
\end_inset
的物品,如果它是01背包中的物品,那么把它看成泛化物品,它就是除了
\begin_inset Formula $h(c)=w$
\end_inset
外,其它函数值都为
\begin_inset Formula $0$
\end_inset
的一个函数。如果它是完全背包中的物品,那么它可以看成这样一个函数,仅当
\begin_inset Formula $v$
\end_inset
\begin_inset Formula $c$
\end_inset
整除时有
\begin_inset Formula $h(v)=w\cdot\frac{v}{c}$
\end_inset
,其它函数值均为
\begin_inset Formula $0$
\end_inset
。如果它是多重背包中重复次数最多为
\begin_inset Formula $m$
\end_inset
的物品,那么它对应的泛化物品的函数有
\begin_inset Formula $h(v)=w\cdot\frac{v}{c}$
\end_inset
仅当
\begin_inset Formula $v$
\end_inset
\begin_inset Formula $c$
\end_inset
整除且
\begin_inset Formula $\frac{v}{c}\leq m$
\end_inset
,其它情况函数值均为
\begin_inset Formula $0$
\end_inset
\end_layout
\begin_layout Standard
一个物品组可以看作一个泛化物品
\begin_inset Formula $h$
\end_inset
。对于一个
\begin_inset Formula $0\ldots V$
\end_inset
中的
\begin_inset Formula $v$
\end_inset
,若物品组中不存在费用为
\begin_inset Formula $v$
\end_inset
的物品,则
\begin_inset Formula $h(v)=0$
\end_inset
,否则
\begin_inset Formula $h(v)$
\end_inset
取值为所有费用为
\begin_inset Formula $v$
\end_inset
的物品的最大价值。
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:分组的背包问题"
\end_inset
中每个主件及其附件集合等价于一个物品组,自然也可看作一个泛化物品。
\end_layout
\begin_layout Subsection
泛化物品的和
\end_layout
\begin_layout Standard
如果给定了两个泛化物品
\begin_inset Formula $h$
\end_inset
\begin_inset Formula $l$
\end_inset
,要用一定的费用从这两个泛化物品中得到最大的价值,这个问题怎么求呢?事实上,对于一个给定的费用
\begin_inset Formula $v$
\end_inset
,只需枚举将这个费用如何分配给两个泛化物品就可以了。同样的,对于
\begin_inset Formula $0\text{\ldots}V$
\end_inset
中的每一个整数
\begin_inset Formula $v$
\end_inset
,可以求得费用
\begin_inset Formula $v$
\end_inset
分配到
\begin_inset Formula $h$
\end_inset
\begin_inset Formula $l$
\end_inset
中的最大价值
\begin_inset Formula $f(v)$
\end_inset
。也即
\end_layout
\begin_layout Standard
\begin_inset Formula
\[
f(v)=\mathrm{max}\{h(k)+l(v-k)\:|\:0\leq k\leq v\}
\]
\end_inset
\end_layout
\begin_layout Standard
可以看到,这里的
\begin_inset Formula $f$
\end_inset
是一个由泛化物品
\begin_inset Formula $h$
\end_inset
\begin_inset Formula $l$
\end_inset
决定的定义域为
\begin_inset Formula $0\text{\ldots V}$
\end_inset
的函数,也就是说,
\begin_inset Formula $f$
\end_inset
是一个由泛化物品
\begin_inset Formula $h$
\end_inset
\begin_inset Formula $l$
\end_inset
决定的泛化物品。
\end_layout
\begin_layout Standard
我们将
\begin_inset Formula $f$
\end_inset
定义为泛化物品
\begin_inset Formula $h$
\end_inset
\begin_inset Formula $l$
\end_inset
的和:
\begin_inset Formula $h$
\end_inset
\begin_inset Formula $l$
\end_inset
都是泛化物品,若函数
\begin_inset Formula $f$
\end_inset
满足以上关系式,则称
\begin_inset Formula $f$
\end_inset
\begin_inset Formula $h$
\end_inset
\begin_inset Formula $l$
\end_inset
的和。泛化物品和运算的时间复杂度取决于背包的容量,是
\begin_inset Formula $O(V^{2})$
\end_inset
\end_layout
\begin_layout Standard
由泛化物品的定义可知:在一个背包问题中,若将两个泛化物品代以它们的和,不影响问题的答案。事实上,对于其中的物品都是泛化物品的背包问题,求它的答案的过程也就是求所
有这些泛化物品之和的过程。若问题的和为
\begin_inset Formula $s$
\end_inset
,则答案就是
\begin_inset Formula $s\text{(}0\ldots V)$
\end_inset
中的最大值。
\end_layout
\begin_layout Subsection
背包问题的泛化物品
\end_layout
\begin_layout Standard
一个背包问题中,可能会给出很多条件,包括每种物品的费用、价值等属性,物品之间的分组、依赖等关系等。但肯定能将问题对应于某个泛化物品。也就是说,给定了所有条件以后
,就可以对每个非负整数
\begin_inset Formula $v$
\end_inset
求得:若背包容量为
\begin_inset Formula $v$
\end_inset
,将物品装入背包可得到的最大价值是多少,这可以认为是定义在非负整数集上的一件泛化物品。这个泛化物品——或者说问题所对应的一个定义域为非负整数的函数——包含了关于
问题本身的高度浓缩的信息。一般而言,求得这个泛化物品的一个子定义域(例如
\begin_inset Formula $0\text{\ldots}V$
\end_inset
)的值之后,就可以根据这个函数的取值得到背包问题的最终答案。
\end_layout
\begin_layout Standard
综上所述,一般而言,求解背包问题,即求解这个问题所对应的一个函数,即该问题的泛化物品。而求解某个泛化物品的一种常用方法就是将它表示为若干泛化物品的和然后求之。
\end_layout
\begin_layout Subsection
小结
\end_layout
\begin_layout Standard
本讲是我在学习函数式编程的Scheme语言时,用函数编程的眼光审视各类背包问题得出的理论。
\end_layout
\begin_layout Standard
我想说:“思考”是一个程序员最重要的品质。简单的问题,深入思考以后,也能发现更多。
\end_layout
\begin_layout Section
背包问题问法的变化
\end_layout
\begin_layout Standard
以上涉及的各种背包问题都是要求在背包容量(费用)的限制下求可以取到的最大价值,但背包问题还有很多种灵活的问法,在这里值得提一下。但是我认为,只要深入理解了求背包
问题最大价值的方法,即使问法变化了,也是不难想出算法的。
\end_layout
\begin_layout Standard
例如,求解最多可以放多少件物品或者最多可以装满多少背包的空间。这都可以根据具体问题利用前面的方程求出所有状态的值(
\begin_inset Formula $F$
\end_inset
数组)之后得到。
\end_layout
\begin_layout Standard
还有,如果要求的是“总价值最小”“总件数最小”,只需将状态转移方程中的
\begin_inset Formula $\mathrm{max}$
\end_inset
改成
\begin_inset Formula $\mathrm{min}$
\end_inset
即可。
\end_layout
\begin_layout Standard
下面说一些变化更大的问法。
\end_layout
\begin_layout Subsection
输出方案
\end_layout
\begin_layout Standard
一般而言,背包问题是要求一个最优值,如果要求输出这个最优值的方案,可以参照一般动态规划问题输出方案的方法:记录下每个状态的最优值是由状态转移方程的哪一项推出来的
,换句话说,记录下它是由哪一个策略推出来的。便可根据这条策略找到上一个状态,从上一个状态接着向前推即可。
\end_layout
\begin_layout Standard
还是以01背包为例,方程为
\begin_inset Formula $F[i,v]=\mathrm{max}\{F[i-1,v],F[i-1,v-C_{i}]+W_{i}\}$
\end_inset
。再用一个数组
\begin_inset Formula $G[i,v]$
\end_inset
,设
\begin_inset Formula $G[i,v]=0$
\end_inset
表示推出
\begin_inset Formula $F[i,v]$
\end_inset
的值时是采用了方程的前一项(也即
\begin_inset Formula $F[i,v]=F[i-1,v]$
\end_inset
),
\begin_inset Formula $G[i,v]=1$
\end_inset
表示采用了方程的后一项。注意这两项分别表示了两种策略:未选第
\begin_inset Formula $i$
\end_inset
个物品及选了第
\begin_inset Formula $i$
\end_inset
个物品。那么输出方案的伪代码可以这样写(设最终状态为
\begin_inset Formula $F[N,V]$
\end_inset
):
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $i\text{\,\leftarrow}N$
\end_inset
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $v\,\leftarrow V$
\end_inset
\end_layout
\begin_layout LyX-Code
while
\begin_inset Formula $i>0$
\end_inset
\end_layout
\begin_layout LyX-Code
if
\begin_inset Formula $G[i,v]=0$
\end_inset
\end_layout
\begin_layout LyX-Code
print 未选第
\begin_inset Formula $i$
\end_inset
项物品
\end_layout
\begin_layout LyX-Code
else if
\begin_inset Formula $G[i,v]=1$
\end_inset
\end_layout
\begin_layout LyX-Code
print
\begin_inset Formula $ $
\end_inset
选了第
\begin_inset Formula $i$
\end_inset
项物品
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $v\,\leftarrow sv-C_{i}$
\end_inset
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $i\,\leftarrow i-1$
\end_inset
\end_layout
\begin_layout Standard
另外,采用方程的前一项或后一项也可以在输出方案的过程中根据
\begin_inset Formula $F[i,v]$
\end_inset
的值实时地求出来。也即,不须纪录
\begin_inset Formula $G$
\end_inset
数组,将上述代码中的
\begin_inset Formula $G[i,v]=0$
\end_inset
改成
\begin_inset Formula $F[i,v]=F[i-1,v]$
\end_inset
\begin_inset Formula $G[i,v]=1$
\end_inset
改成
\begin_inset Formula $F[i,v]=F[i-1][v-C_{i}]+W_{i}$
\end_inset
也可。
\end_layout
\begin_layout Subsection
输出字典序最小的最优方案
\end_layout
\begin_layout Standard
这里“字典序最小”的意思是
\begin_inset Formula $1\ldots N$
\end_inset
号物品的选择方案排列出来以后字典序最小。以输出01背包最小字典序的方案为例。
\end_layout
\begin_layout Standard
一般而言,求一个字典序最小的最优方案,只需要在转移时注意策略。
\end_layout
\begin_layout Standard
首先,子问题的定义要略改一些。我们注意到,如果存在一个选了物品
\begin_inset Formula $1$
\end_inset
的最优方案,那么答案一定包含物品
\begin_inset Formula $1$
\end_inset
,原问题转化为一个背包容量为
\begin_inset Formula $V-C_{1}$
\end_inset
,物品为
\begin_inset Formula $2\ldots N$
\end_inset
的子问题。反之,如果答案不包含物品
\begin_inset Formula $1$
\end_inset
,则转化成背包容量仍为
\begin_inset Formula $V$
\end_inset
,物品为
\begin_inset Formula $2\ldots N$
\end_inset
的子问题。
\end_layout
\begin_layout Standard
不管答案怎样,子问题的物品都是以
\begin_inset Formula $i\ldots N$
\end_inset
而非前所述的
\begin_inset Formula $1\ldots i$
\end_inset
的形式来定义的,所以状态的定义和转移方程都需要改一下。
\end_layout
\begin_layout Standard
但也许更简易的方法是,先把物品编号做
\begin_inset Formula $x\,\leftarrow N+1-x$
\end_inset
的变换,在输出方案时再变换回来。在做完物品编号的变换后,可以按照前面经典的转移方程来求值。只是在输出方案时要注意,如果
\begin_inset Formula $F[i,v]=F[i-1,v]$
\end_inset
\begin_inset Formula $F[i,v]=F[i-1][v-C_{i}]+W_{i}$
\end_inset
都成立,应该按照后者来输出方案,即选择了物品
\begin_inset Formula $i$
\end_inset
,输出其原来的编号
\begin_inset Formula $N-1-i$
\end_inset
\end_layout
\begin_layout Subsection
求方案总数
\end_layout
\begin_layout Standard
对于一个给定了背包容量、物品费用、物品间相互关系(分组、依赖等)的背包问题,除了再给定每个物品的价值后求可得到的最大价值外,还可以得到装满背包或将背包装至某一指
定容量的方案总数。
\end_layout
\begin_layout Standard
对于这类改变问法的问题,一般只需将状态转移方程中的
\begin_inset Formula $\mathrm{max}$
\end_inset
改成
\begin_inset Formula $\mathrm{sum}$
\end_inset
即可。例如若每件物品均是完全背包中的物品,转移方程即为
\end_layout
\begin_layout Standard
\begin_inset Formula
\[
F[i,v]=\mathrm{sum}\{{F[i-1,v],F[i,v-C_{i}]}\}
\]
\end_inset
\end_layout
\begin_layout Standard
初始条件是
\begin_inset Formula $F[0,0]=1$
\end_inset
\end_layout
\begin_layout Standard
事实上,这样做可行的原因在于状态转移方程已经考察了所有可能的背包组成方案。
\end_layout
\begin_layout Subsection
最优方案的总数
\end_layout
\begin_layout Standard
这里的最优方案是指物品总价值最大的方案。以01背包为例。
\end_layout
\begin_layout Standard
结合求最大总价值和方案总数两个问题的思路,最优方案的总数可以这样求:
\begin_inset Formula $F[i,v]$
\end_inset
代表该状态的最大价值,
\begin_inset Formula $G[i,v]$
\end_inset
表示这个子问题的最优方案的总数,则在求
\begin_inset Formula $F[i,v]$
\end_inset
的同时求
\begin_inset Formula $G[i,v]$
\end_inset
的伪代码如下:
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $G[0,0]\,\leftarrow1$
\end_inset
\end_layout
\begin_layout LyX-Code
for
\begin_inset Formula $i\,\leftarrow1$
\end_inset
to
\begin_inset Formula $N$
\end_inset
\end_layout
\begin_layout LyX-Code
for
\begin_inset Formula $v\,\leftarrow0$
\end_inset
to
\begin_inset Formula $V$
\end_inset
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $F[i,v]\,\leftarrow\mathrm{max}\{F[i-1,v],F[i-1,v-C_{i}]+W_{i}\}$
\end_inset
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $G[i,v]\,\leftarrow0$
\end_inset
\end_layout
\begin_layout LyX-Code
if
\family roman
\series medium
\shape up
\size normal
\emph off
\bar no
\strikeout off
\uuline off
\uwave off
\noun off
\color none
\lang english
\begin_inset Formula $F[i,v]=F[i-1,v]$
\end_inset
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $G[i,v]\,\leftarrow G[i,v]+G[i-1][v]$
\end_inset
\end_layout
\begin_layout LyX-Code
if
\begin_inset Formula $F[i,v]=F[i-1,v-C_{i}]+W_{i}$
\end_inset
\end_layout
\begin_layout LyX-Code
\begin_inset Formula $G[i,v]\,\leftarrow G[i,v]+G[i-1][v-C_{i}]$
\end_inset
\end_layout
\begin_layout Standard
如果你是第一次看到这样的问题,请仔细体会上面的伪代码。
\end_layout
\begin_layout Subsection
求次优解、第
\begin_inset Formula $K$
\end_inset
优解
\end_layout
\begin_layout Standard
对于求次优解、第
\begin_inset Formula $K$
\end_inset
优解类的问题,如果相应的最优解问题能写出状态转移方程、用动态规划解决,那么求次优解往往可以相同的复杂度解决,第
\begin_inset Formula $K$
\end_inset
优解则比求最优解的复杂度上多一个系数
\begin_inset Formula $K$
\end_inset
\end_layout
\begin_layout Standard
其基本思想是,将每个状态都表示成有序队列,将状态转移方程中的
\begin_inset Formula $\mathrm{max/min}$
\end_inset
转化成有序队列的合并。
\end_layout
\begin_layout Standard
这里仍然以01背包为例讲解一下。
\end_layout
\begin_layout Standard
首先看01背包求最优解的状态转移方程:
\begin_inset Formula $F[i,v]=\mathrm{max}\{F[i-1,v],F[i-1,v-C_{i}]+W_{i}\}$
\end_inset
。如果要求第
\begin_inset Formula $K$
\end_inset
优解,那么状态
\begin_inset Formula $F[i,v]$
\end_inset
就应该是一个大小为
\begin_inset Formula $K$
\end_inset
的队列
\begin_inset Formula $F[i,v,1\ldots K]$
\end_inset
。其中
\begin_inset Formula $F[i,v,k]$
\end_inset
表示前
\begin_inset Formula $i$
\end_inset
个物品中,背包大小为
\begin_inset Formula $v$
\end_inset
时,第
\begin_inset Formula $k$
\end_inset
优解的值。这里也可以简单地理解为在原来的方程中加了一维来表示结果的优先次序。显然
\begin_inset Formula $f[i,v,1\ldots K]$
\end_inset
\begin_inset Formula $K$
\end_inset
个数是由大到小排列的,所以它可看作是一个有序队列。
\end_layout
\begin_layout Standard
然后原方程就可以解释为:
\begin_inset Formula $F[i,v]$
\end_inset
这个有序队列是由
\begin_inset Formula $F[i-1,v]$
\end_inset
\begin_inset Formula $F[i-1\text{,}v-C_{i}]+W_{i}$
\end_inset
这两个有序队列合并得到的。前者
\begin_inset Formula $F[i-1][V]$
\end_inset
\begin_inset Formula $F[i-1,v,1\ldots K]$
\end_inset
,后者
\begin_inset Formula $F[i-1\text{,}v-C_{i}]+W_{i}$
\end_inset
则理解为在
\begin_inset Formula $F[i-1\text{,}v-C_{i},1\ldots K]$
\end_inset
的每个数上加上
\begin_inset Formula $W_{i}$
\end_inset
后得到的有序队列。合并这两个有序队列并将结果的前
\begin_inset Formula $K$
\end_inset
项储存到
\begin_inset Formula $f[i,v,1\ldots K]$
\end_inset
中的复杂度是
\begin_inset Formula $O(K)$
\end_inset
。最后的第
\begin_inset Formula $K$
\end_inset
优解的答案是
\begin_inset Formula $F[N,V,K]$
\end_inset
。总的时间复杂度是
\begin_inset Formula $O(VNK)$
\end_inset
\end_layout
\begin_layout Standard
为什么这个方法正确呢?实际上,一个正确的状态转移方程的求解过程遍历了所有可用的策略,也就覆盖了问题的所有方案。只不过由于是求最优解,所以其它在任何一个策略上达不
到最优的方案都被忽略了。如果把每个状态表示成一个大小为
\begin_inset Formula $K$
\end_inset
的数组,并在这个数组中有序地保存该状态可取到的前
\begin_inset Formula $K$
\end_inset
个最优值。那么,对于任两个状态的
\begin_inset Formula $\mathrm{max}$
\end_inset
运算等价于两个由大到小的有序队列的合并。
\end_layout
\begin_layout Standard
另外还要注意题目对于“第
\begin_inset Formula $K$
\end_inset
优解”的定义,是要求将策略不同但权值相同的两个方案是看作同一个解还是不同的解。如果是前者,则维护有序队列时要保证队列里的数没有重复的。
\end_layout
\begin_layout Subsection
小结
\end_layout
\begin_layout Standard
显然,这里不可能穷尽背包类动态规划问题所有的问法。甚至还存在一类将背包类动态规划问题与其它领域(例如数论、图论)结合起来的问题,在这篇论背包问题的专文中也不会论
及。但只要深刻领会前述所有类别的背包问题的思路和状态转移方程,遇到其它的变形问题,应该也不难想出算法。
\end_layout
\begin_layout Standard
触类旁通、举一反三,应该也是一个程序员应有的品质吧。
\end_layout
\end_body
\end_document
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lv-peidong/DP-Book.git
git@gitee.com:lv-peidong/DP-Book.git
lv-peidong
DP-Book
DP-Book
master

搜索帮助