There are N (1 ≤ N ≤ 105) cities on land, and there are N - 1 wires connecting the cities. Therefore, each city can transmit electricity to all other cities by these wires.

Dark_Sun made a decision to build a nuclear power plant to supply electricity for all the cities. The nuclear power plant can be built in any city, but the cost for the electricity transmission is very strange. Let the weight of wire which connects city u and city v is E(u, v) (|E(u, v)| < 109). If the power plant is built in city x, the cost for electricity transmission from city x to city y is D(x, y) = (E(x, s1) + E(s1, s2) + ... + E(sp, y))k, where {x, s1, s2, ..., sp, y} is the path from x to y. Because of the bug of the computer, the total cost for building a nuclear power plant in city x is Σ(D(x, i)) mod 100000007 (0 ≤ i < N, ix), and the total cost is obviously not a negative number.

Dark_Sun asks you to write a program to calculate the minimum total cost.

Input

Input will consist of multiple test cases.

The first line of each test case contains two integers N, K (1 ≤ N ≤ 105,0 ≤ K ≤ 10).

The next N - 1 lines, each line contains three integers u, v, w (0 ≤ u, v < N, |w| < 109, uv), indicating E(u, v) is w.

Output

For each case, output one line with one integer, indicating the minimum total cost.

Sample Input

  1. 3 2
  2. 0 1 2
  3. 1 2 2

