/*State definition of the right is the exam time silly smacking transfer of the child in fact the transfer of the father is more simple without considering so many there is no one who has not looked for this information to turn to the past but made a global variable ... wa to quite then there is a special plate when there is a bad writing time is also very long ... ..*/#include<iostream>#include<cstdio>#include<cstring>#defineMAXN 510#defineINF 0x3f3f3f3fusing namespacestd;intN,M,K,NUM,HEAD[MAXN],FA[MAXN],LC[MAXN],RC[MAXN],DIS[MAXN];intson[maxn][maxn],s[maxn],f[maxn][maxn][2],V[MAXN];structnode{intU,v,t,pre;} e[maxn*2];voidADD (int from,intTo,intdis) {num++;e[num].u= from; E[NUM].V=to;e[num].t=dis; E[num].pre=head[ from]; head[ from]=num;}voidDfs (intNowint from) {Fa[now]= from; for(intI=head[now];i;i=e[i].pre)if(e[i].v!= from) {son[now][++s[now]]=e[i].v; DIS[E[I].V]=e[i].t; Dfs (E[i].v,now); }}voidBuild () { for(intI=1; i<=n;i++) for(intj=1; j<=s[i];j++) { intu=i,v=Son[i][j]; if(lc[u]==0) {lc[u]=v;Continue;} U=lc[u]; while(Rc[u]) u=Rc[u]; Rc[u]=v; }}intCintt) { if(m==2&&t==0)return 1; Else return 0;}intDP (intKintPintt) { if(k==0&&P)returnf[k][p][t]=inf; if(f[k][p][t]>=0)returnF[k][p][t]; if(k==0&&p==0)return 0; intR=inf; for(intI=0; i<=p;i++) { intS1=p-i,s2=i; R=min (R,DP (LC[K],S1,0) +DP (rc[k],s2,t) +c (t) *Dis[k]); } for(intI=0; i<=p-1; i++) { ints1=p-i-1, s2=i; R=min (R,DP (LC[K],S1,1) +DP (rc[k],s2,t) +t*Dis[k]); } returnf[k][p][t]=R;}intMain () {scanf ("%d%d%d",&n,&m,&K); if(n-k<m-1) {printf ("-1\n");return 0;} Memset (F,-1,sizeof(f)); intA,b,c; for(intI=1; i<n;i++) {scanf ("%d%d%d",&a,&b,&c); ADD (A,B,C); ADD (B,A,C); } Dfs (1,1); Build (); v[1]=1; printf ("%d\n", DP (lc[1],k-1,1)); return 0;}
Codevs 1746 greedy nine-headed dragon