0-1 algorithm Problem of knapsack problem thought

Source: Internet
Author: User

Topic Overview

After the lottery, general Wisdom first entered the examination room.

Vegetable worm: (the body scatters the luxurious (?) The light) welcome you, the first challenger!!

Xiao Zhi: ... (Go behind the vegetable worm, turn off the light) Her Majesty, although our country is now very rich, but also please do not waste electricity to use such a high-power light bulb.

Vegetable worm (Khan): Ah Ah ~ ~ Eiching said is ~ ~ So, your topic is ... Our intelligence group has picked up the enemy's important general, Peter Pan, to invite his sister to play in the park on Sunday. There are a lot of recreational items in the park, but not every item they like, so they have a "like" rating for each item. Because Peter Pan is also a great role, he will certainly choose the best solution in a limited time. Now all you have to do is find out which of the different activities they choose to make the most of their "liking" in the prescribed time, so we can know where he will appear and then send someone to watch over there.

Xiao Zhi: (light bulb light) every place has a person to guard not on the line?!

"When ~ ~ ~"

Vegetable worm: (hand holds eight centimeters diameter wok, tendon) ... Are you an idiot?-_-## (we're going to have to keep the guards.) How many tables do we have, three missing ones?

Input

The first line of a positive integer N (1<=n<=100) represents the total number of entertainment items; The second line is a positive integer representing the specified time t (0<t<1000); There are n rows, of which the i+2 line has two positive integers, fi (0<=fi<=100) and Ti (0<ti<=100), representing the "liking" of item I and the time it takes.

Output

The sum of the greatest "likes".

Ideas for solving problems

My first reaction was to backtrack and then tle--| | |

In fact, this is a very typical 0-1 knapsack problem, and then according to the 0-1 knapsack problem of the idea of the solution is good.

Problems encountered

In the beginning with backtracking, although I prune (optimal pruning), but still half of the test point timeout.

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

When solving with 0-1 knapsack problem, rows No. 0 and No. 0 need to be reserved for white space (0), otherwise the array subscript will be crossed when the first value is computed.

Source

#include <iostream> #include <fstream> const int max_projects = 101;  
     
     
const int max_time = 1001;  
    struct Project {int happiness;  
     
     
    int time;  
        Project () {this->happiness = 0;  
    This->time = 0;  
     
     
}  
};  
Project Projects[max_projects];  
     
     
int Values[max_projects][max_time]; int getmaxhappiness (int totalprojects, int totaltime) {for (int i = 1; I <= totalprojects; + + i) {f  or (int j = 0; J <= TotalTime + + j) {if (J >= projects[i].time) {Values[i][j]  
            = Std::max (Values[i-1][j], Values[i-1][j-projects[i].time] + projects[i].happiness);  
            else {Values[i][j] = values[i-1][j];  
}} return Values[totalprojects][totaltime];  
    int main () {using Std::cin;  
    Std::ifstream cin; Cin.open ("Input.txT ");  
    int n = 0, t = 0;  
    CIN >> n >> t;  
    for (int i = 1; I <= n; + + i) {cin >> projects[i].happiness >> projects[i].time;  
    int maxhappiness = getmaxhappiness (n, t);  
     
     
    Std::cout << maxhappiness << Std::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.