Sample Output

  1. 8

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #include<cstdlib>
  5. #include<cstring>
  6. #include<string>
  7. #include<cmath>
  8. #include<map>
  9. #include<set>
  10. #include<vector>
  11. #include<queue>
  12. #include<bitset>
  13. #include<ctime>
  14. #include<time.h>
  15. #include<deque>
  16. #include<stack>
  17. #include<functional>
  18. #include<sstream>
  19. //#include<cctype>
  20. //#pragma GCC optimize(2)
  21. using namespace std;
  22. #define maxn 100005
  23. #define inf 0x7fffffff
  24. //#define INF 1e18
  25. #define rdint(x) scanf("%d",&x)
  26. #define rdllt(x) scanf("%lld",&x)
  27. #define rdult(x) scanf("%lu",&x)
  28. #define rdlf(x) scanf("%lf",&x)
  29. #define rdstr(x) scanf("%s",x)
  30. #define mclr(x,a) memset((x),a,sizeof(x))
  31. typedef long long ll;
  32. typedef unsigned long long ull;
  33. typedef unsigned int U;
  34. #define ms(x) memset((x),0,sizeof(x))
  35. const long long int mod = 100000007;
  36. #define Mod 1000000000
  37. #define sq(x) (x)*(x)
  38. #define eps 1e-5
  39. typedef pair<int, int> pii;
  40. #define pi acos(-1.0)
  41. //const int N = 1005;
  42. #define REP(i,n) for(int i=0;i<(n);i++)
  43. typedef pair<int, int> pii;
  44.  
  45. inline int rd() {
  46. int x = 0;
  47. char c = getchar();
  48. bool f = false;
  49. while (!isdigit(c)) {
  50. if (c == '-') f = true;
  51. c = getchar();
  52. }
  53. while (isdigit(c)) {
  54. x = (x << 1) + (x << 3) + (c ^ 48);
  55. c = getchar();
  56. }
  57. return f ? -x : x;
  58. }
  59.  
  60. ll gcd(ll a, ll b) {
  61. return b == 0 ? a : gcd(b, a%b);
  62. }
  63. int sqr(int x) { return x * x; }
  64.  
  65. /*ll ans;
  66. ll exgcd(ll a, ll b, ll &x, ll &y) {
  67. if (!b) {
  68. x = 1; y = 0; return a;
  69. }
  70. ans = exgcd(b, a%b, x, y);
  71. ll t = x; x = y; y = t - a / b * y;
  72. return ans;
  73. }
  74. */
  75. int n, K;
  76. struct node {
  77. int u, v;
  78. ll w;
  79. int nxt;
  80. }e[maxn<<1];
  81.  
  82. ll dp[maxn][12];
  83. int head[maxn];
  84. int tot;
  85. ll C[14][14];
  86.  
  87. void init() {
  88. C[0][0] = C[1][1] = C[1][0] = 1;
  89. for (int i = 2; i <= 13; i++) {
  90. C[i][i] = C[i][0] = 1;
  91. for (int j = 1; j < i; j++) {
  92. C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
  93. C[i][j] %= mod;
  94. }
  95. }
  96. }
  97.  
  98. void addedge(int u, int v, ll w) {
  99. e[++tot].u = u; e[tot].v = v; e[tot].w = w; e[tot].nxt = head[u]; head[u] = tot;
  100. }
  101.  
  102. void dfs1(int u, int fa) {
  103. // ms(dp[u]);
  104. dp[u][0] = 1;
  105. for (int i = head[u]; i; i = e[i].nxt) {
  106. int v = e[i].v;
  107. if (v == fa)continue;
  108. dfs1(v, u);
  109. for (int k = 0; k <= K; k++) {
  110. ll ut = 1;
  111. for (int j = 0; j <= k; j++) {
  112. dp[u][k] = (dp[u][k] + C[k][j] * dp[v][k - j] % mod*ut%mod) % mod;
  113. dp[u][k] = (dp[u][k] % mod + mod) % mod;
  114. ut = ut * e[i].w%mod;
  115. }
  116. }
  117. }
  118. return;
  119. }
  120.  
  121. void dfs2(int u, int fa) {
  122. for (int i = head[u]; i; i = e[i].nxt) {
  123. int v = e[i].v;
  124. if (v == fa)continue;
  125. ll tmp[20];
  126. for (int k = 0; k <= K; k++) {
  127. tmp[k] = dp[u][k];
  128. ll ut = 1;
  129. for (int j = 0; j <= k; j++) {
  130. tmp[k] = (tmp[k] - C[k][j] * dp[v][k - j] % mod*ut%mod) % mod;
  131. tmp[k] = (tmp[k] % mod + mod) % mod;
  132. ut = ut * e[i].w%mod;
  133. }
  134. }
  135. for (int k = 0; k <= K; k++) {
  136. ll ut = 1;
  137. for (int j = 0; j <= k; j++) {
  138. dp[v][k] = (dp[v][k] + C[k][j] * tmp[k - j] % mod*ut%mod) % mod;
  139. dp[v][k] = (dp[v][k] % mod + mod) % mod;
  140. ut = ut * e[i].w%mod;
  141. }
  142. }
  143. }
  144. for (int i = head[u]; i; i = e[i].nxt) {
  145. int v = e[i].v;
  146. if (v == fa)continue;
  147. dfs2(v, u);
  148. }
  149. }
  150.  
  151. int main()
  152. {
  153. // ios::sync_with_stdio(0);
  154. init();
  155. while (scanf("%d%d",&n,&K)!=EOF) {
  156. // ms(e);
  157. ms(head); tot = 0;
  158. ms(dp);
  159.  
  160. for (int i = 1; i < n; i++) {
  161. int u, v; rdint(u); rdint(v);
  162. ll w; rdllt(w); u++; v++;
  163. addedge(u, v, w); addedge(v, u, w);
  164. }
  165. if (K == 0) {
  166. cout << n - 1 << endl;
  167. continue;
  168. }
  169. dfs1(1, -1); dfs2(1, -1);
  170. ll ans = -inf;
  171. for (int i = 1; i <= n; i++) {
  172. if (ans == -inf || ans > dp[i][K])ans = dp[i][K];
  173. }
  174. printf("%lld\n", 1ll * ans);
  175. }
  176. return 0;
  177. }
  1.  

