diff --git a/0-1背包.cpp b/0-1背包.cpp deleted file mode 100644 index 9214b76..0000000 --- a/0-1背包.cpp +++ /dev/null @@ -1,77 +0,0 @@ -#include -#include -using namespace std; -typedef struct{ - int v; - int w; - int no; -}goods; -int n,c;//ÎïÆ·ÊýÁ¿£¬±³°üÈÝÁ¿ -int *x;//µ±Ç°½â -goods *a;//ÎïÆ· -int bv,*bx;//×îÓÅÖµ£¬×îÓŽâ -int cc,cw;//µ±Ç°¼ÛÖµ£¬µ±Ç°ÖØÁ¿ - -int cmp(goods a,goods b)//Ô¼¶¨sort ËùÓÃcmpº¯Êý -{ - return a.v*b.w>=a.w*b.v; -} - -float Bound(int step){//̰ÐÄËã·¨ÇóµÃµ±Ç°×´Ì¬Ï¶ÔÓ¦±³°üµÄ×îÓÅÖµ -int i,m=c-cw; -float b=cc; -for(i=step; i<=n&&a[i].w<=m;i++) - b+=a[i].v, m-=a[i].w; -if(i<=n) - b+=(m*1.0/a[i].w)*a[i].v; -return b; -} - -void dfs(int step){ - int i; - if(step>n){//Ò¶×Ó½áµã - if(cc>bv){ - bv=cc; - for(i=1;i<=n;i++)bx[a[i].no]=x[i];//ÐòºÅ»¹Ô­ - } - return;//»ØÍ· - } - - if(cw+a[step].w<=c){//Âú×ãÔ¼ÊøÌõ¼þ£¬½øÈë×ó×ÓÊ÷ - x[step]=1; - cc+=a[step].v;cw+=a[step].w; - dfs(step+1); - cc-=a[step].v;cw-=a[step].w;//»Ö¸´ÏÖ³¡ - } - - if(Bound(step+1)>bv){//Âú×ãÉϽçÌõ¼þ£¬½øÈëÓÒ×ÓÊ÷ - x[step]=0; - dfs(step+1); - return; - } -} - -int main(){ - int i; - cout<<"ÇëÊäÈëÎïÆ·¸öÊýn£º"<>n; - a=new goods[n+1]; - bx=new int[n+1]; - x=new int[n+1]; - cc=0; - cw=0; - bv=0; - cout<<"ÇëÊäÈë±³°üÈÝÁ¿c£º"<>c; - for(i=1;i<=n;i++)a[i].no=i,x[i]=0; - printf("ÇëÒÀ´ÎÊäÈë%d¸öÎïÆ·µÄ¼ÛÖµ:\n",n); - for(i=1;i<=n;i++)cin>>a[i].v; - printf("ÇëÒÀ´ÎÊäÈë%d¸öÎïÆ·µÄÖØÁ¿:\n",n); - for(i=1;i<=n;i++)cin>>a[i].w; - dfs(1); - printf("×îÓżÛֵΪ£º%d\n",bv); - cout<<"×îÓŽâΪ£º"<