Original title Link: http://www.lydsy.com/JudgeOnline/problem.php?id=1017
Ideas and Code reference: http://hzwer.com/5198.html
The first thing you can think of is a knapsack problem, but given its tree structure, it is not possible to directly find out the maximum number of items each item can buy and the price of each item.
Price to say, directly to the subtree and add.
As for the maximum number of items purchased, it is certain that the maximum amount of purchases per item will not exceed the number of purchases/sub-items required for the min{sub-item}
Calculate the price and the number of pieces, you can do the backpack.
#include <cstdio>#include<cstring>Const intinf=2147483647;using namespacestd;voidReadint&y) {y=0;Charx=GetChar (); while(x<'0'|| X>'9') x=GetChar (); while(x>='0'&&x<='9') {y=y*Ten+x-'0'; X=GetChar (); }}intMinintXinty) { returnX<y?x:y;}intMaxintXinty) { returnX>y?x:y;}intN,m,cnt,tot,ans;intp[ -],r[ -],c[ -];intf[ -][ the][2005];intg[ -][2005],h[ -][2005];Chars[5];intlast[ -],deg[ -];structedge{intTo,next,v;} e[20005];voidAddintUintVintW) {e[++cnt].to=v; E[cnt].next=Last[u]; Last[u]=CNT; E[CNT].V=W; DEG[V]++;}voiddpintx) { if(!Last[x]) {R[x]=min (r[x],m/c[x]); for(intI=0; i<=r[x];i++) { for(intj=i;j<=r[x];j++) f[x][i][j*c[x]]= (j-i) *P[x]; } return; } R[x]=inf; for(intI=last[x];i;i=E[i].next) {DP (e[i].to); R[X]=min (r[x],r[e[i].to]/e[i].v); C[X]+=e[i].v*C[e[i].to]; } R[x]=min (r[x],m/c[x]); Memset (g,-0x3f3f3f3f,sizeof(g)); g[0][0]=0; for(intl=r[x];l>=0; l--) { inttot=0; for(intI=last[x];i;i=E[i].next) {Tot++; for(intj=0; j<=m;j++) { for(intk=0; k<=j;k++) G[tot][j]=max (g[tot][j],g[tot-1][j-k]+f[e[i].to][l*E[I].V] [K]); } } for(intj=0; j<=l;j++) { for(intk=0; k<=m;k++) F[x][j][k]=max (f[x][j][k],g[tot][k]+p[x]* (lj)); } }}intMain () {memset (F,-0x3f3f3f3f,sizeof(f)); Read (n); read (m); for(intI=1; i<=n;i++) {read (p[i]); scanf ("%s", s); if(s[0]=='A') { intx;read (x); while(x--) { intV,num;read (v), read (num); Add (I,v,num); } } Elseread (C[i]), read (R[i]); } for(intx=1; x<=n;x++) { if(deg[x]!=0)Continue; DP (x); Tot++; for(intI=0; i<=m;i++) { for(intj=0; j<=i;j++) { for(intk=0; k<=r[x];k++) H[tot][i]=max (h[tot][i],h[tot-1][j]+f[x][k][i-J]); } } } for(intI=0; i<=m;i++) ans=Max (ans,h[tot][i]); printf ("%d", ans); return 0;}
"bzoj1017" [JSOI2008] Warcraft Map DOTR