—lŽ®‚X +-------------------------------------+----------------+----------------------+ | ¼½ÃÑ–¼(º°ÄÞ): ‚`‚k‚f‚n‚o‚`‚j | | ƒVƒXƒeƒ€•ñ‘ŠT—v | +-------------------------------------+ +----------------------+ | Ó¼Þ­°Ù–¼(º°ÄÞ): KNAPSAC(A701) | ײÌÞ×Ø-“o˜^”Ô†: | +-------------------------------------+---------------------------------------+ | 쬎ҥŠw¶”Ô†: ‘‘ò‘PŽq21SS1159 | •¶‘쬎ҥŠw¶”Ô†: ‘‘ò‘PŽq21SS1159 | +-------------------------------------+---------------------------------------+ | 쬊úŠÔ: 62/01/ [ / / | •¶‘쬊úŠÔ: 62/02/ [ / / | +-------------------------------------+---------------------------------------+ | eƒ‚ƒWƒ…[ƒ‹: ALGOS | Žqƒ‚ƒWƒ…[ƒ‹: | +-------------------------------------+---------------------------------------+ | u‹`–¼E‰Û‘è–¼: ‘²‹ÆŒ¤‹† ã | ŽQl•¶Œ£(’˜ŽÒ–¼A‘–¼Ao”ÅŽÐA”­s”N)| +-------------------------------------+ | | ŠJ”­Œ`‘Ô: ˆÚA | SARA BAASE 'COMPUTER ALGORITHMS' | +-------------------------------------+ | | Œ`Ž®: ƒTƒuƒ‹[ƒ`ƒ“ | ADDISON WESLEY 1978 | +-------------------------------------+---------------------------------------+ | —˜—p‘ÎÛŽÒ: ˆê”Ê | Ši”[ÒÃÞ¨±Œ`Ž®: ƒtƒƒbƒs[ƒfƒBƒXƒN | +-------------------------------------+---------------------------------------+ | –Ú“Ii–â‘è‰ðàA‹@”\‰ðà|§ŒÀŽ–€j| Ši”[ÒÃÞ¨±”Ô†A̧²Ù–¼: @@@@@@@ | | Œˆ‚ß‚ç‚ꂽd‚³‚ðŽ‚‰ו¨‚ð,Œˆ‚Ü‚Á +---------------------------------------+ | | ‹LqŒ¾ŒêE‘–s‚n‚r: TURBO PASCAL | | ‚½—e—Ê‚Ì ƒiƒbƒvƒTƒbƒN‚É‹l‚߂鎞‚Ì, +---------------------------------------+ | | ‘–sðŒiƒn[ƒhƒEƒFƒAAƒ\ƒtƒgƒEƒFƒAj| | ‰×•¨‚Ì‘g‚݇‚킹‚ð‹‚ß‚éƒTƒuƒ‹[ƒ` | PC-9801 | | | | | ƒ“B | MS-DOS | +-------------------------------------+---------------------------------------+ | ƒL[ƒ[ƒhi“K—p•ª–ìAŽè–@‚È‚Çj F | •ª—ÞƒR[ƒhi‚b‚qƒR[ƒh‚ðŽg—pj F | | THE KNAPSACK PROBLEM | | +-------------------------------------+---------------------------------------+ | ŒÄ‚Ño‚µ–@: KNAPSAC(N,C,K,S,SETT,MAXSUM) | | | +-----------------------------------------------------------------------------+ | ‘€ìŽè‡(ºÝÌßØ°ÄÌßÛ¸Þ×Ñ(Ò²ÝÌßÛ¸Þ×Ñ)‚Ìê‡:1.ƒVƒXƒeƒ€‹N“®Žè‡¤2.ƒf[ƒ^“üo—Í | | Žè‡‚ÆŒ`Ž®¤3.I—¹Žè‡) | | (»ÌÞÙ°ÁÝ‚Ìê‡:1.ŒÄ‚Ño‚µŒ`Ž®-ˆø‚«”‚Ì•À‚וû-¤2.eƒ‹[ƒ`ƒ“¤3.Žqƒ‹[ | | ƒ`ƒ“¤4.‚»‚Ì‘¼) | | 1 CONST NN=100 | | | | TYPE CARGO=ARRAY[1..NN] OF INTEGER | | | | MARK =ARRAY[1..NN] OF BOOLEAN | | | | KNAPSAC(N,C,K:INTEGER;VAR S:CARGO;VAR SETT:MARK;VAR MAXSUM:INTEGER) | | | | N : i“ü—Íj ‰×•¨‚̌” | | | | C : i“ü—Íj ƒiƒbƒvƒTƒbƒN‚ÌÅ‘å—e—Ê | | | | K : i“ü—Íj ‘g‚݇‚킹‚ÌŬŒÂ” | | | | S : i“ü—Íj Še‰×•¨‚Ìd‚³ | | | | SETT : io—Íj ‰×•¨‚Ì‘g‚݇‚킹 | | | | MAXSUM : io—Íj Å‘åd—Ê | | | | | | | | | | | | | | | +-----------------------------------------------------------------------------+ ’. ƒVƒXƒeƒ€‚P‚‚ɂ‚«‚P–‡ì¬ •K—v‚Èꇃ‚ƒWƒ…[ƒ‹‚P‚‚ɂ‚«‚P–‡ì¬(²ÝÀ°Ìª°½Žd—l‘-—lŽ®‚P-‚ɒljÁ) —lŽ®‚V +-------------------------------------+----------------+----------------------+ | ƒVƒXƒeƒ€–¼: ‚`‚k‚f‚n‚o‚`‚j | | –â‘è‰ðà | +-------------------------------------+ +----------------------+ | ƒ‚ƒWƒ…[ƒ‹–¼: ‚j‚m‚`‚o‚r‚`‚b | ײÌÞ×Ø-“o˜^”Ô†: | +-------------------------------------+---------------------------------------+ | 쬎Ò: ¯–ìŒ\Žq | •¶‘쬎Ò: ¯–ìŒ\Žq | +-------------------------------------+---------------------------------------+ | 쬊úŠÔ: 60/07/ [ 60/08/ | •¶‘쬊úŠÔ: 60/09/ [ 60/10/ | +-------------------------------------+---------------------------------------+ | | | ‚h‚m‚o‚t‚s : i‚r1,‚r2,¥¥¥,‚rn, ‚bj | | | | ‚r1,¥¥¥,‚rn : Še‰×•¨‚Ìd‚³ | | | | ‚b : ƒiƒbƒvƒTƒbƒN‚ÌÅ‘å—e—Ê | | | | ‚n‚t‚s‚o‚t‚s : ‡”‚ri … ‚b ‚ð•K—v\•ªðŒ‚Æ‚µ‚ÄA ‡”‚ri‚ª | | | | Å‘å‚Æ‚È‚é‚悤‚È i 1,2,¥¥¥,n j‚Ì •”•ªW‡‚s | | | | ‚𠌩‚‚¯‚éB i‡”‚ri ‚Ì i ‚Í ‚s‚Ì—v‘fj | | | | | | | | Å“K‚ȉð‚ÍAi 1,2,¥¥¥,n j‚Ì •”•ªW‡ ‚s ‚Ì ‚·‚ׂẴTƒuƒZƒbƒg‚Ì | | | | ‘˜a‚ðŒvŽZ‚·‚邱‚Æ‚É‚æ‚Á‚Ä Œ©‚‚¯‚ç‚ê‚éB | | n | | ‚»‚ê‚ç‚̃TƒuƒZƒbƒg‚ÍA‚Q ’ö‚Å‚ ‚é | | | | ‚»‚±‚ÅAk†0 ‚̃Aƒ‹ƒSƒŠƒYƒ€ ‚`k ‚ÍA‘½‚­‚Ä K ŒÂ ‚Ì—v‘f‚ðŽ‚ƒTƒu | | | | ƒZƒbƒg ‚s ‚ðl‚¦‚éB | | | | i k : Subset of size j | | | | | | | | —á | | Input : i 54,45,43,29,23,21,14,1,110 j | | | | ---------------------------------------------------------- | | Subset of Objects SUM | | size k added | | ---------------------------------------------------------- | | k=0 54,45,1 100 | | | | MAXSUM=100 SET=[54,45,1] | | ---------------------------------------------------------- | | k=1 54 45,1 100 | | 45 54,1 100 | | 43 54,1 98 | | 29 54,23,1 107 | | 23 54,29,1 107 | | 21 54,29,1 105 | | 14 54,29,1 98 | | 1 54,45 100 | | | | MAXSUM=107 SET=[29,54,23,1] | | ---------------------------------------------------------- | | | | | | | +-----------------------------------------------------------------------------+ —lŽ®‚U +-------------------------------------+----------------+----------------------+ | ƒVƒXƒeƒ€–¼: ‚`‚k‚f‚n‚o‚`‚j | | ƒAƒ‹ƒSƒŠƒYƒ€—¬‚ê} | +-------------------------------------+ +----------------------+ | ƒ‚ƒWƒ…[ƒ‹–¼: KNAPSAC-1 | ײÌÞ×Ø-“o˜^”Ô†: | +-------------------------------------+---------------------------------------+ | 쬎Ò: ¯–ìŒ\Žq | •¶‘쬎Ò: ¯–ìŒ\Žq | +-------------------------------------+---------------------------------------+ | 쬊úŠÔ: 60/07/ [ 60/08/ | •¶‘쬊úŠÔ: 60/09/ [ 60/12/ | +-------------------------------------+---------------------------------------+ | | | | | ++-------++ +-----+ +-------+ +---------+ | | !!MODULE !! +CONST + + NN + ! 100 ! | | !! !!--+---+ +--! !---! ! | | !!KNAPSAC!! ! + + + + ! ! | | ++-------++ ! +-----+ +-------+ +---------+ | | ! +-----+ ++-------++ +--------++ +---------+ | | ! + TYPE + !! CARGO !! ! ARRAY !! !INTEGER ! | | !---+ +--!! !!---! !!--! ! | | ! + + ! !! !! ! [1..NN]!! ! ! | | ! +-----+ ! ++-------++ +--------++ +---------+ | | ! ! ++-------++ +--------++ +---------+ | | ! ! !! MARK !! ! ARRAY !! !BOOLEAN ! | | ! !-!! !!---! !!--! ! | | ! !! !! ! [1..NN]!! ! ! | | ! ++-------++ +--------++ +---------+ | | ! ++-------++ +-----+ +-------+ +---------+ | | ! !!PROCEDU!! +PARAMAT+ + N,C,K + !INTEGER ! | | !---!!RE !!--+ ER +-+-! !--! ! | | ! !!KNAPSAC!! + + ! + + ! ! | | ! ++-------++ +-----+ ! +-------+ +---------+ | | ! ! +-------+ ++-------++ | | ! ! + VAR + !!CARGO !! | | ! !-! S !--!! !! | | ! ! + + !! !! | | ! ! +-------+ ++-------++ | | ! ! +-------+ ++-------++ | | ! ! + VAR + !!MARK !! | | ! !-! SETT !--!! !! | | ! ! + + !! !! | | ! ! +-------+ ++-------++ | | ! ! +-------+ +---------+ | | ! ! + VAR + !INTEGER ! | | ! !-! MAXSUM !--! ! | | ! + + ! ! | | ! +-------+ +---------+ | | +--+--+ | | !KN-1 ! | | +-----+ | | +-----+ | | | | | | | | | | | | | | | | | | | | | | | | | | | +-----------------------------------------------------------------------------+ —lŽ®‚U +-------------------------------------+----------------+----------------------+ | ƒVƒXƒeƒ€–¼: ‚`‚k‚f‚n‚o‚`‚j | | ƒAƒ‹ƒSƒŠƒYƒ€—¬‚ê} | +-------------------------------------+ +----------------------+ | ƒ‚ƒWƒ…[ƒ‹–¼: KNAPSAC-2 | ײÌÞ×Ø-“o˜^”Ô†: | +-------------------------------------+---------------------------------------+ | 쬎Ò: ¯–ìŒ\Žq | •¶‘쬎Ò: ¯–ìŒ\Žq | +-------------------------------------+---------------------------------------+ | 쬊úŠÔ: 60/07/ [ 60/08/ | •¶‘쬊úŠÔ: 60/09/ [ 60/12/ | +-------------------------------------+---------------------------------------+ | | | | | +-----+ | | !KN-1 ! | | +-----+ | | +--+--+ | | ! +-----+ +-------+ +---------+ | | ! + VAR + + I,J,M + !INTEGER ! | | !--+ +--+--! !---! ! | | ! + + ! + + ! ! | | ! +-----+ ! +-------+ +---------+ | | ! ! +-------+ +---------+ | | ! ! + SUM + !INTEGER ! | | ! !--! !---! ! | | ! ! + + ! ! | | ! ! +-------+ +---------+ | | ! ! +-------+ ++-------++ | | ! ! + T + !! MARK !! | | ! !--! !---!! !! | | ! + + !! !! | | ! +-------+ ++-------++ | | ! ++-------++ +-----+ +-------+ +---------+ | | ! !!PROCEDU!! + VAR + + U,V,W + !INTEGER ! | | !--!!ER !!--+--+ +---! !---! ! | | ! !! SORT !! ! + + + + ! ! | | ! ++-------++ ! +-----+ +-------+ +---------+ | | ! ! | | ! ! +-----+ | | ! ! !KN-2 ! | | ! !----+-----+ | | +--+--+ +-----+ | | !KN-3 ! | | +-----+ | | +-----+ | | | | | | | | | | +-----+ +--------++ +--------++ +-------+ +---------+ | | !KN-2 ! ! FOR !! ! FOR !! ! IF + !W:=S[U]; ! | | +-----+----! V:=2 !!--! U:=N !!--! S[U]> +---!S[U]:=S[U! | | +-----+ ! TO N !! !DOWNTO V!! ! S[U-1]+ !-1]; ! | | +--------++ +--------++ +-------+ !S[U-1]:=W! | | +---------+ | | | | | | | | | | | | | | | | | | | | | +-----------------------------------------------------------------------------+ —lŽ®‚U +-------------------------------------+----------------+----------------------+ | ƒVƒXƒeƒ€–¼: ‚`‚k‚f‚n‚o‚`‚j | | ƒAƒ‹ƒSƒŠƒYƒ€—¬‚ê} | +-------------------------------------+ +----------------------+ | ƒ‚ƒWƒ…[ƒ‹–¼: KNAPSAC-3 | ײÌÞ×Ø-“o˜^”Ô†: | +-------------------------------------+---------------------------------------+ | 쬎Ò: ¯–ìŒ\Žq | •¶‘쬎Ò: ¯–ìŒ\Žq | +-------------------------------------+---------------------------------------+ | 쬊úŠÔ: 60/07/ [ 60/08/ | •¶‘쬊úŠÔ: 60/09/ [ 60/12/ | +-------------------------------------+---------------------------------------+ | | | | | +-----+ | | !KN-3 ! | | +-----+ | | +--+--+ | | ! ++-------++ +---------+ | | ! !!FUNCTIO!! !INTEGER ! | | !----!!N !!--+--! ! | | ! !! BIN !! ! ! ! | | ! ++-------++ ! +---------+ | | ! ! +-----+ +-------+ +---------+ | | ! ! +PARAMAT+ + DEC + !INTEGER ! | | ! !--+ER +-+--! !---! ! | | ! ! + + ! + + ! ! | | ! ! +-----+ ! +-------+ +---------+ | | ! ! ! +-------+ ++-------++ | | ! ! ! + VAR + !! NARK !! | | ! ! !--! T !---!! !! | | ! ! + + !! !! | | ! ! +-------+ ++-------++ | | ! ! +-----+ +-------+ +---------+ | | ! ! + VAR + + I,C + !INTEGER ! | | ! !--+ +----! !---! ! | | ! ! + + + + ! ! | | ! ! +-----+ +-------+ +---------+ | | ! ! +--------++ +---------+ | | ! ! ! FOR !! ! T[I]:= ! | | ! !--!I:=1 TO !!----! FALSE ! | | ! ! N !! ! ! | | ! +----+---++ +---------+ | | ! ! | | ! +----+----+ | | ! ! C:=0; ! | | ! ! I:=1 ! | | ! ! ! | | ! +----+----+ | | ! ! | | ! +----+---++ +-------+ +---------+ | | ! !WHILE !! ! IF + !T[I]:= ! | | ! !(DEC<>0)!!--+---!DEC MOD +---! TRUE; ! | | ! !and(I<=N)! ! ! 2 =1 + !C:=C+1 ! | | ! +----+---++ ! +----+--+ +---------+ | | ! ! ! ! | | ! ! ! +---------+ | | ! ! ! !DEC:=DEC ! | | ! ! !---!MOD 2; ! | | ! ! !I:=I+1 ! | | ! ! +---------+ | | ! +----+----+ | | +--+--+ !BIN:=C ! | | !KN-4 ! ! ! | | +-----+ ! ! | | +-----+ +---------+ | +-----------------------------------------------------------------------------+ —lŽ®‚U +-------------------------------------+----------------+----------------------+ | ƒVƒXƒeƒ€–¼: ‚`‚k‚f‚n‚o‚`‚j | | ƒAƒ‹ƒSƒŠƒYƒ€—¬‚ê} | +-------------------------------------+ +----------------------+ | ƒ‚ƒWƒ…[ƒ‹–¼: KNAPSAC-4 | ײÌÞ×Ø-“o˜^”Ô†: | +-------------------------------------+---------------------------------------+ | 쬎Ò: ¯–ìŒ\Žq | •¶‘쬎Ò: ¯–ìŒ\Žq | +-------------------------------------+---------------------------------------+ | 쬊úŠÔ: 60/07/ [ 60/08/ | •¶‘쬊úŠÔ: 60/09/ [ 60/12/ | +-------------------------------------+---------------------------------------+ | | | | | +-----+ | | !KN-4 ! | | +-----+ | | +--+--+ | | ! ++-------++ +---------+ | | ! !!FUNCTIO!! !INTEGER ! | | !----!!N !!--+--! ! | | ! !! WA !! ! ! ! | | ! ++-------++ ! +---------+ | | ! ! +-----+ +-------+ ++-------++ | | ! ! +PARAMAT+ + T + !! MARK !! | | ! !--+ER +----! !----!! !! | | ! ! + + + + !! !! | | ! ! +-----+ +-------+ ++-------++ | | ! ! +-----+ +-------+ +---------+ | | ! ! + VAR + + SUM + !INTEGER ! | | ! !--+ +-+--! !----! ! | | ! ! + + ! + + ! ! | | ! ! +-----+ ! +-------+ +---------+ | | ! ! ! +-------+ +---------+ | | ! ! ! + K + !INTEGER ! | | ! ! !--! !----! ! | | ! ! + + ! ! | | ! ! +-------+ +---------+ | | ! ! +---------+ | | ! ! !SUM:=0 ! | | ! !--! ! | | ! ! ! | | ! +----+----+ | | ! ! | | ! +----+---++ +-------+ +---------+ | | ! ! FOR !! ! IF + !SUM:=SUM+! | | ! ! K:=1 TO!!---! T[K] +----!S[K]; ! | | ! ! N !! ! + !WA:=SUM ! | | ! +--------++ +-------+ +---------+ | | +--+--+ | | !KN-5 ! | | +-----+ | | +-----+ | | | | | | | | | | | | | | | | | | | | | | | | | | | +-----------------------------------------------------------------------------+ —lŽ®‚U +-------------------------------------+----------------+----------------------+ | ƒVƒXƒeƒ€–¼: ‚`‚k‚f‚n‚o‚`‚j | | ƒAƒ‹ƒSƒŠƒYƒ€—¬‚ê} | +-------------------------------------+ +----------------------+ | ƒ‚ƒWƒ…[ƒ‹–¼: KNAPSAC-5 | ײÌÞ×Ø-“o˜^”Ô†: | +-------------------------------------+---------------------------------------+ | 쬎Ò: ¯–ìŒ\Žq | •¶‘쬎Ò: ¯–ìŒ\Žq | +-------------------------------------+---------------------------------------+ | 쬊úŠÔ: 60/07/ [ 60/08/ | •¶‘쬊úŠÔ: 60/09/ [ 60/12/ | +-------------------------------------+---------------------------------------+ | | | | | +-----+ | | !KN-5 ! | | +-----+ | | +--+--+ | | ! +---------+ | | ! ! SORT ! | | !----! ! | | ! ! | | +----+----+ | | ! | | +----+----+ | | !MAXSUM:=0! | | ! M:=1 ! | | ! ! | | +----+----+ | | ! | | +----+---++ +---------+ | | ! FOR !! ! M:=M*2 ! | | !I:=0 TO !!---! ! | | ! N !! ! ! | | +----+---++ +---------+ | | ! | | +----+---++ +-------+ +-------+ +-----+ | | ! FOR !! ! IF + ! IF + !KN-6 ! | | !I:=0 TO !!---!BIN(I,T) +---!WA(T)<=C +--+-----+ | | !M-1 !! ! <=K + ! + +-----+ | | +--------++ +-------+ +-------+ | | | | | | +-----+ | | !KN-6 ! | | +-----+ | | +--+--+ | | ! | | +----+----+ | | !SUM:= ! | | ! WA(T) ! | | ! ! | | +----+----+ | | ! | | +----+---++ +------------+ +---------+ | | ! FOR !! !IF (NOT T[J]+ !SUM:=SUM+! | | !J:=1 TO !!---!and (SUM+S[J] +--!S[J]; T[J! | | !N !! !<=C + !]:=TRUE ! | | +----+---++ +------------+ +---------+ | | ! | | +----+--+ +---------+ | | !IF + !NAXSUM:= ! | | !MAXSUM +--!SUM; ! | | !B: | | | | B>CKNAPSAC <“ü—̓tƒ@ƒCƒ‹–¼ | | | | 3. ƒeƒXƒg—á | | | | A>B: | | | | B>CKNAPSAC