BZOJ4987:Tree(树形DP)
Description
Input
Output
Sample Input
1 2 35129
2 3 42976
3 4 24497
2 5 83165
1 6 4748
5 7 38311
4 8 70052
3 9 3561
8 10 80238
Sample Output
Solution
那么我们可以进行树形dp,设f[i,j,0/1/2]表示以i为根的子树选了j个点,且直径的端点已经选了0/1/2个的最优方案。
Code
#include<iostream>
#include<cstring>
#include<cstdio>
#define N (3009)
using namespace std; struct Edge{int to,next,len;}edge[N<<];
int n,m,u,v,l,ans,INF,size[N],f[N][N][],tmp[N][];
int head[N],num_edge; void add(int u,int v,int l)
{
edge[++num_edge].to=v;
edge[num_edge].len=l;
edge[num_edge].next=head[u];
head[u]=num_edge;
} void Min(int &x,int y){x=min(x,y);} void Dfs(int x,int fa)
{
size[x]=;
for (int i=;i<=n;i++)
for (int j=;j<=;j++)
f[x][i][j]=INF;
f[x][][]=f[x][][]=f[x][][]=;
for (int i=head[x]; i; i=edge[i].next)
if (edge[i].to!=fa)
{
Dfs(edge[i].to,x);
for (int j=; j<=size[x]+size[edge[i].to]; ++j)
tmp[j][]=tmp[j][]=tmp[j][]=INF;
for (int j=; j<=size[x]; ++j)
for (int k=; k<=size[edge[i].to]; ++k)
{
int to=edge[i].to,len=edge[i].len;
Min(tmp[j+k][],f[x][j][]+f[to][k][]+len*);
Min(tmp[j+k][],f[x][j][]+f[to][k][]+len*);
Min(tmp[j+k][],f[x][j][]+f[to][k][]+len);
Min(tmp[j+k][],f[x][j][]+f[to][k][]+len);
Min(tmp[j+k][],f[x][j][]+f[to][k][]+len*);
Min(tmp[j+k][],f[x][j][]+f[to][k][]+len*);
}
size[x]+=size[edge[i].to];
for (int j=; j<=size[x]; ++j)
for (int k=; k<=; ++k)
f[x][j][k]=min(f[x][j][k],tmp[j][k]);
}
ans=min(ans,f[x][m][]);
} int main()
{
memset(&INF,0x3f,sizeof(INF));
scanf("%d%d",&n,&m);
for (int i=; i<=n-; ++i)
{
scanf("%d%d%d",&u,&v,&l);
add(u,v,l); add(v,u,l);
}
ans=INF; Dfs(,);
printf("%d\n",ans);
}
BZOJ4987:Tree(树形DP)的更多相关文章
- 熟练剖分(tree) 树形DP
熟练剖分(tree) 树形DP 题目描述 题目传送门 分析 我们设\(f[i][j]\)为以\(i\)为根节点的子树中最坏时间复杂度小于等于\(j\)的概率 设\(g[i][j]\)为当前扫到的以\( ...
- hdu-5834 Magic boy Bi Luo with his excited tree(树形dp)
题目链接: Magic boy Bi Luo with his excited tree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: ...
- CF 461B Appleman and Tree 树形DP
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other ...
- codeforces 161D Distance in Tree 树形dp
题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...
- hdu6035 Colorful Tree 树形dp 给定一棵树,每个节点有一个颜色值。定义每条路径的值为经过的节点的不同颜色数。求所有路径的值和。
/** 题目:hdu6035 Colorful Tree 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6035 题意:给定一棵树,每个节点有一个颜色值.定 ...
- 5.10 省选模拟赛 tree 树形dp 逆元
LINK:tree 整场比赛看起来最不可做 确是最简单的题目. 感觉很难写 不过单独考虑某个点 容易想到树形dp的状态. 设f[x]表示以x为根的子树内有黑边的方案数. 白边方案只有一种所以不用记录. ...
- Codeforces Round #263 Div.1 B Appleman and Tree --树形DP【转】
题意:给了一棵树以及每个节点的颜色,1代表黑,0代表白,求将这棵树拆成k棵树,使得每棵树恰好有一个黑色节点的方法数 解法:树形DP问题.定义: dp[u][0]表示以u为根的子树对父亲的贡献为0 dp ...
- codeforces Round #263(div2) D. Appleman and Tree 树形dp
题意: 给出一棵树,每个节点都被标记了黑或白色,要求把这棵树的其中k条变切换,划分成k+1棵子树,每颗子树必须有1个黑色节点,求有多少种划分方法. 题解: 树形dp dp[x][0]表示是以x为根的树 ...
- POJ 2486 Apple Tree(树形DP)
题目链接 树形DP很弱啊,开始看题,觉得貌似挺简单的,然后发现貌似还可以往回走...然后就不知道怎么做了... 看看了题解http://www.cnblogs.com/wuyiqi/archive/2 ...
随机推荐
- Excel的vlookup函数的用法
VLOOKUP函数用于搜索指定区域内首列满足条件的元素,确定待检测单元格在区域中的行序号,再进一步返回选定单元格的值. 为了讲解的需要,特制作如图所示的表格.当然,大家也可以根据自己的实际情况设计合适 ...
- table 中的tr 行点击 变换颜色背景
<style> table{border-collapse: collapse;border-spacing: 0; width: 100%;} table tr th,td{border ...
- HTML5--(3)过渡+动画+颜色+文本
一.过渡transition transition-property指定属性名称 (如width.height.background-color.内外边距) all 所有属性都将获得过渡效果(默认) ...
- SQL Server中的小技巧(重复、替换、截取、去空格、去小数点后的位数)
PS:随笔写的在SQL Server中要用到的 (重复.替换.截取.去空格.去小数点后的位数) /*---------------------------重复--------------------- ...
- hdu 3999 二叉查找树
The order of a Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDU-2046 骨牌铺方格【递推】
http://acm.hdu.edu.cn/showproblem.php?pid=2046 和前面的一样,a[i] = a[i-1] + a[i-2] #include<iostream> ...
- HDU 3466(01背包变种
http://acm.hdu.edu.cn/showproblem.php?pid=3466 http://www.cnblogs.com/andre0506/archive/2012/09/20/2 ...
- axios中的qs
qs是一个npm仓库所管理的包,可通过npm install qs命令进行安装. 1. qs.parse()将URL解析成对象的形式 const Qs = require('qs'); let url ...
- 洛谷P4781 【模板】拉格朗日插值(拉格朗日插值)
题意 题目链接 Sol 记得NJU有个特别强的ACM队叫拉格朗,总感觉少了什么.. 不说了直接扔公式 \[f(x) = \sum_{i = 1}^n y_i \prod_{j \not = i} \f ...
- 淘宝 NPM 镜像
使用说明 : 更多见 https://npm.taobao.org 你可以使用我们定制的 cnpm (gzip 压缩支持) 命令行工具代替默认的 npm: $ npm install -g cnpm ...