Bsoj 1660 Elective (tree DP)

Source: Internet
Author: User

"Mock test" elective course

Time limit:10000ms Memory limit:65536k
Total submit:365 accepted:166
Case Time Limit:1000ms

Description

Each student in the university, in order to achieve a certain degree of credit, must choose some courses from a number of courses to learn, in the course some courses must be before certain courses to learn, such as advanced mathematics is always in other courses before the study. Now there are n courses, each course has a credit, each course has one or no direct first course (if course A is a course B of the first course that is only after the course of Class A to study B). A student chooses the M-gate course from these courses and asks him what the maximum credits he can get.

Input

The first line has two integers n,m separated by a space. (1<=n<=300,1<=m<=200)
The next n rows, the i+1 line contains two integers ki and Si, ki represents the direct first course of Class I course, and Si represents the credits of the first class. If Ki=0 says there is no direct first class (1<=ki<=n, 1<=si<=20).

Output

Only one line, choose the maximum score of the M-gate course.

Sample Input

7 4
2 2
0 1
0
4
2 1 7 1 7 6-2 2

Sample Output

13

Source

Xinyue

Title: http://mail.bashu.cn:8080/bs_oj/showproblem?problem_id=1660

You have to choose the M gate in the N course, make the credit that obtains is biggest, of course some class depend on other class to choose first ...

Analysis: It is easy to think of a backpack in the tree to solve the problem, assuming f[i][j] as the root of I tree, including I, to choose the maximum of the J course

Then there is f[i][j]=max{F[i][a]+f[k][b]}k is the subtree of I, a+b=j

The complexity is that O (n*m^2) is sufficient for the data range of this problem, but I used an optimization of the backpack for this generalization.

Degree of complexity reduced to O (n*m)

Code:

#include <cstdio>
#include <iostream>
using namespace std;
const int mm=333;
int f[mm][mm],k[mm],s[mm];
int i,j,n,m;
void TREEDP (int u,int c)
{
    if (c) for (int i=1,j;i<=n;++i)
        if (k[i]==u)
        {for
            (j=0;j<c;++j) F I [J]=f[u][j]+s[i];
            TREEDP (i,c-1);
            for (J=1;J<=C;++J)
                F[u][j]=max (f[u][j],f[i][j-1]);
        }
int main ()
{
    while (~scanf ("%d%d", &n,&m))
    {
        for (i=1;i<=n;++i)
            scanf ("%d%d", &k[i],&s[i]);
        for (I=0;i<=m;++i) f[0][i]=0;
        TREEDP (0,m);
        printf ("%d\n", F[0][m]);
    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.