[ctsc2001]1378 Elective Course

Source: Internet
Author: User

1378 Topic description of course Selection

The school implements the credit system. Each compulsory course is subject to a fixed credit and must also receive a corresponding elective course credit. The school opens an elective course for N (n<300), and the number of optional courses per student is given. Students take this m course and pass the examination will be able to obtain the corresponding credit.

In elective courses, some courses can be directly elective, some courses require a certain amount of basic knowledge, you must choose some other courses on the basis of the optional. For example, "Frontpage" must be elective after "Windows Operational basics". We call the "Basics of Windows Operations" a first-class lesson in Frontpage. There is at most one door for direct first-class lessons in each course. Two courses may also have the same first class. Each course has a class number, in turn, the For example:

"See pictures"
Table 1 is 2 of the first courses, 2 is 3, 4 of the first course.   If you choose 3, then both 1 and 2 must have been selected. Your task is to identify a course for yourself, so that you can get the most credits and must meet the principle of first-class priority. It is assumed that there is no time conflict between courses.

Input output Format input format:

The first line has two integers n,m separated by a space. (1<=n<=200,1<=m<=150)

The next n lines, line i+1 contains two integers ki and si, ki denotes the direct first course of the I course, and Si represents the credits for the I course. If ki=0 indicates that there is no direct first course (1<=ki<=n, 1<=si<=20).

Output format:

With only one line, choose the maximum score for M-gate courses.

Input and Output Sample input example # #:
7  ten  2
Sample # # of output:
13
Data range and Tips Data Size & Hint

Each test point 1s

Category labels Tags Click here to expandDynamic planning A typical tree DP, which I hope readers can understand and grasp carefully.

Analysis:

According to the description of the topic we can know that this is a tree motion, but because a point can have multiple sons, it is difficult to write equations, so we think of the forest to two fork, then do.

Here first to add the knowledge of multi-fork to two fork, the rule is the left son, right brother, that is, the left child tree is the son node, the right subtree is a sibling node. This problem is forest, we only need virtual 0 node. The root of our search is f[0].left;

After converting to a two-fork tree, it is easy to write equations f[i,j] to represent the tree with the roots of I, choosing the maximum number of credits that can be obtained with J-courses.

F[i,j]:=max{f[tree[i].left,k]+f[tree[i].right,j-1-k]+a[i],f[tree[i].right,j]} is only related to the current course selection, which is very obvious, is based entirely on the definition of the two-fork tree we have converted and the requirements of the topic.

AC code :

#include <cstdio>#include<cstring>#include<iostream>using namespacestd;#defineN 1010intN,m,f[n][n];structnode{intL,r,num;} Tree[n];voidinit () {scanf ("%d%d",&n,&m);  for(intI=1, x;i<=n;i++) {scanf ("%d%d",&x,&tree[i].num); if(!TREE[X].L) tree[x].l=i; Else{            intt=TREE[X].L;  while(TREE[T].R) t=TREE[T].R; TREE[T].R=i; }     }}intDfsintIintm) {    intTMP1,TMP2; if(f[i][m]!=-1)returnF[i][m]; if(!i| |! Mreturnf[i][m]=0; TMP1=DFS (TREE[I].R,M);  for(intj=0; j<m;j++) {TMP2=dfs (tree[i].l,j) +dfs (tree[i].r,m-j-1)+Tree[i].num; if(TMP1&LT;TMP2) tmp1=TMP2; }    returnf[i][m]=TMP1;}intMain () {memset (F,-1,sizeoff);    Init (); printf ("%d\n", DFS (tree[0].l,m)); return 0;}

--------------------------------------------------------------------------------------------------------------- --------------------------------

Gorgeous Split-line

--------------------------------------------------------------------------------------------------------------- ---------------------------------

Reflection:

Many of the sons of the tree, difficult to handle when we can consider the multi-fork to two fork, or the forest to two fork. The simplified equation is only related to the current node selection or not selection. In particular, this will increase the depth of the tree, that is, the number of times we press the stack, the data range is very cautious use.

Memory Search is a tool to solve the tree, pay attention to the memory of the search framework, and play. Be sure to pay attention to the details. In the function should have the current state is known, the Boundary condition processing (notice to the array assignment), according to the equation enumeration state to find the best, finally will find the optimal assignment to the array and return the value.

[ctsc2001]1378 Elective Course

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.