}
template
Typew cleft=c-cw; //Ê£ÓàÈÝÁ¿ Typep b=cp;
//ÒÔÎïÆ·µ¥Î»ÖØÁ¿¼ÛÖµµÝ¼õÐò×°ÈëÎïÆ· while (i<=n&&w[i]<=cleft){ cleft-=w[i]; b+=p[i]; i++; } //×°Âú±³°ü
if(i<=n)b+=p[i]* cleft/w[i]; return b; }
class Object {
friend int Knapsack(int*,int *,int,int); public;
int operator<=(Object a) const {return(d>=a.d);} private: int ID; float d; };
template
Typep Knapsack(Typep p[], Typep w[],Typew c,int n) {
//ΪKnap::Backtrack ³õʼ»¯ Typew W=0; Typep P=0;
Object*Q=new Object[n];
10
for(int i=1;i<=n;i++){ Q[i-1].ID=i;
Q[i-1].d=1.0*p[i]/w[i]; p+=p[i]; w+=w[i]; }
if(w<=c)return P;//×°ÈëËùÓÐÎïÆ·
//ÒÀÎïÆ·µ¥Î»ÖØÁ¿¼ÛÖµÅÅÐò
sort(Q,n);
Knap
ÓÉÓÚ¼ÆËãÉϽ纯ÊýÐèÒªO(n)ʱ¼ä£¬ÔÚ×Çé¿öÏÂÓÐO(2n)¸öÓÒº¢×Ó½áµãÐèÒªÉϽ纯Êý£¬¹Ê¼ÆËã0-1±³°üÎÊÌâµÄ»ØËÝËã·¨ËùÐèµÄ¼ÆËãʱ¼ä¸´ÔÓ¶ÈΪO(n2n)¡£
11
¶Ô»ØËÝ·¨µÄ¸Ä½øÖ÷ÒªÊǶÔÅжÏÊÇ·ñÒƶ¯ÓÒ×ÓÊ÷ÉÏ£¬Ò»ÖÖ¸üÓÐЧµÄ·½·¨ÊÇ°´Ð§ÒæÃܶÈvi/wi¶ÔÊ£Óà¶ÔÏóÅÅÐò£¬½«¶ÔÏó°´ÃܶȵݼõµÄ˳ÐòÈ¥Ìî³ä±³°üµÄÊ£ÓàÈÝÁ¿£¬µ±Óöµ½µÚÒ»¸ö²»ÄÜÈ«²¿·ÅÈ˱³°üµÄ¶ÔÏóʱ£¬¾ÍʹÓÃËüµÄÒ»²¿·Ö¡£
»ØËÝËã·¨µÄÔËÐÐʱ¼äÈ¡¾öÓÚËüÔÚËÑË÷¹ý³ÌÖÐËùÉú³ÉµÄ½áµãÊý,¶øÏ޽纯Êý¿ÉÒÔ´óÁ¿¼õÉÙËùÉú³ÉµÄ½áµã¸öÊý,Ê¡È¥Ðí¶àÎÞνµÄËÑË÷, ʹµÃËÑË÷Ëٶȸü¿ì ,Æäµ÷ÓÃÏ޽纯Êý¼ÆËãÉϽçÐ軨·ÑO(n)ʱ¼ä ,×Çé¿öÏÂÓÐO(2n)¸ö½áµãÐèµ÷ÓÃÏ޽纯Êý ,Ð軨·ÑO(n)ʱ¼ä,ËùÒÔ¸ÃËã·¨µÄʱ¼ä¸´ÔÓ¶ÈΪO(n2n)[12]¡£ »ØËÝ·¨µÄÁíÒ»¸öÖØÒªÌØÐÔ¾ÍÊÇÔÚËÑË÷Ö´ÐеÄͬʱ²úÉú½â¿Õ¼ä ÔÚËÑË÷ÆÚ¼äµÄÈκÎʱ¿Ì ½ö±£Áô´Ó¿ªÊ¼½áµãµ½µ±Ç°¿ÉÀ©Õ¹½áµãµÄ·¾¶Æä¿Õ¼äÐèÇóΪO(´Ó¿ªÊ¼½áµãÆð×·¾¶µÄ³¤¶È),ËùÒÔ ,´Ë´¦¸ÃËã·¨µÄ¿Õ¼ä¸´ÔÓ¶ÈΪO(n),»ØËÝ·¨ÊÇËã·¨Éè¼ÆµÄ»ù±¾·½·¨Ö®Ò» ,ËüÊÊÓÃÓÚ½âһЩÉæ¼°µ½Ñ°ÕÒÒ»×é½âµÄÎÊÌâ»òÕßÇóÂú×ãijЩԼÊøÌõ¼þµÄ×îÓŽâµÄÎÊÌâ,ÇÒÊÊÓÃÓÚÇó½â×éºÏÊýÁ¿½Ï´óµÄÎÊÌâ¡£
2.3 0-1±³°üÎÊÌâÔÚ·ÖÖ¦-Ï޽編ÖеÄʵÏÖ
2.3.1·ÖÖ¦-Ï޽編µÄ»ù±¾ÔÀíÓë·ÖÎö
·ÖÖ¦Ï޽緢ÊÇÁíÒ»ÖÖϵͳµØËÑË÷½â¿Õ¼äµÄ·½·¨£¬ËüÓë»ØËÝ·¨µÄÖ÷ÒªÇø±ðÔÚÓÚ¶ÔE-½áµã(expansion node)µÄÀ©³ä·½Ê½¡£Ã¿¸ö»î½áµãÓÐÇÒ½öÓÐÒ»´Î»á±ä³ÉE-½áµã¡£µ±Ò»¸ö½áµã±äΪE-½áµãʱ£¬ÔòÉú³É´Ó¸Ã½áµãÒƶ¯Ò»²½¼´¿Éµ½´ïµÄËùÓÐнáµã¡£ÔÚÉú³ÉµÄ½áµãÖУ¬Å×ÆúÄÇЩ²»¿ÉÄܵ¼³ö(×îÓÅ)¿ÉÐнâµÄ½áµã£¬ÆäÓà½áµã¼ÓÈË»î½áµã±í£¬È»ºó´Ó±íÖÐÑ¡ÔñÒ»¸ö½áµã×÷ΪÏÂÒ»¸öE½áµã¡£´Ó»î½áµã±íÖÐÈ¡³öËùÑ¡ÔñµÄ½áµã²¢½øÐÐÀ©³ä£¬Ö±µ½ÕÒµ½½â»ò»î¶¯±íΪ¿Õ£¬À©³ä²Å½áÊø¡£ ÓÐÁ½ÖÖ³£Óõķ½·¨¿ÉÓÃÀ´Ñ¡ÔñÏÂÒ»¸öE-½áµã:
¢Ù ÏȽøÏȳö(FIFO)¼´´Ó»î½áµã±íÖÐÈ¡³ö½áµãµÄ˳ÐòÓë¼ÓÈ˽áµãµÄ˳ÐòÏàͬ£¬Òò´Ë»î½áµã±íµÄÐÔÖÊÓë¶ÓÁÐÏàͬ¡£
¢Ú ×îСºÄ·Ñ»ò×î´óÊÕÒæ·¨ÔÚÕâÖÖģʽÖУ¬Ã¿¸ö½áµã¶¼ÓÐÒ»¸ö¶ÔÓ¦µÄºÄ·Ñ»òÊÕÒæ¡£Èç¹û²éÕÒÒ»¸ö¾ßÓÐ×îСºÄ·ÑµÄ½â£¬Ôò»î½áµã±í¿ÉÒÔ×îС¶ÑÀ´½¨Á¢£¬ÏÂÒ»¸öE-½áµã¾ÍÊǾßÓÐ×îСºÄ·ÑµÄ»î½áµã£¬Èç¹ûÏ£ÍûËÑË÷Ò»¸ö¾ßÓÐ×î´óÊÕÒæµÄ½â£¬ Ôò¿ÉÓÃ×î´ó¶ÑÀ´¹¹Ôì»î½áµã±í£¬ÏÂÒ»¸öE-½áµãÊǾßÓÐ×î´óÊÕÒæµÄ»î½áµã[15]¡£
12
2.3.2 0-1±³°üÎÊÌâµÄʵÏÖ
¹¤×÷ÔÚ½â¿Õ¼äÊ÷ÉϵÄFIFO·ÖÖ¦¶¨½ç·½·¨·Ç³£ÀàËÆÓÚ´Ó¸ù½áµã³ö·¢µÄ¿í¶ÈÓÅÏÈËÑË÷¡£ËüÃǵÄÖ÷ÒªÇø±ðʱÔÚFIFO·ÖÖ¦¶¨½çÖв»¿ÉÐеĽáµã²»»á±»ËÑË÷¡£
0-1±³°üÎÊÌâµÄ×î´óÊÕÒæ·ÖÖ¦¶¨½çËã·¨¿ÉÒÔʹÓö¨½çº¯ÊýÀ´¼ÆËã»î½áµãµÄÊÕÒæÉÏÏÞupprofit£¬Ê¹µÃÒÔ»î½áµãΪ¸ùµÄ×ÓÊ÷ÖеÄÈÎÒ»½áµãµÄÊÕÒæÖµ¶¼²»¿ÉÄܳ¬¹ýupprofit£¬»î½áµãµÄ×î´ó¶ÑʹÓÃupprofit×÷Ϊ¹Ø¼üÖµÓò¡£ÔÚ×Ó¼¯Ê÷ÖÐÖ´ÐÐ×î´óÊÕÒæ·ÖÖ¦¶¨½çËÑË÷µÄº¯ÊýÊ×Ïȳõʼ»¯»î½áµãµÄ×î´ó¶Ñ£¬²¢Ê¹ÓÃÒ»¸öÊý×ébestxÀ´¼Ç¼×îÓŽ⡣ÓÉÓÚÐèÒª²»¶ÏµØÀûÓÃÊÕÒæÃܶÈÀ´ÅÅÐò£¬ÎïÆ·µÄË÷ÒýÖµ»áËæÖ®±ä»¯£¬Òò´Ë±ØÐ뽫º¯ÊýËùÉú³ÉµÄ½á¹ûÓ³Éä»Ø³õʼʱµÄÎïÆ·Ë÷Òý¡£º¯ÊýÖеÄÑ»·Ê×ÏȼìÑéE-½áµã×óº¢×ӵĿÉÐÐÐÔ£¬ÈçËüÊÇ¿ÉÐеģ¬Ôò½«Ëü¼ÓÈë×Ó¼¯Ê÷¼°»î½áµã¶ÓÁÐ(¼´×î´ó¶Ñ)£¬½öµ±½áµãÓÒ×ÓÊ÷µÄ¶¨½çÖµÖ¸Ã÷¿ÉÄÜÕÒµ½Ò»¸ö×îÓŽâʱ²Å½«ÓÒº¢×Ó¼ÓÈë×Ó¼¯Ê÷ºÍ¶ÓÁÐÖС£
ÔòÖ÷ÒªËã·¨ÃèÊöΪ£º class Object{
friend int Knapsack(int *,int *,int,int,int *); public:
int operator<=(Object a) const {return (d>=a.d);} private: int ID;
float d;//µ¥Î»ÖØÁ¿¼ÛÖµ };
template
friend Knap
friend int Knapsack(int *,int *,int,int,int *); private:
bbnode *parent; //Ö¸Ïò¸¸½áµãµÄÖ¸Õë
13