Description
Input
Output
There is only one number, that is, the minimum value of the cost of access to the entire tree that you can get and the additional modification.
Sample Input
4 10
1 2 3 4
1 2 3 4
1 2 3 4
Sample Output
29
HINT
The original image of the input is left, and its access cost is 1x1+2x2+3x3+4x4=30. The best modification scheme is to change the weight of the 3rd node in the input to 0, get the right image, the access cost is 1x2+2x3+3x1+4x2=19, plus the additional modification cost 10, altogether 29.
It's a strange feeling.
The state I set is f[l,r,k] the sum of the minimum access cost for the element weight value of the interval [l,r] that is greater than or equal to K
Then enumerate a point as the root
1. The weight of this point is <k, then the cost of K is changed to K,f[l,r,k]=min (F[L,R,K],F[L,X-1,K]+F[X+1,R,K]+S[L,R]+K)
2. This point weight value >=k, then we can not spend, can also spend K to change to K,f[l,r,k]=min (f[l,r,k],f[l,x-1,k]+f[x+1,r,k]+s[l,r]+k,f[l,x-1,v[x]]+f[x+1,r,v [X]] +S[L,R])
1 Const2maxn= the;3 type4Node=Record5 X,v,c:longint;6 Flag:boolean;7 End;8 var9F:Array[0.. MAXN,0.. MAXN,0.. MAXN] ofLongint;TenA:Array[0.. MAXN] ofnode; OneSArray[0.. MAXN] ofLongint; A N,p,ans:longint; - - procedureSwapvarx,y:node); the var - T:node; - begin -t:=x;x:=y;y:=T; + End; - + procedureDownvarx:longint;y:longint); A begin at ifX>y Thenx:=y; - End; - - procedureInit; - var - I,j,cnt,min:longint; in begin - read (n,p); to fori:=1 toN Doread (a[i].x); + fori:=1 toN Doread (A[I].V); - fori:=1 toN Doread (A[I].C); the forI:=nDownto 2 Do * forj:=1 toI-1 Do $ ifa[j].x>a[j+1].x ThenSwap (a[j],a[j+1]);Panax Notoginseng fori:=1 toN Dos[i]:=s[i-1]+a[i].c; -cnt:=0; j:=1; the whileCnt<n Do + begin Amin:=Maxlongint; the fori:=1 toN Do + ifA[i].flag=false ThenDown (MIN,A[I].V); - fori:=1 toN Do $ if(A[i].flag=false) and(a[i].v=min) Then $ begin - Inc (CNT); -a[i].flag:=true; thea[i].v:=J; - End;Wuyi Inc (J); the End; - End; Wu - procedureDP; About var $ I,j,k,l:longint; - begin - fori:=0 toN-1 Do - forj:=1 toN-i Do A fork:=1 toN Do + begin thef[j,i+j,k]:=Maxlongint; - forL:=j toI+j Do $ begin the ifA[l].v<k ThenDown (f[j,i+j,k],f[j,l-1, k]+f[l+1, i+j,k]+s[i+j]-s[j-1]+p) the Else the begin theDown (f[j,i+j,k],f[j,l-1, k]+f[l+1, i+j,k]+s[i+j]-s[j-1]+p); -Down (f[j,i+j,k],f[j,l-1, a[l].v]+f[l+1, i+j,a[l].v]+s[i+j]-s[j-1]); in End; the End; the End; Aboutans:=Maxlongint; the fori:=1 toN DoDown (ans,f[1, N,i]); the writeln (ans); the End; + - begin the Init;Bayi DP; the End.
View Code
1564: [NOI2009] binary search tree-Bzoj