[JSOI2008] Warcraft Map

Source: Internet
Author: User

Description

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 requires basic
equipment or a lower level of advanced equipment to synthesize and synthesize without the need for additional gold coins. 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. Which Sange again with Ogre Axe, Belt
of Giant strength and sange Recipe scroll synthesized.

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.

(1 <= N <= 51) and M (0 <= m <= 2,000)

Solution

Set aside for a long time the divine question.

Looking at the past, a tree-shaped synthetic route, the choice of sub-tree is related to the ability to synthesize roots, but also to allocate a gold coin, the ultimate gain. And there are restrictions on items.

So, it is a tree-shaped dependency on the knapsack problem.

But the status is not very good, because subtree's equipment may be left behind and may be waiting to synthesize more advanced equipment.

So the state must record the sub-tree of the I node, how many I will be used for the composition above

Set F[i][j][k], I is the root of the subtree, the synthesis of J I for the above synthesis, a total cost of k yuan, that is, the purchase of leaves to spend k yuan.

When it's transferred,

Let's figure out the answer to each subtree first.

Back to X, the Outer enumeration L represents the composition of several X

Then select each subtree in turn and use the tree backpack.

Notice that each subtree here chooses to synthesize at least l*need[y], and need[x] represents the number of X's required to synthesize a father.

Therefore, not like the general backpack, each subtree to choose.

With a pack of backpacks, G[tot][j] says, considering the former tot subtree, spending J dollars to get the maximum power. (each subtree satisfies a minimum of l*need[y])

G[tot][j]=max (G[tot-1][j-k]+f[y][l*nd[y]][k])

When the statistics are done,

Then enumerate a J, indicating that L left a J-synthetic x on a layer of equipment.

F[x][j][k]=max (g[tot][k]+ (l-j) *p[x])

It should be noted that in order to ensure the use of l*nd[y], must make g,f initial value is-inf

0 is definitely not going to work. That would probably use less than l*nd[y] money to synthesize l*nd, although the total strength is 0, but it may also be an optimal solution.

Sometimes, in order to transfer legitimacy, the initial value must be set to a maximum or a minimum.

In this way, each time the optimal solution, it will definitely come out from here.

You can reduce the length of the loop by combining the value needed to synthesize each x and the upper limit of the X composition.

Code
#include <bits/stdc++.h>using namespaceStd;typedefLong Longll;Const intm= -+3;Const intn= -;Const intinf=0x3f3f3f3f;intn,m;intL[n],p[n],c[n];intNd[n];BOOLBa[n];structnode{intNxt,to;} e[2*N];inthd[n],cnt;voidAddintXinty) {e[++cnt].nxt=Hd[x]; E[cnt].to=y; HD[X]=CNT;}voiddpintx) {    if(Ba[x]) {l[x]=min (l[x],m/c[x]); return; }     for(intI=hd[x];i;i=e[i].nxt) {        inty=e[i].to;        DP (y); C[X]+=c[y]*Nd[y]; L[X]=min (l[x],l[y]/Nd[y]); } L[x]=min (l[x],m/c[x]); }intf[n][ the][m];intG[n][m];intRT;BOOLDu[n];voidDfsintx) {    if(Ba[x]) { for(intL=0; l<=l[x];l++){             for(intj=0; j<=l;j++) {f[x][j][l*c[x]]=p[x]* (lj); }        }        return ; }         for(intI=hd[x];i;i=e[i].nxt) {        inty=e[i].to;    DFS (y); }             for(intL=0; l<=l[x];l++){        intnow=0;         for(intI=hd[x];i;i=e[i].nxt) {            inty=e[i].to; now++; memset (G[now],-0x3f,sizeofG[now]);  for(intj=0; j<=m;j++){                  for(intk=0; k<=j;k++) {G[now][j]=max (g[now][j],g[now-1][j-k]+f[y][l*Nd[y]]                [K]); }            }                }         for(intH=0; h<=l;h++){             for(intk=0; k<=m;k++){                if(g[now][k]+ (l-h) *p[x]>F[x][h][k]) {F[x][h][k]=g[now][k]+ (l-h) *P[x];                 }                             }        }    }}intMain () {scanf ("%d%d",&n,&m); CharOpints; memset (L,inf,sizeofL);  for(intI=1; i<=n;i++) {scanf ("%d",&P[i]); Op=GetChar (); if(op=='B') {Ba[i]=1;//Is a leafdu[i]=1; scanf ("%d%d",&c[i],&L[i]); }        Else{scanf ("%d",&s); intSon;  for(intj=1; j<=s;j++) {scanf ("%d",&son); scanf ("%d",&Nd[son]); Du[son]=1;            Add (I,son); }        }    }         for(intI=1; i<=n;i++)if(!du[i]) rt=i;        DP (RT); Memset (F,-inf,sizeoff); DFS (RT);    intans=0;  for(intj=0; j<=l[rt];j++){         for(intk=0; k<=m;k++) {ans=Max (ans,f[rt][j][k]); }} printf ("%d", ans); return 0;} 

[JSOI2008] Warcraft Map

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.