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. linux多线程默认栈大小和最大线程数

    linux的线程栈大小可以使用ulimit -s查看,对于ubunt 2.6的内核线程栈的默认大小为8M,如下: shine@shine-bupt:~/Program/C$ ulimit -s 819 ...

  2. UIThread

    功能如下:点击Create则新创建一个窗口 一 . 资源中添加对话框,右键添加类MyDlg 双击初始对话框中的按钮,实现按钮功能:点击则创建一个对话框 CMyDialog* pDialog = new ...

  3. css四可见,部分可见和重叠半透明

    <html> <head> <title>javascript</title> <style type="text/css"& ...

  4. C# ShowDialog时窗体贱传值得方法

    用C#开发应用的时候,通常需要多个窗体!有时候为了打开窗体的时候禁止操作父窗体,我们一般采用模态对话框的方法,也算就是用ShowDialog()方法. 假设有两个窗体A和B,A作为主窗体使用ShowD ...

  5. conda create -n AlphaPose2018 python=2.7

    conda create -n AlphaPose2018 python=2.7Solving environment: done ==> WARNING: A newer version of ...

  6. SpringMVC第二天

    SpringMVC第二天   框架课程 1. 课程计划 1.高级参数绑定 a) 数组类型的参数绑定 b) List类型的绑定 2.@RequestMapping注解的使用 3.Controller方法 ...

  7. libcurl用法

    本文以向百度搜索开放平台搜索关键字所对应的推荐搜索条目为例子: url:http://m.baidu.com/su?wd=%s&action=opensearch&ie=utf-8 ( ...

  8. 2.一个简单的c++程序。

    每个程序员的Hello World程序 //This is a small c++ program #include <iostream> int main() { std::cout & ...

  9. 第一章Python简介

    1.Python shell(Python命令解释器) 如下所示 2.Python的交互模式 如下 3.代码编辑器窗口 在上面的那些指令称为源代码. 4.在python中,缩进是有语法意义的. 在某行 ...

  10. .NET将服务器文件导出

    导出文件:                string filePath = Server.UrlDecode(filePath);                if (File.Exists(fi ...