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

3 2
0 1 2
1 2 2

Sample Output

8

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<time.h>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 100005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
#define mclr(x,a) memset((x),a,sizeof(x))
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 100000007;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-5
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii; inline int rd() {
int x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/
int n, K;
struct node {
int u, v;
ll w;
int nxt;
}e[maxn<<1]; ll dp[maxn][12];
int head[maxn];
int tot;
ll C[14][14]; void init() {
C[0][0] = C[1][1] = C[1][0] = 1;
for (int i = 2; i <= 13; i++) {
C[i][i] = C[i][0] = 1;
for (int j = 1; j < i; j++) {
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
C[i][j] %= mod;
}
}
} void addedge(int u, int v, ll w) {
e[++tot].u = u; e[tot].v = v; e[tot].w = w; e[tot].nxt = head[u]; head[u] = tot;
} void dfs1(int u, int fa) {
// ms(dp[u]);
dp[u][0] = 1;
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].v;
if (v == fa)continue;
dfs1(v, u);
for (int k = 0; k <= K; k++) {
ll ut = 1;
for (int j = 0; j <= k; j++) {
dp[u][k] = (dp[u][k] + C[k][j] * dp[v][k - j] % mod*ut%mod) % mod;
dp[u][k] = (dp[u][k] % mod + mod) % mod;
ut = ut * e[i].w%mod;
}
}
}
return;
} void dfs2(int u, int fa) {
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].v;
if (v == fa)continue;
ll tmp[20];
for (int k = 0; k <= K; k++) {
tmp[k] = dp[u][k];
ll ut = 1;
for (int j = 0; j <= k; j++) {
tmp[k] = (tmp[k] - C[k][j] * dp[v][k - j] % mod*ut%mod) % mod;
tmp[k] = (tmp[k] % mod + mod) % mod;
ut = ut * e[i].w%mod;
}
}
for (int k = 0; k <= K; k++) {
ll ut = 1;
for (int j = 0; j <= k; j++) {
dp[v][k] = (dp[v][k] + C[k][j] * tmp[k - j] % mod*ut%mod) % mod;
dp[v][k] = (dp[v][k] % mod + mod) % mod;
ut = ut * e[i].w%mod;
}
}
}
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].v;
if (v == fa)continue;
dfs2(v, u);
}
} int main()
{
// ios::sync_with_stdio(0);
init();
while (scanf("%d%d",&n,&K)!=EOF) {
// ms(e);
ms(head); tot = 0;
ms(dp); for (int i = 1; i < n; i++) {
int u, v; rdint(u); rdint(v);
ll w; rdllt(w); u++; v++;
addedge(u, v, w); addedge(v, u, w);
}
if (K == 0) {
cout << n - 1 << endl;
continue;
}
dfs1(1, -1); dfs2(1, -1);
ll ans = -inf;
for (int i = 1; i <= n; i++) {
if (ans == -inf || ans > dp[i][K])ans = dp[i][K];
}
printf("%lld\n", 1ll * ans);
}
return 0;
}

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. NPOI-WebForm_Excel导入与导出

    本文面对的是第一次 接触NPOI的童鞋 不必为了一些琐碎的事情搞的心情烦躁 废话不多说先上 Demo 的全家福 接下来直接上代码 public partial class _Default : Sys ...

  2. Python常用算法

    本节内容 算法定义 时间复杂度 空间复杂度 常用算法实例 1.算法定义 算法(Algorithm)是指解题方案的准确而完整的描述,是一系列解决问题的清晰指令,算法代表着用系统的方法描述解决问题的策略机 ...

  3. C#支持的编码格式

    转自: http://www.java2s.com/Book/CSharp/0040__Essential-Types/Get_all_supported_encodings.htm using Sy ...

  4. EKF model&realization

    REF: https://pythonrobotics.readthedocs.io/en/latest/modules/localization.html#unscented-kalman-filt ...

  5. IWebBrowser和IE浏览器的行为不一样

    原本一直以为IWebBrowser2的行为和IE浏览器的行为应该是一样的,但是最近发现事实不是如此. IE8以后的浏览器都带有兼容模式,而IWebBrowser2默认情况下是在兼容模式下运行的,可以参 ...

  6. =面试题:java面试基本方向 背1 有用 项目二技术学完再看

    一.Java基础 1. 集合框架A)集合中泛型优点? 将运行期的ClaasCastException 转到编译期异常.  泛型还提供通配符 1)HashMap---允许一个键为null,允许多个值为n ...

  7. Spring 实例化bean的三种方式

    第一种方法:直接配置Bean <bena id="所需要实例化的一个实例名称" class="包名.类名"/> 例如: 配置文件中的bean.XML ...

  8. Filter过滤器简单学习

    Servlet 过滤器方法 过滤器是一个实现了 javax.servlet.Filter 接口的 Java 类.javax.servlet.Filter 接口定义了三个方法: 序号 方法 & ...

  9. 远程访问Function时报错Remote table-valued function calls are not allowed.

    开始是这样调用的:select * from [LinkedServer].[db name].dbo.[function name](param1, param2) 原因: Only table-v ...

  10. 解决jquery操作checkbox火狐下第二次无法勾选问题

    最近在学习jQuery(版本jquery-1.9.1.js),要求用jQuery实现全选/全不选.反选,在IE(IE8)中没有问题,但在火狐浏览器中调试的时候出现了一些小问题,达不到效果. html代 ...