| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:AC!我没有使用滚动数组,望高手不吝踢教!In Reply To:AC!我没有使用滚动数组,望高手不吝踢教! Posted by:lexdene at 2009-04-05 09:26:37 > 我没有使用滚动数组,只是普通的01背包问题的解法。
> C++新手,望高手不吝赐教!
> #include<iostream>
> using namespace std;
> int main(){
> int n,m,i,j,*w,*v,*f;
> while(cin>>n>>m){
> //申请空间
> w=new int[n];
> v=new int[n];
> f=new int[m+1];
> //读入数据
> for(i=0;i<n;i++)
> cin>>w[i]>>v[i];
> //数据初始化
> for(i=0;i<=m;i++) f[i]=0;
> //计算过程
> for(i=0;i<n;i++)
> for(j=m;j>=w[i];j--)
> if(f[j-w[i]]+v[i]>f[j])
> f[j]=f[j-w[i]]+v[i];
> //求最大值
> int max=-1;
> for(i=0;i<=m;i++)
> if(f[i]>max) max=f[i];
> //输出结果
> cout<<max<<endl;
> //释放空间
> delete w,v,f;
> }
> return 0;
> }
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator