HDU 2376 Average distance (树形dp)
来源:互联网 发布:批量打印pdf软件 编辑:程序博客网 时间:2024/06/01 08:14
Average distance
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 588 Accepted Submission(s): 213
Special Judge
Problem DescriptionGiven a tree, calculate the average distance between two vertices in the tree. For example, the average distance between two vertices in the following tree is (d01 + d02 + d03 + d04 + d12 +d13 +d14 +d23 +d24 +d34)/10 = (6+3+7+9+9+13+15+10+12+2)/10 = 8.6.
Input
On the first line an integer t (1 <= t <= 100): the number of test cases. Then for each test case:
One line with an integer n (2 <= n <= 10 000): the number of nodes in the tree. The nodes are numbered from 0 to n - 1.
n - 1 lines, each with three integers a (0 <= a < n), b (0 <= b < n) and d (1 <= d <= 1 000). There is an edge between the nodes with numbers a and b of length d. The resulting graph will be a tree.
One line with an integer n (2 <= n <= 10 000): the number of nodes in the tree. The nodes are numbered from 0 to n - 1.
n - 1 lines, each with three integers a (0 <= a < n), b (0 <= b < n) and d (1 <= d <= 1 000). There is an edge between the nodes with numbers a and b of length d. The resulting graph will be a tree.
Output
For each testcase:
One line with the average distance between two vertices. This value should have either an absolute or a relative error of at most 10-6
One line with the average distance between two vertices. This value should have either an absolute or a relative error of at most 10-6
150 1 60 2 30 3 73 4 2
8.6
bapc2007
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2376
题目大意:一棵树,求任意两点间的平均距离
题目分析:先求出任意两点间的距离和,再除总边数(这里的边指的是任意两点间的直接路径数 n * (n - 1) / 2),求和显然没能枚举,我们可以发现一条路径被经过的次数为其左边点数*右边点数,所以根据这个我们可以算出每条边对总值的贡献,树形dp求一下即可
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2376
题目大意:一棵树,求任意两点间的平均距离
题目分析:先求出任意两点间的距离和,再除总边数(这里的边指的是任意两点间的直接路径数 n * (n - 1) / 2),求和显然没能枚举,我们可以发现一条路径被经过的次数为其左边点数*右边点数,所以根据这个我们可以算出每条边对总值的贡献,树形dp求一下即可
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int const MAX = 1e4 + 5;int n, cnt, head[MAX], num[MAX];double sum;struct EDGE{int v, w, next;}e[2 * MAX];void Add(int u, int v, int w){e[cnt].v = v;e[cnt].w = w;e[cnt].next = head[u];head[u] = cnt ++;}void DFS(int u, int fa){num[u] = 1;for(int i = head[u]; i != -1; i = e[i].next){int v = e[i].v;int w = e[i].w;if(v != fa){DFS(v, u);num[u] += num[v];sum += 1.0 * num[v] * (n - num[v]) * w;}}}int main(){int T;scanf("%d", &T);while(T --){cnt = 0;sum = 0;memset(head, -1, sizeof(head));scanf("%d", &n);for(int i = 0; i < n - 1; i++){int u, v, w;scanf("%d %d %d", &u, &v, &w);Add(u, v, w);Add(v, u, w);}DFS(0, -1);printf("%.7f\n", sum / (1.0 * n * (n - 1) / 2.0));}}
0 0
- HDU 2376 Average distance (树形dp)
- HDU 2376 Average distance 树形dp
- zoj 2912 hdu 2376 Average distance 树形
- HDU 2376 Average distance(树上两点间的期望)
- hdu 2376 Average distance DFS 求树上任意两点距离和
- 树形dp hdu Computer
- 【树形DP】hdu 1520
- hdu 1054 #树形DP
- hdu 4303 树形dp
- hdu 4340 树形dp
- hdu 4340 树形DP
- HDU 4340 树形DP
- hdu 4267 树形dp
- hdu 4383 树形dp
- HDU 2196 树形dp
- HDU 2196 树形dp
- hdu 1054 树形dp
- HDU 4276 树形DP
- C语言scanf函数详细解释
- 性能测试(并发负载压力)测试分析
- #ifndef, #define, #endif 作用
- Linux下安装MYSQL
- js字符串拼接时,日期控件中的双引号需要转义,不能用\"转义,要用"
- HDU 2376 Average distance (树形dp)
- 上传图片的方法
- C语言 输出系统时间
- poj 3259 Wormholes(基础Bellman-Ford)
- Android studio使用的小问题,小技巧总结----持续更新中
- Vim 实用技术,第 2 部分: 常用插件
- Why does my NSDateFormatter sometimes return an a.m. or p.m. with yyyyMMddHHmmssSSS
- SSH学习四 Hibernate多对一的关系 + 增删改查
- SQL的四种连接-左外连接、右外连接、内连接、全连接