//k a robot, starting from the roots of a tree,//Go to each side of the tree to consume energy and ask how much energy the K person spends at least to traverse all points//dp[u][i] Represents the minimum amount of energy required to traverse a subtree with the I node as the root node of the U point//When i = 0 indicates that a point has traversed the subtree and returned to the previous node#include <cstdio>#include <cstring>#include <iostream>#include <vector>using namespace STD;Const intMAXN =10010;Const intINF =0x3f3f3f3f;intdp[maxn][ -] ;intHEAD[MAXN];intNedge;intN, S, m;structedge{intV, W;intNext;} edge[maxn<<1] ;voidAddedge (intU,intVintW) {edge[nedge].v = v; EDGE[NEDGE].W = W; Edge[nedge].next = Head[u]; Head[u] = nedge++;}voidDfsintU,intPre) {intFlag =0; for(inti = Head[u]; I! =-1; I =edge[i].next) {intv = edge[i].v;if(v = = Pre)Continue; DFS (v, u);intlen = EDGE[I].W; for(intj = M;j >=0; j--) {intsum = INF; for(intK =0; k <= j;k++) {intTMP = (k = =0?2*len:k*len); sum = min (sum, dp[u][j-k] + dp[v][k] + tmp); } dp[u][j] = sum; } }}intMain () {//freopen ("In.txt", "R", stdin); while(~scanf("%d%d%d", &n, &s, &m)) {memset(Head,-1,sizeof(head)) ;memset(DP,0,sizeof(DP)) ; Nedge =0; for(inti =1; i < n;i++) {intU, V, W;scanf("%d%d%d", &u, &v, &w); Addedge (U, V, W); Addedge (V, U, W); } DFS (S,0) ;cout<<dp[s][m]<<endl; }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hdu4003find Metal Mineral Tree-shaped dp+ Group backpack