hdu 2736 Average distance
Average distance
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 682 Accepted Submission(s): 244
Special Judge
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 the average distance between two vertices. This value should have either an absolute or a relative error of at most 10-6
5
0 1 6
0 2 3
0 3 7
3 4 2
哎,思路想复杂了,其实蛮简单的:
引:如果暴力枚举两点再求距离是显然会超时的。转换一下思路,我们可以对每条边,求所有可能的路径经过此边的次数:设这条边两端的点数分别为A和B,那 么这条边被经过的次数就是A*B,它对总的距离和的贡献就是(A*B*此边长度)。我们把所有边的贡献求总和,再除以总路径数N*(N-1)/2,即为最 后所求。
每条边两端的点数的计算,实际上是可以用一次dfs解决的。任取一点为根,在dfs的过程中,对每个点k记录其子树包含的点数(包括其自身),设点数为a[k],则k的父亲一侧的点数即为N-a[k]。这个统计可以和遍历同时进行。故时间复杂度为O(n)。
16447376 | 2016-03-06 09:40:59 | Accepted | 2376 | 312MS | 4540K | 2093 B | C++ | czy |
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <stack>
#include <cctype>
#include <vector>
#include <cmath>
#include <map>
#include <queue> #define ll long long
#define N 10005
#define eps 1e-8 using namespace std; int T;
double tot[N];
double cou[N];
double num;
int n; struct PP
{
int to;
double val;
}; vector<PP>Edge[N]; PP te; void add_adge(int from,int to,double val)
{
te.to = to;te.val = val;
Edge[from].push_back(te);
te.to = from;te.val = val;
Edge[to].push_back(te);
} void dfs(int now,int fa)
{
unsigned int i;
PP nt;
cou[now] = ;
for(i = ;i < Edge[now].size();i++){
nt.to = Edge[now][i].to;
nt.val = Edge[now][i].val;
if(nt.to == fa){
continue;
}
dfs(nt.to,now);
tot[now] = tot[now] + tot[nt.to] + (n-cou[nt.to]) * cou[nt.to] * nt.val;
cou[now] = cou[now] + cou[nt.to];
//printf(" now = %d i=%d to=%d tot=%.6f cou=%.6lf\n",now,i,nt.to,tot[now],cou[now]);
}
//printf(" i=%d tot=%.6f cou=%.6f\n",now,tot[now],cou[now]);
} int main()
{
//freopen("in.txt","r",stdin);
scanf("%d",&T);
int i;
int from,to;
double val;
for(int ccnt=;ccnt<=T;ccnt++){
//while(scanf("%lf%lf%lf%lf",&a[0],&a[1],&a[2],&a[3])!=EOF){
scanf("%d",&n);
memset(tot,,sizeof(tot));
memset(cou,,sizeof(cou));
num = 1.0 *n*(n-)/;
for(i=;i<=n;i++){
Edge[i].clear();
}
for(i=;i<=n-;i++){
scanf("%d%d%lf",&from,&to,&val);
add_adge(from,to,val);
}
dfs(,-);
//for(i=0;i<n;i++){
// for(int j=0;j<Edge[i].size();j++){
// printf(" i=%d to=%d val=%.6lf\n",i,Edge[i][j].to,Edge[i][j].val);
// }
// }
for(i=;i<n;i++){
//printf(" i=%d tot=%.6f cou=%.6f\n",i,tot[i],cou[i]);
}
printf("%lf\n",tot[]/num);
}
return ;
}
hdu 2736 Average distance的更多相关文章
- 【hdu 2376】Average distance
[题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=2376 [题意] 让你计算树上任意两点之间的距离的和. [题解] 算出每条边的两端有多少个节点设为n ...
- hdu 4712 Hamming Distance(随机函数暴力)
http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...
- hdu 4712 Hamming Distance 随机
Hamming Distance Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- HDU 4712 Hamming Distance(随机算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4712 题目大意:任意两个数按位异或后二进制中含1的个数被称为海明距离,给定n个数,求出任意其中两个最小 ...
- hdu 4712 Hamming Distance ( 随机算法混过了 )
Hamming Distance Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- HDU 472 Hamming Distance (随机数)
Hamming Distance Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) To ...
- HDU 4712 Hamming Distance(随机算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4712 解题报告:输入n个数,用十六进制的方式输入的,任意选择其中的两个数进行异或,求异或后的数用二进制 ...
- HDU 4217 Hamming Distance 随机化水过去
Hamming Distance Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- HDU ACM 2895-Edit distance
Edit distance Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total ...
随机推荐
- poj1862 Stripies
思路: 简单贪心. 实现: #include <iostream> #include <cstdio> #include <algorithm> #include ...
- CSS层叠的问题、标准文档流、伪类选择器
一.层叠的问题 CSS有两个性质: 1.继承性 2.层叠性:选择器的一种选择能力,谁的权重大就选谁 层叠性又分为: 1).选不中:走继承性 (font.color.text.) 继承性的权重是0 若 ...
- HTML的历史与历史遗留问题
1. <style type="text/css"> 从前,HTML的设计者认为以后应该还会有其他样式,不过如今我们已经醒悟,事实表明,完全可以只使用<style ...
- layer设置弹出全屏
//弹出即全屏 var index = layer.open({ type: , content: 'http://www.layui.com', area: ['300px', '195px'], ...
- vue+element ui项目总结点(一)select、Cascader级联选择器、encodeURI、decodeURI转码解码、mockjs用法、路由懒加载三种方式
不多说上代码: <template> <div class="hello"> <h1>{{ msg }}</h1> <p> ...
- MongoDB最简单的入门教程之四:使用Spring Boot操作MongoDB
Spring Boot 是一个轻量级框架,可以完成基于 Spring 的应用程序的大部分配置工作.Spring Boot的目的是提供一组工具,以便快速构建容易配置的Spring应用程序,省去大量传统S ...
- Python list 增加/插入元素的说明
http://blog.csdn.net/cnmilan/article/details/9259343 在Python中append 用来向 list 的末尾追加单个元素,如果增加的元素是一个lis ...
- myBatis参数处理 myBatis佟刚课程笔记
单个参数:myBatis不会做特殊处理 #{参数名}: 取出参数值 多个参数: myBatis会做特殊处理 多个参数会被封装成一个MAP key:param1 param2.... param10,或 ...
- Hibernate-01 入门
学习任务 Hibernate开发环境的搭建 使用Hibernate对单表进行增删改操作 使用Hibernate按照数据表主键查询 关于Hibernate 简介 Hibernate的创始人Gavin K ...
- github 新建一个仓库后
每次都记不住命令,记一下,防止找不到 echo "# learn18" >> README.md git init git add README.md git comm ...