Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

学习啦!!!多重背包通过二进制化化为01背包,将O(V*N)降为O(V*logN),,,感谢背包九讲!

Posted by xijunlee93 at 2013-03-31 23:33:30 on Problem 1276
#include<fstream>
using namespace std;
int f[100010];
int n[230],w[230],v,N;
int b[11]={1,2,4,8,16,32,64,128,256,512,1024};
int max(int a,int b)
{
    if (a>b)
    return a;
    else
    return b;
}
int main()
{
     ifstream infile;
     ofstream outfile;
     infile.open("data.in",ios::in);
     outfile.open("data.out",ios::out);
     int i,j,cash,count,k;
     int nn,ww;
     while(infile>>cash)
     {
         memset(f,0,sizeof(f));
         memset(n,0,sizeof(n));
         memset(w,0,sizeof(w));
         infile>>N;
         count=1;
         for (i=1;i<=N;i++)
         {
            infile>>nn>>ww;
            if (nn!=0)
            {for (j=10;j>=0;j--)
            if(nn-b[j]+1>0)
            break;
            for (k=0;k<=j-1;k++)
            {n[count]=b[k];w[count]=ww*b[k];count++;}
            n[count]=nn-b[j]+1;w[count]=ww*(nn-b[j]+1);count++;}
         }
         count--;
         for (i=1;i<=count;i++)
            for (v=cash;v>=0;v--)
             if (v-w[i]>=0)
             f[v]=max(f[v],f[v-w[i]]+w[i]);
         outfile<<f[cash]<<endl;
     }
     return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator