Project scheduling (Dynamic planning)

Source: Internet
Author: User

Project Description: Xiao Ming every day in the open-source community to do projects, assuming that he has a lot of projects to choose from every day, each project has a start time and deadlines, assuming that after each project, get paid is different. Because Xiao Ming will soon master graduate, facing to buy a house, buy a car, give his girlfriend to buy all kinds of bags of pear, but his wallet is empty, he needs enough money to enrich the wallet. Omnipotent netizens trouble you to help Xiao Ming, how to arrange their own projects in the shortest possible time to ensure the most money (note: When doing projects, the project can not be parallel, that is, two projects can not overlap between the time, but a project just ended, you can immediately do another project, that is, the project start and end point can overlap).
Input:

The input may contain multiple test samples.
For each test case, the first line of input is an integer n (1<=n<=10000): The number of items that represent Xiaoming's hands.
Then there are n rows, each with 3 integers st, Ed, and Val, each representing the start of the project, the deadline and the project remuneration, separated by a space between the two adjacent numbers.
The values of St, Ed, and value are in the range of 32-bit signed integers (int), and the input data guarantees that the sum of all the data is also within the range of int

Output:

For each test case, output the maximum reward that xiaoming can get.

Sample input:
31 3 64 8 92 5 1641 14 105 20 1515 20 818 22 12
Sample output:
1622

Problem Solving Ideas:

Dynamic Programming Questions

Can be solved according to the 01 knapsack idea. The difference is that 01 of the backpacks are order-independent.

This is sorted by the end time. (You can also sort by start time, and the order of traversal needs to be reversed)

Reference code:
1#include <stdio.h>2#include <algorithm>3 using namespacestd;4 intN;5 classP6 {7   Public:8  intStart,end,value;9 };TenP p[10001]; One intdp[10001];//Dp[i] Arrange the top I items, up to the maximum value obtained A BOOLcmpConstP & P1,ConstP &p2) { -     returnP1.end <P2.end; - } the  - intMain () - { -   intN, I, J; +    while(SCANF ("%d", &n)! =EOF) { -    for(i=1; i<=n; i++) +{scanf (" %d%d%d", &p[i].start, &p[i].end, &p[i].value); ADp[i] =0; at     } -Sort (p+1, p+n+1, CMP);//Sort by end time is the focus -dp[0] =0; -      for(i=1; i<=n; i++) -         { -         for(j=i-1; J>0; j--)//find the most J to meet the current I. They're not satisfied, j=0 . in{if(P[i].start >= P[j].end) Break;} -Dp[i] = Dp[j] +P[i].value; to       if(Dp[i] < dp[i-1]) +Dp[i] = dp[i-1]; -         } theprintf"%d\n", Dp[n]); *     } $     return 0;Panax Notoginseng}

Project scheduling (Dynamic planning)

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.