Nuclear Power Plant ZOJ - 3840 树形dp
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, i ≠ x), 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 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, u ≠ v), 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的更多相关文章
- ZOJ 3626(树形DP+背包+边cost)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3626 题目大意:树中取点.每过一条边有一定cost,且最后要回 ...
- ZOJ 3805 (树形DP)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5337 题目大意:方块连接,呈树形.每个方块有两种接法,一种接在父块 ...
- ZOJ 3201 树形dp+背包(简单题)
#include<cstdio> #include<vector> #include<cstring> #include<iostream> using ...
- ZOJ 3188 ZOJ 3188 Treeland Exhibition(树形DP)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3278 题意:给出一棵树,找出一个不大于长度为m的链,使得其他点到该链 ...
- 2014 Super Training #9 E Destroy --树的直径+树形DP
原题: ZOJ 3684 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3684 题意: 给你一棵树,树的根是树的中心(到其 ...
- 【转】【DP_树形DP专辑】【9月9最新更新】【from zeroclock's blog】
树,一种十分优美的数据结构,因为它本身就具有的递归性,所以它和子树见能相互传递很多信息,还因为它作为被限制的图在上面可进行的操作更多,所以各种用于不同地方的树都出现了,二叉树.三叉树.静态搜索树.AV ...
- 【DP_树形DP专题】题单总结
转载自 http://blog.csdn.net/woshi250hua/article/details/7644959#t2 题单:http://vjudge.net/contest/123963# ...
- hdu 4756 MST+树形dp ****
题意:给你n(n = 1000)个二维点,第一个点是power plant,还有n - 1个点是dormitories.然后现在知道有一条寝室到寝室的边是不能连的,但是我们不知道是哪条边,问这种情况下 ...
- poj2378 树形DP
C - 树形dp Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bit ...
随机推荐
- get-task-allow有什么用
[failed to get the task for process问题] A: Why am I getting "Error launching remote program: fai ...
- rdlc设置指定列隐藏
此用户帐户对提案名称列不可见
- axure可用密钥
axure8.0注册码 激活码:(亲测可用) 用户名:aaa 注册码:2GQrt5XHYY7SBK/4b22Gm4Dh8alaR0/0k3gEN5h7FkVPIn8oG3uphlOeytIajxG ...
- ubuntu 12.04下搭建web服务器(MySQL+PHP+Apache) 教程
最近,经理让我搭建一个服务器,我从网上找了一篇比较好的教程,按教程一步步执行感觉挺顺利,所以准备整理一下这个教程.尊重原版注明出处:http://news.2ky.cn/Linux/ubuntu-12 ...
- 434. Number of Segments in a String 字符串中的单词个数
[抄题]: Count the number of segments in a string, where a segment is defined to be a contiguous sequen ...
- 面试题:jsp九大内置对象
我们常说的JSP有九大内置对象分别为:request.response.session.out.pagecontext.page.exception.application.config. 我们知道, ...
- Hadoop 使用Combiner提高Map/Reduce程序效率
众所周知,Hadoop框架使用Mapper将数据处理成一个<key,value>键值对,再网络节点间对其进行整理(shuffle),然后使用Reducer处理数据并进行最终输出. 在上述过 ...
- 实现一个简单的Plugin
1.准备 1)安装pluginlib_tutorials包: 2)创建一个包 2.创建基类 创建一个基类,所有的插件都将从基类继承,如RegularPolygon基类 创建一个基类头文件 其内容如下 ...
- 【未整理】web.xml加载顺序.RP
一 1.启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点. 2.紧急着,容创建一个Ser ...
- Reading——The Non-Designer's Design Book
看这本书的时候真的好恨没有CS7在手><,不然我百度几张图来模拟下,体验下设计的快感. 人们总是很容易注意到在他们潜意识里存在的东西,比如说这个图: 我们很容易联想到微信,但是3 ...