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

0-1背包

Posted by 201608020115 at 2017-09-27 19:22:15 on Problem 3624
#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:
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