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. Python基础学习六 操作Redis

    import redis r = redis.Redis(host=',db=1) #set get delete setex 都是针对string类型的 k-v # r.set('louis_ses ...

  2. spring不能注入静态变量的原因

    静态方法是属于类(class)的,普通方法才是属于实体对象(也就是New出来的对象)的,spring注入是在容器中实例化对象,所以不能使用静态方法. @Autowired private static ...

  3. Linux下安装Python2.7

    Linux下安装Python2.7 CentOS6.8 中默认安装了Python2.6,但是很多应用需要使用Python2.7,于是要学会如何在Linux下安装Python2.7,这里记录一下,免得以 ...

  4. 图论算法》关于tarjan算法两三事

    关于tarjan,在下觉得这个算法从本质上是一种暴力求强连通分量的方法,但事实上这也是最有效的求强连通分量的方法之一,它对于处理各种强连通分量中奇怪问题,都可以直接转化,所以比较通用和常见. 什么是t ...

  5. Installing R under Unix-alikes

    Linux上R的安装 可参考https://cran.r-project.org/doc/manuals/r-release/R-admin.html#Installing-R-under-Unix_ ...

  6. Luogu 2868 [USACO07DEC]观光奶牛Sightseeing Cows

    01分数规划复习. 这东西有一个名字叫做最优比率环. 首先这个答案具有单调性,我们考虑如何检验. 设$\frac{\sum_{i = 1}^{n}F_i}{\sum_{i = 1}^{n}T_i} = ...

  7. JMS-消息中间件的应用02-安装ActiveMQ-来自慕课学习-新手学习

    What is ActiveMQ?       -----突然好想打英文,好奇怪 请看来自官网的介绍: Apache ActiveMQ ™ is the most popular and powerf ...

  8. 跨域问题hbuilder

    1.借助jquery-jsonp插件 $.jsonp({ url: url, data: { 'name': usd, 'passwd': pass }, callbackParameter: &qu ...

  9. [GO]随机生成切片元素并使用冒泡排序方式进行排序

    package main import ( "math/rand" "time" "fmt" ) func ButtleData(s []i ...

  10. 列表推导式对比For循环执行效率

    我们在前面的学习中都知道,如果把1-10以内的元素追加到一个新的列表表中,如果使用for循环我们可以这么做: a = [] for i in range(1,11): a.append(i) prin ...