Nuclear Power Plant ZOJ - 3840 树形dp的更多相关文章

  1. ZOJ 3626(树形DP+背包+边cost)

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3626 题目大意:树中取点.每过一条边有一定cost,且最后要回 ...

  2. ZOJ 3805 (树形DP)

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5337 题目大意:方块连接,呈树形.每个方块有两种接法,一种接在父块 ...

  3. ZOJ 3201 树形dp+背包(简单题)

    #include<cstdio> #include<vector> #include<cstring> #include<iostream> using ...

  4. ZOJ 3188 ZOJ 3188 Treeland Exhibition(树形DP)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3278 题意:给出一棵树,找出一个不大于长度为m的链,使得其他点到该链 ...

  5. 2014 Super Training #9 E Destroy --树的直径+树形DP

    原题: ZOJ 3684 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3684 题意: 给你一棵树,树的根是树的中心(到其 ...

  6. 【转】【DP_树形DP专辑】【9月9最新更新】【from zeroclock's blog】

    树,一种十分优美的数据结构,因为它本身就具有的递归性,所以它和子树见能相互传递很多信息,还因为它作为被限制的图在上面可进行的操作更多,所以各种用于不同地方的树都出现了,二叉树.三叉树.静态搜索树.AV ...

  7. 【DP_树形DP专题】题单总结

    转载自 http://blog.csdn.net/woshi250hua/article/details/7644959#t2 题单:http://vjudge.net/contest/123963# ...

  8. hdu 4756 MST+树形dp ****

    题意:给你n(n = 1000)个二维点,第一个点是power plant,还有n - 1个点是dormitories.然后现在知道有一条寝室到寝室的边是不能连的,但是我们不知道是哪条边,问这种情况下 ...

  9. poj2378 树形DP

    C - 树形dp Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit ...

随机推荐

  1. Bootstrap 简介(Web前端CSS框架)

    目录1.简介2.特点3.组件4.Javascript插件5.定制自己的框架代码6.Bootstrap Less 1.简介Bootstrap是Twitter推出的一个开源的用于前端开发的工具包.它由Tw ...

  2. 求输出和为n的所有连续自然数序列

    这是编程之美中的一道题.编程之美中的题目是这样的: 1+2=3 4+5=9 2+3+4=9 等式的左边都是两个或者两个以上的连续自然数相加,那么是不是所有的整数都可以写成这样的形式? 问题1:写个程序 ...

  3. Makefile 编写规则 - 1

    Makefilen内容 1. 显示规则:显示规则说明了,如何生成一个或多个目标.这是由Makefile指出要生成的文件和文件依赖的文件.2. 隐晦规则:基于Makefile的自动推导功能3. 变量的定 ...

  4. xgboost 调参 !

    https://jessesw.com/XG-Boost/ http://blog.csdn.net/u010414589/article/details/51153310

  5. loj2512 [BJOI2018]链上二次求和

    传送门 分析 咕咕咕 代码 #include<iostream> #include<cstdio> #include<cstring> #include<st ...

  6. 运行maven build报错No goals have been specified for this build.

    运行maven报错: [ERROR] No goals have been specified for this build. You must specify a valid lifecycle p ...

  7. up6-chrome 45+安装教程

    up6-Chrome 45+安装说明 说明:只需要安装up6.exe即可,up6.exe为插件集成安装包. 1.以管理员身份运行up6.exe.up6.exe中已经集成Chrome 45插件.  

  8. MySQL性能调优与架构设计——第6章 MySQL Server 性能的相关因素

    第6章 MySQL Server 性能的相关因素 前言 大部分人都一致认为一个数据库应用系统(这里的数据库应用系统概指所有使用数据库的系统)的性能瓶颈最容易出现在数据的操作方面,而数据库应用系统的大部 ...

  9. Android在一个app中启动另一个App

    Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); Compon ...

  10. 【转】Android自定义控件(三)——有弹性的ListView

    原文地址:http://blog.csdn.net/a105865708/article/details/17959459 上一次我们试验了有弹性的ScrollView.详情 这一次,我们来试验有弹性 ...