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 |
0-1背包#include<iostream> #include<stdio.h> using namespace std; int c[20000]={0}; //背包的大小 int main() { int i,j,n,m; int w[4000],v[4000]; //物品的质量和价值 while(~scanf("%d%d",&n,&m)) { for(i=0;i<n;i++) scanf("%d%d",&w[i],&v[i]); for(i=0;i<n;i++) for(j=m;j>=w[i];j--) c[j]=max(c[j-w[i]]+v[i],c[j]); //有两个重量一样的出现,他们的j-w[i]是一样的,再次比较大小, //也就是比较重量相同的物品那个价值更高 cout<<c[m]<<endl; } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator