[luogu4037 JSOI2008] Warcraft map (tree DP)

Source: Internet
Author: User

Transmission Door

Description

DOTR (Defense of the Robots) Allstars is a popular global Warcraft map, his rules are simple with the same popular map DotA (Defense of the Ancients) Allstars.

The hero inside the DOTR has only one attribute--power. They need to buy equipment to boost their strength, and each piece of equipment can raise a fixed number of points for the hero's strength, so the hero's strength is equal to the strength of all the equipment it buys. Equipment is divided into basic equipment and high-level equipment two kinds. Basic equipment can be purchased directly from the store with gold coins, while advanced equipment needs to be crafted with basic equipment or a lower level of advanced equipment that does not require additional gold. The synthetic route of the equipment can be represented by a tree.

For example, the synthesis of Sange and Yasha requires Sange,yasha and Sange and Yasha Recipe scroll three-like items. Among them Sange again with Ogre Axe, Belt of Giant strength and sange Recipe scroll synthesis. There is a limit to the number of basic equipment, which limits your inability to synthesize certain cost-effective equipment without restriction.

Now, hero Spectre has m gold coins, he wants to use the money to buy equipment to make his strength value as high as possible. Can you help him? He will teach you magical haunt (ghost possession) in return.

Input

The first line contains two integers, n (1 <= n <= 51) and M (0 <= m <= 2,000). Indicates the number of types and coins of the equipment, respectively. Equipped with an integer number of 1 to N.

The next n lines, in the order of equipment 1 to equipment N, describe a device in each line.

The first non-negative integer in each line represents the strength value of the equipment contribution.

The next non-null character indicates whether the equipment is basic or advanced, a for advanced equipment, and b for basic equipment. In the case of basic equipment, the next two positive integers represent the unit price (in gold coins) and the quantity limit (no more than 100). If it is an advanced equipment, followed by a positive integer c, indicating that the advanced equipment requires C low-level equipment. The following 2C number, in turn, describes the type of a low-level equipment and the number of needs.

Output

The first line contains an integer s that represents the maximum number of point strength values that can be lifted.

Sample Input

10 59
5 A 3 6 1 9 2 10 1
1 B 5 3
1 B 4 3
1 B 2 3
8 A 3 2 1 3 1 7 1
1 B 5 3
5 B 3 3
3 A 1 1 5 1 4 1
1 B 3 5
1 B 4 3

Sample Output

33

Solution

Set Dp[i][j][k] indicates that the first item for the root tree spent K yuan to provide upper J I items for more advanced equipment synthesis
There is also an auxiliary array g[i] that represents the maximum power that can be gained by spending I dollars in all the sub-trees of the current

Code
by menteur_hxy#include<cstdio> #include <vector> #include <cstring> #include <iostream># Include<algorithm> #define F (I,A,B) for (register int i= (a), i<= (b), i++) #define R (I,A,B) for (register int i= (b); I>= (a); i--) #define E (I,u) for (register int i=head[u];i;i=nxt[i]) using namespace Std;int read () {int x=0,f=1; char c    =getchar ();    while (!isdigit (c)) {if (c== '-') f=-f; C=getchar ();}    while (IsDigit (c)) x= (x<<1) + (x<<3) +c-48,c=getchar (); return x*f;} const int Inf=0x3f3f3f3f;const int N=60,m=2010;int n,m,cnt;int Nxt[n*n],to[n*n],nd[n*n],head[n],num[n],pri[n],ml[n], Siz[n],pow[n],g[m],ans[m],vis[n],deg[n];int dp[n][n<<1][m];vector <int> t;void dfs (int u) {if (Vis[u]) ret    urn;    Vis[u]=1;        if (!head[u]) {ml[u]=min (ml[u],m/pri[u]);        F (I,0,ml[u]) F (J,i,ml[u]) dp[u][i][j*pri[u]]=pow[u]* (j-i);    return;    } Ml[u]=inf;        E (i,u) {int v=to[i];        DFS (v);        Ml[u]=min (Ml[u],ml[v]/nd[i]); Pri[u]+=nd[i]*PRI[V];    } ml[u]=min (Ml[u],m/pri[u]);        R (I,0,ml[u]) {memset (g,-0x3f,sizeof g); g[0]=0;            E (j,u) {int v=to[j];                R (a,0,m) {int t=-1e9;            F (b,0,a) T=max (T,g[a-b]+dp[v][i*nd[j]][b]);//1 g[a]=t;        }}//for (int j=0;j<=m;j++) cout<<g[j]<< "";cout<<endl; F (j,0,i) F (k,0,m) Dp[u][j][k]=max (dp[u][j][k],g[k]+ (i-j) *pow[u]),//3}} #define Add (a,b,c) nxt[++cnt]=head[a],to[cnt    ]=b,nd[cnt]=c,head[a]=cntint Main () {N=read (), M=read ();        F (i,1,n) {pow[i]=read ();         int C=getchar ()-' A ' +1;            if (c==1) {num[i]=read ();                F (J,1,num[i]) {int a=read (), B=read ();            Add (i,a,b);d eg[a]++;    }} else Pri[i]=read (), Ml[i]=read ();        } memset (dp,-0x3f,sizeof dp);//2 F (I,1,n) if (!deg[i]) {DFS (i);    R (j,0,m) F (k,0,j) Ans[j]=max (Ans[j],ans[j-k]+dp[i][0][k]); }//F(i,1,m) cout<<ans[i]<< "";cout<<endl;    printf ("%d", ans[m]); return 0;}

[luogu4037 JSOI2008] Warcraft map (tree dp)

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.