[鑳屽寘涔濊1]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[鑳屽寘涔濊1]相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%5b%5d' title='[]'>[]
ima put 瑙掑害 瀹屾垚 娌℃湁 鏀硅繘 amount 鐩存帴P1.01鍩虹鑳屽寘闂
瀵逛簬N涓疂鐭筹紝姣忎釜瀹濈煶鐨勪环鍊间负vi锛岄噸閲忚姳璐逛负wi銆傝儗鍖呯殑鎬昏浇閲嶉噺涓篧锛屽垯璇曢棶瀵逛簬涓€涓儗鍖呰繖涔堟斁瀹濈煶鎵嶈兘浣垮叾瑁呯殑瀹濈煶鎬讳环鍊兼渶澶с€?
鍏蜂綋鎬濊矾锛氳€冭檻鐘舵€侊紝鍒╃敤i琛ㄧず绗琲涓疂鐭筹紝j琛ㄧず褰撳墠鑳屽寘鐨勫凡鐢ㄧ┖闂达紝d[i][j]灏卞彲浠ヨ〃绀哄綋鍓嶇姸鍐典笅鑳屽寘鍐呭疂鐭崇殑鏈€澶т环鍊笺€傚垯瑕佹眰鐨勯棶棰樺彲浠ヨ浆鍖栦负d[N][W]鐨勬眰鍙栥€?
鐒跺悗鏋勫缓鐘舵€佽浆绉绘柟绋嬶細
d[i][j]=max{d[i−1][j],d[i−1][j−w[i]]+v[i]}d[i][j]=max{d[i−1][j],d[i−1][j−w[i]]+v[i]}
鍊煎緱娉ㄦ剰鐨勬槸杩欓噷max鍐呭彧鏈変袱椤规槸鍥犱负涓€涓疂鐭冲彧鏈夐€夊拰涓嶉€変袱绉嶆儏鍐碉紝鎵€浠P鐘舵€佽浆绉绘柟绋嬬殑鎬濇兂灏辨槸锛?/p>
褰撳墠鐘舵€?= 鍙栨渶浼榹鍓嶄竴鐘舵€?锛屽墠涓€鐘舵€?…鍓嶄竴鐘舵€乶}
浠庝笂鍙互鐪嬪嚭鐘舵€佺殑閫夊彇寰堥噸瑕侊紝骞朵笖鐢变簬鏄€氳繃灏嗘渶缁堥棶棰樹笉鏂垎涓哄墠涓€绉嶇姸鎬佷笅鐨勬渶浼樿В锛堝嵆鏈€浼樺瓙缁撴瀯锛夛紝鎵€浠ヨ閲囩敤鑷簳鍚戜笂鐨勬柟寮忚绠楋紝鍗冲厛璁$畻鏈€灏忛棶棰樺崟鍏冿紝骞惰褰曪紝鐒跺悗璁$畻鍚庝竴绾ч棶棰樻椂灏卞彲浠ヤ互鐩存帴璋冪敤鑺傜害鏃堕棿銆傛墍浠ユ椂鑷簳鍚戜笂銆備篃灏辨槸瀵硅В绌洪棿鏍戜粠璁$畻鍙跺瓙闂寮€濮嬮€愭鎺ㄥ悜涓婚棶棰樸€?
浠旂粏鑰冭檻杩欎竴杩囩▼涔熷彲浠ュ彂鐜帮紝鍦ㄩ€愭涓婃帹鐨勮繃绋嬩腑锛屾瘝闂鐨勫喅绛栨柟妗堟槸涓嶄細瀵逛箣鍓嶇殑瀛愰棶棰橀€犳垚褰卞搷鐨勶紝鍥犳杩欏氨鏄棤鍚庢晥鎬х殑琛ㄧ幇銆傦紙鍙跺瓙闂鐨勫喅绛?>瀛愰棶棰?鐨勫喅绛?>…->瀛愰棶棰榥鐨勫喅绛?>姣嶉棶棰樼殑鍐崇瓥锛?
浼唬鐮佷负锛?/p>
d[0][0...W] = 0;
for i = 1->N
for j = 0->W
if(w[i] > j)
d[i][j] =d[i-1][j]
else
d[i][j] = max{d[i-1][j],d[i-1][j-w[i]]+v[i]}
鍥句腑闈掕壊浠h〃d[2][5]锛屽彲浠ョ湅鍑轰粬鐢辩豢鑹茬殑涓や釜鍚勭涓殑鏁板€煎緱鍑恒€傚嵆褰撳墠鐘舵€佺敱鍓嶄竴鐘舵€佺殑涓や釜瑙d腑鐨勬渶浼樿В寰楀嚭銆傛帴涓嬫潵榛勮壊鐨勪笁涓湀涔熶竴鏍枫€傚洜姝P鐨勮矾绾垮氨鏄粠宸︿笂瑙掍笉鏂绠楀緱鍒板彸涓嬭鐨勬渶缁堥棶棰樼殑瑙c€傝娉ㄦ剰鐨勬槸DP鏈変釜璧嬩簣鍒濆€肩殑姝ラ銆?/p>
闂浼樺寲
绌洪棿鐨勪紭鍖?/h3>
浠庤繖寮犲浘閲岄潰杩樺彲浠ョ湅鍑猴紝姣忎釜瀹濈煶i鐨勯€夋嫨锛屽叾瀹炲彧闇€瑕佷笂涓€娆$殑i-1鏃跺€欑殑閫夋嫨鎯呭喌銆傚洜姝ゅ叾瀹炲彲浠ョ敤涓€涓竴缁存暟缁勮€屼笉鏄簩缁存暟缁勮〃绀恒€?nbsp;
鍏堢湅浼唬鐮?/p>
d[0...W] = 0;
for i = 1->N
for j = W->0 //杩欓噷鏄敮涓€鐨勪笉鍚?br /> if(w[i] > j)
d[j] =d[j]
else
d[j] = max{d[j],d[j-w[i]]+v[i]}
浼唬鐮佸彧鏄绗簩涓猣or寰幆鐨勯『搴忓仛浜嗘敼鍙樸€傚嵆鏀逛负浜嗕粠W鍒?锛岀劧鍚庡氨鎶婃暟缁勬崲鎴愪簡涓€缁存暟缁勩€?nbsp;
缁撳悎涓婂浘鎴戜滑鍙互鐪嬩笅鍙戠敓浜嗕粈涔堛€傞偅涓竴缁存暟缁勫唴瀛樺彇鐨勪笢瑗垮鍥剧孩鑹查儴鍒嗐€傛槸涓や釜鐘舵€佺殑缁撳悎銆傚彲浠ョ湅鍑猴紝褰撴垜瑕佹眰d[2][6]鏃讹紝鎴戦渶瑕佺殑d[1][6]鍜宒[1][1],杞崲鍒颁竴缁存暟缁勬椂锛屽氨鏄痙[6]鍜宒[1]锛岀劧鍚庣畻鍑烘潵鏂扮殑d[6]瀵瑰師鏁扮粍杩涜瑕嗙洊銆傝繖鏍峰氨瀹屾垚浜嗙┖闂寸殑鍘嬬缉
鍐嶈繘涓€姝?/h2>
涓婁竴鑺傛槸浠庡乏杈硅€冭檻閬嶅巻鐨勮妭鐪佺晫闄愩€傚嵆鍘绘帀浠?寮€濮嬪埌w[i]鐨勯儴鍒嗐€?
杩欎竴鑺傛槸浠庡彸杈硅€冭檻閬嶅巻鐨勮妭鐪佺晫闄愩€?
鑰冭檻瀵逛簬缁撴灉绗琻涓疂鐭筹紝鍏惰€冭檻鐨勫氨鏄痙[W]椤癸紝閭e叾鍓嶄竴椤癸紝鍏跺疄鍙渶瑕佹彁渚涗粠W鍒癢-w[n]椤瑰嵆鍙€傚涓嬪浘钃濊壊椤广€傚洜姝わ紝瀵逛簬绗琲涓疂鐭虫椂锛屾垜浠寚鎺ц€冭檻鐨刣[j]椤圭殑鑼冨洿涓猴細W鍒癿ax{w[i],W−sum(w[i],w[i+1],...,w[n])}W鍒癿ax{w[i],W−sum(w[i],w[i+1],...,w[n])}
d[0...W] = 0; for i = 1->N for j = W->max{w[i],W-sum(w[i->n])} d[j] = max{d[j],d[j-w[i]]+v[i]}
public void zeroOnePack(w[i], v[i]){
for j = W->max{w[i],W-sum(w[i->n])}
d[j] = max{d[j],d[j-w[i]]+v[i]}
}
P2.瀹屽叏鑳屽寘闂
鍋囪嫢姣忕瀹濈煶鐨勬暟閲忎笉璁句笂闄愶紝鍒欓棶棰樿浆鍙樹负瀹屽叏鑳屽寘闂銆?nbsp;
璇ラ棶棰樼殑姹傝В鏈変袱绉嶆柟娉曪紝涓€涓槸鍩虹鏂规硶锛屼竴涓娊璞℃柟娉曘€?/p>
鍩虹鏂规硶
鍏跺疄璇存槸鏃犻檺锛岃繕鏄湁闄愮殑锛屽嵆涓€绉嶅疂鐭虫暟閲忚偗瀹氬皬浜庤儗鍖呭閲廣/w[i]
鑰冭檻瀛愮姸鎬侊紝鏈€鐩存帴鐨勫氨鏄?/p>
d[i][j]=max{d[i−1][j−k∗w[i]]+k∗v[i]|0≤k≤W/w[i]}d[i][j]=max{d[i−1][j−k∗w[i]]+k∗v[i]|0≤k≤W/w[i]}
鍗冲綋鍓嶇姸鎬?= 鍙栨渶浼榹鍓嶄竴鐘舵€?锛屽墠涓€鐘舵€?…鍓嶄竴鐘舵€乶}
鏀硅繘鏂规硶鏄紝灏嗕竴绉嶅疂鐭硁i涓湅鎴恘i涓粺涓€瑙勬牸鐨勫疂鐭筹紝鍒欐€诲叡鏈塻um(n1, n2, … , nn)涓疂鐭筹紝澶у惊鐜负
for i = 1 -> sum(n1, n2, … , nn)
杩欎釜鏂规硶鍙槸鎹簡涓搴︾湅闂锛屽苟娌℃湁闄嶄綆浠d环銆備笉杩囦粠杩欎釜瑙掑害鑰冭檻锛屽叾瀹炴垜浠彲浠ヨ€冭檻瀵瑰悓涓€绉嶅疂鐭筹紝鎴戜滑鎷跨殑涓暟鍙互绛変环涓轰竴棰楀ぇ瀹濈煶锛屾瘮濡傦紝鍙?棰楀疂鐭砳鍒欑浉褰撲簬鍙栦簡涓€棰椾腑2*w[i],浠峰€?*v[i]鐨勫ぇ瀹濈煶銆?
鐜板湪鐨勬儏鍐靛氨鏄浣曟瀯寤哄悇绉嶅ぇ瀹濈煶鏉ヤ繚璇佸彇鐨勬椂鍊欎笉浼氶噸澶嶏紝杩欐椂鍊欏彲浠ヨ€冭檻璁$畻鏈虹殑浜岃繘鍒跺瓨鍌ㄦ柟寮?鍙鏈?鐨?锛?锛?hellip;锛宯娆℃柟鐨勬暟锛屽氨鍙互缁勫悎鍑轰换鎰?鍒?鐨刵娆℃柟涔嬮棿鐨勬暟)銆傛瀯寤虹殑澶у疂鐭充环鍊煎簲璇ヤ负1棰梚锛?棰梚锛?棰梚銆傘€傘€?鐨刱娆℃柟棰梚銆傝繖鏍凤紝鎴戝彧瑕佸杩欎簺鏋勯€犵殑澶у疂鐭宠繘琛屾槸鍚﹁鍏ュ垽鏂紝灏卞彲浠ュ垽鏂嚭鏈€浼樿В鏄鍑犻瀹濈煶i锛屽叾涓璳瑕佹弧瓒硍[i]∗2k≤Ww[i]∗2k≤W銆?
浼唬鐮?/p>
for i = 1->N
k = 1
while(k <= W/w[i])
zeroOnePack(k*w[i],k*v[i])
k = k*2
鍏跺疄涓婂紡鏄湁鍐椾綑鐨勶紝鍗筹紝k涓嶇敤鍙栭偅涔堝ぇ锛屽彧瑕佷繚璇?锛?锛?锛?hellip;锛宬鐨勫疂鐭冲姞璧锋潵鎬荤殑涓暟鏄皬浜嶹/w[i]鐨勬渶澶у€煎氨鍙互锛岃€屼笉鐢ㄩ潪瑕佺k涓槸灏忎簬W/w[i]鐨勬渶澶у€笺€傚洜姝ゆ瀯寤烘柊鐨刱鐨勮竟鐣屽簲璇ユ槸sum(1,2,4,…,k)<=W/w[i]鏃剁殑鏈€澶у€笺€傚苟涓斾负浜嗕繚璇佹€诲拰涓篧/w[i]锛岃繕瑕佸啀琛ヤ笂涓€棰楋紙W/w[i]-sum(1,2,4,…,k)锛?w[i]鐨勫ぇ瀹濈煶銆?
浼爜濡備笅锛?/p>
for i = 1->N
k = 1
amount = W/w[i]
while(k <= amount)
zeroOnePack(k*w[i],k*v[i])
amount = amount - k
k = k*2
zeroOnePack(k*w[i],k*v[i])
鎶借薄鏂规硶—鏇村揩
杩樻槸杩欏紶鍥撅紝鍙互鐪嬪嚭锛屽綋鍒濅负浜嗕繚璇佹槸浠巇[i-1][j],d[i-1][j-w[i]]鎺ㄥ嚭d[i][j]锛屾垜浠噰鐢ㄤ粠W鍒皐[i]鐨勬柟寮忥紝鍏剁洰鐨勫氨鏄繚璇佷笉浼氫骇鐢熶粠d[i][0]鎺ㄥ嚭[i][j]鐨勬儏鍐碉紝鐜板湪锛屾棦鐒舵槸涓€涓疂鐭砳瑕佹湁鏃犻檺涓紝閭d箞鏄庢樉鍙互閲囩敤浠巇[i][0]鎺ㄥ嚭[i][j]鐨勬儏鍐碉紝鎵€浠ョ粰鍑虹殑for寰幆鏄粠0->W.
浼唬鐮佸涓嬶細
d[0...W] = 0;
for i = 1->N
for j = 0->W
if(w[i] > j)
d[j] =d[j]
else
d[j] = max{d[j],d[j-w[i]]+v[i]}
鎴戜滑鍙互瀹氫箟璇ヨ繃绋嬩负涓€涓柊鏂规硶锛?/p>
public void completePack(w[i], v[i]){
for j = 0->W
if(w[i] > j)
d[j] =d[j]
else
d[j] = max{d[j],d[j-w[i]]+v[i]}
}
P3.澶氶噸鑳屽寘
澶氶噸鑳屽寘鍦ㄨ繖閲屾寚鐨勬槸褰撳疂鐭砳鐨勪釜鏁扮粰瀹氫负ni鏃剁殑鑳屽寘闂銆傝繖閲屽彲浠ヨ€冭檻锛屽綋ni = 1鏃讹紝灏辨槸01鑳屽寘闂锛屽綋ni澶т簬W/w[i]鏃讹紝灏辨槸瀹屽叏鑳屽寘闂銆傚綋ni鍦ㄤ袱鑰呬箣闂存椂锛屽彲浠ヨ浆鎹㈡垚瀹屽叏鑳屽寘闂閲岄潰鐨勫熀纭€瑙f硶銆傚洜姝ゅ彲浠ユ瀯閫犲涓嬩吉浠g爜锛?/p>
public void multiplePack(w[i], v[i], amount)
if(amount > W/w[i])
completePack(w[i], v[i])
else{
k = 1
amount = W/w[i]
while(k <= amount)
zeroOnePack(k*w[i],k*v[i])
amount = amount - k
k = k*2
zeroOnePack(k*w[i],k*v[i])
}
P9.鑳屽寘闂硶鍙樺寲
鏋勯€犳渶浼樿В
鏋勯€犳渶浼樿В鐨勬柟娉曪紝鎴戝鍒扮殑鏈変袱绉嶃€?nbsp;
绗竴绉嶆槸閲囩敤01鍩虹鑳屽寘鏂规鏃讹紝寰楀埌浜哾[i][j]锛岃嫢浠 = N锛沯 = W锛涘垯鍙互鏍规嵁姹傚嚭鐨勪簩缁存暟缁勮繘琛岄€嗘帹姹傚嚭姣忎釜鏈€浼橀€夋嫨銆備吉浠g爜濡備笅锛?/p>
public getOptSolution(){
i = N
j = W
while(d[i][j] > d[0][j])
if d[i][j] == d[i-1][j]
娌℃湁閫夋嫨绗琲涓疂鐭?br /> i = i - 1
else
閫夋嫨浜嗙i涓疂鐭?br /> i = i - 1
j = j - w[i]
}
绗簩涓槸閲囩敤浜嗕竴缁存暟缁勫幓杩涜璁$畻鐨勬椂鍊欙紝鐗瑰埆鏄湪姹傚彇澶氶噸鑳屽寘鎴栬€呭畬鍏ㄨ儗鍖呴棶棰樻椂鐨勬瀯閫犳渶浼樿В鏂规硶锛屽叧閿氨鏄缓绔嬭緟鍔╃殑鏁扮粍杩涜甯繖璁板綍锛屼笉杩囩敱浜庡湪澶氶噸鑳屽寘闂鏃讹紝姣忕瀹濈煶鐨勫叿浣撳睍寮€澶у疂鐭虫暟閲忎簨鍏堣绠楁瘮杈冮夯鐑︼紝鍥犳鍙互閲囩敤鍔ㄦ€佹暟缁凙rrayList鏉ュ府蹇欍€?nbsp;
浼唬鐮侊細
//--------------------鏋勯€犺褰曠敤鍔ㄦ€佹暟缁勶紙鍦ㄥぇ寰幆涓級------------------
d[0->W] = 0
鍔ㄦ€佹暟缁剅ecorder
for i = 1->N
multiplePack(w[i], v[i], amount)
//--------------------鏀硅繘01鑳屽寘鏂规硶锛屽鍔犺褰曟暟缁勶紙鍦ㄥ皬寰幆涓級----------------
public void zeroOnePack(w[i], v[i]){
boolean[] rec
for j = W->w[i]
rec[j] = isPut(d[j],d[j-w[i]]+v[i]) //鏋勯€犲瓙鏂规硶鍒ゆ柇锛屽垽鏂緷鎹悓max
d[j] = max{d[j],d[j-w[i]]+v[i]}
recorder.add(rec)
//---------------------鏋勯€犳渶浼樿В-------------------------
鍚?getOptSolution()鏂规硶
琛ュ厖锛?
鍏跺疄鍙鐞嗚В澶氶噸鑳屽寘鐨勫熀纭€鏂规硶锛屾槸鍙互涓嶇敤鏋勯€犺褰曟暟缁勪篃鍙互瀹屾垚鏈€浼樿В鐨勬瀯閫犵殑銆備笉杩囦細姣旇緝楹荤儲銆傚湪姝ゅ啀鎻愬強涓€涓嬪閲嶈儗鍖呯殑鎬濇兂锛?
鍋囪鍘熸潵鏈塏绉嶅疂鐭?,2,..,N.姣忕鏈塶[i]涓?
鍒欑幇鍦ㄥ閲嶈儗鍖呯殑鍩虹鏂规硶鐨勬€濇兂鏄浆鎹㈡垚M绉嶅疂鐭筹紝鍒嗗埆涓?
锛坣[1],2*n[1],4*n[1],…,k[1]*n[1]锛?(n[2],2*n[2],4*n[2],…,k[2]*n[2]),…………….,(n[N],2*n[N],4*n[N],…,k[N]*n[N])鍗冲彲銆?/p>
以上是关于[鑳屽寘涔濊1]的主要内容,如果未能解决你的问题,请参考以下文章