Portal:
Very conventional tree regulation, to the left son right brother.
Then F[node][anc][k] indicates that on node nodes, the most recent contributing ancestor is on the ANC, and on node's son and brother there is the optimal value of K-contributing nodes.
The following transfer equations are then obtained.
f[node][anc][k]=min{f[son[node]][anc][k]+f[bro[node]][anc][k-k]}+value[node]* (Dis[node]-dis[anc]); No contribution
F[node][anc][k]=min{f[son[node]][node][k]+f[bro[node]][anc][k-1-k]} has contributed
Here is the code implementation:
1 //Bzoj 18122 //by Cydiater3 //2016.9.54#include <iostream>5#include <cstdio>6#include <cstdlib>7#include <cstring>8#include <string>9#include <algorithm>Ten#include <queue> One#include <map> A#include <ctime> -#include <cmath> -#include <iomanip> the using namespacestd; - #definell Long Long - #defineUp (I,j,n) for (int i=j;i<=n;i++) - #defineDown (i,j,n) for (int i=j;i>=n;i--) + Const intmaxn= the; - Const intoo=0x3f3f3f3f; +InlineintRead () { A CharCh=getchar ();intx=0, f=1; at while(ch>'9'|| ch<'0'){if(ch=='-') f=-1; ch=GetChar ();} - while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} - returnx*F; - } - intn,k,son[maxn],bro[maxn],value[maxn],dis[maxn],link[maxn],len=0, FA[MAXN],F[MAXN][MAXN][MAXN],PRE_FA[MAXN]; - structedge{ in inty,next,v; -}e[maxn<<1]; to namespacesolution{ +InlinevoidInsertintXintYintV) {e[++len].next=link[x]; link[x]=len;e[len].y=y;e[len].v=v;} - voidDfsintnode) { the for(intI=link[node];i;i=E[i].next) { *dis[e[i].y]=dis[node]+e[i].v; $ DFS (E[I].Y);Panax Notoginseng } - } the voidinit () { +N=read (); k=read (); Apre_fa[0]=-1; theUp (I,1, N) { +Value[i]=read ();intY=read (), v=read (); -pre_fa[i]=y; $ Insert (y,i,v); $ } -Dfs0);d is[0]=0; - } the voidBuildintNodeintfather) { - inttmp=link[node];son[node]=e[tmp].y;fa[node]=father;Wuyi if(TMP) build (E[tmp].y,node); node=e[tmp].y; the for(intI=e[tmp].next;i;i=E[i].next) { -bro[node]=E[i].y;build (e[i].y,node); WuNode=e[i].y; - } About } $ voidTREEDP (intnode) { - if(Son[node]) TREEDP (Son[node]); - if(Bro[node]) TREEDP (Bro[node]); - intFather=Pre_fa[node]; A while(father!=-1){ +Up (Lim,0, k) up (K,0, Lim) { the inttmp=value[node]* (dis[node]-Dis[father]); - if(Son[node]) tmp+=F[son[node]][father][k]; $ if(Bro[node]) tmp+=f[bro[node]][father][lim-K]; thef[node][father][lim]=min (f[node][father][lim],tmp); the } theUp (Lim,1, k) up (K,1, Lim) { the inttmp=0; - if(Son[node]) tmp+=f[son[node]][node][k-1]; in if(Bro[node]) tmp+=f[bro[node]][father][lim-K]; thef[node][father][lim]=min (f[node][father][lim],tmp); the } AboutFather=Pre_fa[father]; the } the if(node==0){ theFather=0; +Up (Lim,0, k) up (K,0, Lim) { - inttmp=0; the if(Son[node]) tmp+=F[son[node]][father][k];Bayi if(Bro[node]) tmp+=f[bro[node]][father][lim-K]; thef[node][father][lim]=min (f[node][father][lim],tmp); the } - } - } the voidSlove () { theBuild0,-1); theMemset (F,0x3f,sizeof(f)); theTREEDP (0); -printf"%d\n", f[0][0][k]); the } the } the intMain () {94 //freopen ("input.in", "R", stdin); the using namespacesolution; the init (); the slove ();98 return 0; About}View Code
BZOJ1812 [Ioi2005]river