Very Classic Tree DP (if not the tree DP is recommended first look at the following: I just read the other question first, shake hands or something.
Give a tree, leave n the edge (if it leaves the father's Bembie left), and ask the last Apple how many ....
I find it hard to make a contribution,,, after all, not very much. DP better understand,,,,,,, is to think of the,,,,, as a template problem to record it ~ ~ ~
#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
using namespace Std;
const int maxn=101,maxq=100;
int N;//jieidanshu
int Q;//baoliudebian
int G[maxn][maxn];//juzhenbiaoshiliangdianguanxi
int son[maxn][2]; Zijiedian
int APPLE[MAXN]; Jiluzijieidanshang de Pingguoshu
int DP[MAXN][MAXN]; Jiyihua
int FLAG[MAXN]; Biaoji
int ISLEAF[MAXN]; Jilushifouziye
void Create (int root)
{
int i,ct=0;
for (i=1;i<=n;i++)
{
if (g[root][i]!=-1&&flag[i]==0)//zhaodaoleziye
{
son[root][ct++] = i; Diyigehuozhediergeziye
Apple[i]=g[root][i];
Flag[i] = 1;
Create (i); Jixujianshu
}
}
if (ct==0)//meiyouziye
{
Isleaf[root]=1;
}
}
int TREEDP (int root, int num)//num Biaoshi shengxiade Dianshu
{
if (num==0) return 0; Bushengxia
if (isleaf[root]==1) return apple[root]; Zuihouyihang
if (dp[root][num]!=-1) return dp[root][num]; Jiyihua
int i,temp=0;
for (i=0;i<num;i++)
{
Temp=max (TEMP,TREEDP (son[root][0],i) +TREEDP (son[root][1],num-1-i));
}
Dp[root][num]=temp+apple[root];
printf ("%d**\n", Dp[root][num]);
return Dp[root][num];
}
int main ()
{
int i,x,y,w;
memset (G,-1,sizeof (g));
scanf ("%d%d", &n,&q);
for (i=1;i<=n-1;i++)
{
scanf ("%d%d%d", &x,&y,&w);
g[x][y]=w;//zhelihenzhongyaodexiangfa!
G[y][x]=w;
}
memset (flag,0,sizeof (flag));
memset (isleaf,0,sizeof (isleaf));
flag[1]=1;//1 Shi Morende
apple[1]=0;
Create (1);
Memset (Dp,-1,sizeof (DP));
printf ("%d\n", TREEDP (1,q+1));
return 0;
}
URAL1018 Binary Apple Tree