The legendary nine-headed dragon is a particularly gluttonous animal. Although the name is called "Nine Dragons", it is only
Said it was born with nine heads, and in the process of growing up, it sometimes grows a lot of new head, head of the
The total will be far greater than nine, of course, the old head because of aging and their own fall off.
One day, a nine-headed dragon with a M-head saw a fruit tree with n fruit, overjoyed,
I can't wait to eat it all in a mouthful. But you have to take care of each head, so it needs to divide the N fruit
into M group, each group has at least one fruit, let each head eat a group.
This m head has one of the largest, called "Big Head", is the head of the heads, it to eat exactly k
Fruit, and the K-fruits should naturally include the only one of the largest fruits. Fruit by N-1
The root branches are connected, because the fruit tree is a whole, so you can start from any fruit along the branches.
"Go" to any one of the other fruits.
For each branch, if the two fruits it connects need to be eaten by a different head, then two
The head will break the branches together and separate the fruit, and if the two fruits are eaten by the same head, then
The head will not bother to break it and eat the fruit directly with the branches. Of course, eating branches is not very
Comfortable, so each branch has a eat down "uncomfortable value", and the nine Dragon's uncomfortable value is the
The sum of the "uncomfortable values" of the branches eaten by the head.
Nine dragon hope its "uncomfortable value" as small as possible, can you help it calculate?
As shown in Example 1, the fruit tree contains 8 fruits, 7 segments of branches, each section of the branch "uncomfortable
Value "is labeled next to the branch. Nine Dragons have two heads, the big head needs to eat 4 fruits, which must
Must contain the greatest fruit. namely n=8,m=2,k=4:
Figure one describes the shape of the fruit tree, and figure II describes the optimal strategy.
The Big head eats 4 fruit, uses the solid point marking;
The small head eats 4 fruit, uses the hollow point marking;
Nine Dragon's discomfort value is 4, because the image is marked with a thin edge
The branches of the memory were eaten by the big head.
Enter a description
Input Description
The 1th line of the input file dragon.in contains three integers N (1<=n<=300), M (2<=m<=n),
K (1<=k<=n). n Fruit numbered,..., N, and the largest fruit number is always 1. 2nd
Row to Nth line describes the shape of the fruit tree, each line contains three integers a (1<=a<=n), B (1<=b<=n),
C (0<=c<=105), which indicates that there is an uncomfortable value for the branch of C to connect fruit A and fruit B.
Output description
Output Description
The output file Dragon.out has only one row and contains an integer that represents the "big head" requirement
Under the premise of the nine Dragons the minimum value of the discomfort value. If the requirements are not met, output-1.
Sample input
Sample Input
8 2 4
1 2 20
1 3 4
1 4 13
2 5 10
2 6 12
3 7 15
3 8 5
Sample output
Sample Output
4
Data range and Tips
Data Size & Hint
The sample corresponds to the example in the title description.
"Problem Analysis"
The effect is: A nine dragon animal, have m head, each head must eat the fruit, a tree with n fruit, assigned to it each head to eat,
One of the biggest heads to eat K fruit, the rest assigned to the other head, if a head at the same time to eat adjacent fruit will have a uncomfortable
The value, now wants you to assign the fruit to make the discomfort value and minimum.
Problem Solving Ideas:
No solution good judgment, n-k<m-1 is no solution (fruit not enough to eat).
A case of a solution
If you can simplify the M-head into a head to eat K the minimum cost is good. In two cases:
1, m=2, is the head of the tree to eat the branches + small heads to eat the branches.
2, M>2, at this time only to consider the big head to eat the branches, because the other can be divided into different heads according to the odd to eat, so that they are not counted into the final answer.
Then there is a simple tree-shaped DP problem, first turn the tree into a binary tree, then: f[i][j][k]=min{f[lc[i]][x][0]+f[rc[i]][j-x][k]+ (m==2) * (k==0) *d[i] | | f[lc[i]][x-1][1] +f[rc[i]][j-x][k]+ (k==1) *d[i]}f[i][j][k] refers to I as the root of the sub-tree of J for the Big Head eat, the father is K (k=1 by the head to eat k=0 by small heads eat).
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intmaxn=302;Const intinf=100000000;intn,m,k,tot=0, ans=INF;intA[MAXN][MAXN],SUM[MAXN];intf[maxn][maxn][3];BOOLB[MAXN];structnode{intLc,rc,f,d;} TREE[MAXN];voidDebugintv) {printf ("%d\n", V); if(TREE[V].LC) debug (TREE[V].LC); if(tree[v].rc) debug (tree[v].rc);}voidDEBUG2 (intv) { if(TREE[V].LC) debug (TREE[V].LC); printf ("%d\n", V); if(tree[v].rc) debug (tree[v].rc);}intdpintRootintXintg) { if(x<0)returnINF; if(f[root][x][g]>=0)returnF[root][x][g]; if(!root &&!x)returnf[root][x][g]=0; F[ROOT][X][G]=INF; for(intI=0; I<=min (X,sum[root]); i++) { intT1=DP (Tree[root].lc,i,0) + (m==2) * (g==0)*TREE[ROOT].D; intT2=DP (tree[root].lc,i-1,1) + (g==1)*TREE[ROOT].D; intT3=DP (tree[root].rc,x-i,g); T1=min (t1,t2); F[ROOT][X][G]=min (f[root][x][g],t1+T3); } returnf[root][x][g];}voidFind (introot) { if(!root)return ; Find (TREE[ROOT].LC); Find (TREE[ROOT].RC); Sum[root]=sum[tree[root].lc]+sum[tree[root].rc]+1;}intMain () {scanf ("%d%d%d",&n,&m,&k); for(intI=1; i<n;i++) { intx,y,d; scanf ("%d%d%d",&x,&y,&d); Tree[y].rc=tree[x].lc; TREE[X].LC=y; TREE[Y].D=D; } if(n-k<m-1) {printf ("-1\n");return 0;} memset (SUM,0,sizeof(sum)); Find (1); Memset (F,255,sizeof(f)); printf ("%d\n", DP (tree[1].lc,k-1,1)); return 0;}
Codevs Greedy nine-headed dragon