Poj 3624 01 backpack

Source: Internet
Author: User

When I was a beginner, I tried to solve the problem with greedy ideas, but I still couldn't think of it for more than an hour.

// The two parameters F [k] in Max, and f [k-weight [I] + value [I] indicate the maximum value when the backpack capacity is K. // F [k] Is this meaning, not to mention. // While F [k-weight [I] + value [I] also indicates the maximum value when the backpack capacity is K. Why? // First, F [k-weight [I] indicates the capacity of a backpack with K-weight [I, that is to say, F [k-weight [I] // indicates that the capacity is less than weiht [I] to the value of K, + walue [I] makes up for the difference. So ...... // If you pair f [k] = max (F [K], (F [k-weight [I] + value [I]); this sentence is not very clear, // The following code will help you. // cout <"I =" <I + 1 <"f [" <k <"] = "<F [k] <Endl;

  

#include <iostream>using namespace std;int f[12881];int main(){    int Num, TotalWeight, i, j, k, weight[12881], value[12881];    cin >> Num >> TotalWeight;    for(i = 0; i < Num; ++i)    cin >> weight[i] >> value[i];    for(i = 0; i < Num; ++i){        for(k = TotalWeight; k >= weight[i]; --k){            f[k] = max(f[k], (f[k - weight[i]] + value[i]));        }    }    cout << f[TotalWeight] << endl;    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.