HDU 5148 (Cities-tree backpack), hducities-

Source: Internet
Author: User

HDU 5148 (Cities-tree backpack), hducities-

Cities Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 670 Accepted Submission (s): 198


Problem DescriptionLong long ago, there is a knight called JayYe. he lives in a small country. this country is made up of n cities connected by n-1 roads (that means it's a tree ). the king wants to reward JayYe because he beats the dedevil and save the princess. the king decide to give JayYe exactly K cities as his daughter's dowry.
Here comes the question. although JayYe beats the dedevil, his knee was injured. so he doesn't want to move too much, he wants his citys as close as possible, that is, JayYe wants the expected distance of his cities as small as possible.
The expected distance is defined as the expected distance between node u and node v, both u and v are randomly choose from JayYe's K cities equiprobably (that means first choose u randomly from JayYe's K cities, then choose v randomly from JayYe's K cities, so the case u equals to v is possible ).
Suppose you are the king, please determine the K cities so that JayYe is happy.
Because the author is lazy, you only need tell me the minimum missing CT distance.
InputThe first line contains a single integer T, indicating the number of test cases.
Each test case begins with two integers n and K, indicating the number of cities in this country, the number of cities the king gives to the knight. then follows n-1 lines, each line contains three integers a, B, and c, indicating there is a road connects city a and city B with length c.

[Technical Specification]
1 <= T <= 100
1 <= K <= min (50, n)
1 <= n <= 2000
1 <= a, B <= n
0 <= c <= 100000
OutputFor each case, output one line, contain one integer, the minimum average CT distance multiply K2 .
Sample Input
12 21 2 1
 
Sample Output
2
 
SourceBestCoder Round #23
Recommendheyang | We have carefully selected several similar problems for you: 5231 5230 5229 5228

Connected sub-graphs on the card store tree

The dp [I, j] indicates that the subtree I takes j nodes.

The number of times the v u-v sub-tree of the u tree is obtained = 2 * number of nodes obtained by the sub-tree v * (number of nodes obtained by the K-sub-tree v) K

Then it satisfies the optimal sub-structure


# Include <cstdio> # include <cstring> # include <cstdlib> # include <algorithm> # include <functional> # include <iostream> # include <vector> using namespace std; # define For (I, n) for (int I = 1; I <= n; I ++) # define Fork (I, k, n) for (int I = k; I <= n; I ++) # define Rep (I, n) for (int I = 0; I <n; I ++) # define ForD (I, n) for (int I = n; I --) # define RepD (I, n) for (int I = n; I> = 0; I --) # define Forp (x) for (int p = pre [x]; p = next [p]) # define Forpiter (x) fo R (int & p = iter [x]; p = next [p]) # define Lson (x <1) # define Rson (x <1) + 1) # define MEM (a) memset (a, 0, sizeof (a); # define MEMI (a) memset (a, 127, sizeof ()); # define MEMi (a) memset (a, 128, sizeof (a); # define F (100000007) # define MAXN (2000 + 10) # define MAXK (100 + 10) # define MAXc (100000 + 10) # define INF (commandid pretty ll) typedef long ll; int n, k; ll dp [MAXN] [MAXK]; vector <int> edge [MAXN], dis [MAXN]; void dfs (int U, int fa) {int sz = edge [u]. size (); Rep (I, sz) {int v = edge [u] [I]; ll d = dis [u] [I]; if (v! = Fa) {dfs (v, u); ForD (j, k) {ForD (t, J-1) // obtain at least one node dp [u] [j] = min (dp [u] [j], dp [u] [t] + dp [v] [j-t] + (j-t) * (k-(j-t )) * d * 2) ;}}} int main () {// freopen ("1003.in"," r ", stdin); // freopen (". out "," w ", stdout); int T; scanf (" % d ", & T); while (T --) {scanf (" % d ", & n, & k); For (I, n) dis [I]. resize (0); For (I, n) edge [I]. resize (0); For (I, n-1) {int a, B, c; scanf ("% d", & a, & B, & c ); edge [a]. push_back (B); edge [B]. push_back (a); dis [a]. push_back (c); dis [B]. push_back (c);} MEM (dp) For (I, n) Fork (j, 2, k) {dp [I] [j] = (ll) INF ;} dfs (1,-1); ll ans = INF; For (I, n) ans = min (ans, dp [I] [k]); printf ("% I64d \ n", ans);} return 0 ;}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.