HDU_5723_最小生成树+任意两点距离的期望
Abandoned country
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3449 Accepted Submission(s):
846
m(m≤1000000) roads to be re-built, the length of each road is wi(wi≤1000000) . Guaranteed that any two w are different. The roads made all the villages connected directly or indirectly
before destroyed. Every road will cost the same value of its length to rebuild.
The king wants to use the minimum cost to make all the villages connected with
each other directly or indirectly. After the roads are re-built, the king asks a
men as messenger. The king will select any two different points as starting
point or the destination with the same probability. Now the king asks you to
tell him the minimum cost and the minimum expectations length the messenger will
walk.
For each test case, the first
line contains two integers n,m indicate the number of villages and the number of roads to be re-built. Next
m lines, each line have three number i,j,w , the length of a road connecting the village i and the village j is w .
two decimal places. They separated by a space.
题意:求最小生成树,再求任意两点距离的期望。
最小生成树用kruscal。期望一开始不会求,看题解发现使用dfs:分别求每条边的贡献,一条边的贡献等于这条边的|左子树|*|右子树|*value。
代码中使用pair和vector
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std; struct Path
{
int x,y;
double value;
} path[]; typedef pair<int ,int> P;
vector <P> edge[]; bool cmp(Path a,Path b)
{
return a.value<b.value;
}
int father[],n,m;; int find(int x)
{
if(x!=father[x])
father[x]=find(father[x]);
return father[x];
} void merge(int a,int b)
{
int x=find(a);
int y=find(b);
if(x!=y)
father[x]=y;
} long long sumv=,sumd=;
void kruscal()
{
for(int i=; i<=n; i++)
father[i]=i;
for(int i=; i<=n; i++)
edge[i].clear();
sort(path,path+m,cmp);
for(int i=; i<m; i++)
{
int x=path[i].x,y=path[i].y,value=path[i].value;
if(find(path[i].x)!=find(path[i].y))
{
sumv+=path[i].value;
merge(path[i].x,path[i].y);
edge[x].push_back(P(y,value));
edge[y].push_back(P(x,value));
}
}
} int dfs(int x,int last)
{
int cnt=;
for(int i=;i<edge[x].size();i++)
{
int y=edge[x][i].first,value=edge[x][i].second;
if(last!=y)
{
int now=dfs(y,x);
cnt+=now;
sumd+=1.0*now*(n-now)*value;
}
}
return cnt+;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
sumv=;
sumd=;
scanf("%d%d",&n,&m);
for(int i=; i<m; i++)
{
scanf("%d%d%lf",&path[i].x,&path[i].y,&path[i].value);
}
kruscal();
dfs(,-);
printf("%I64d %.2lf\n",sumv,sumd*2.0/(1.0*n)/(n-1.0));
}
return ;
}
HDU_5723_最小生成树+任意两点距离的期望的更多相关文章
- HDU 5723 Abandoned country(kruskal+dp树上任意两点距离和)
Problem DescriptionAn abandoned country has n(n≤100000) villages which are numbered from 1 to n. Sin ...
- hdu6446 网络赛 Tree and Permutation(树形dp求任意两点距离之和)题解
题意:有一棵n个点的树,点之间用无向边相连.现把这棵树对应一个序列,这个序列任意两点的距离为这两点在树上的距离,显然,这样的序列有n!个,加入这是第i个序列,那么这个序列所提供的贡献值为:第一个点到其 ...
- HDU 2376 树形dp|树上任意两点距离和的平均值
原题:http://acm.hdu.edu.cn/showproblem.php?pid=2376 经典问题,求的是树上任意两点和的平均值. 这里我们不能枚举点,这样n^2的复杂度.我们可以枚举每一条 ...
- HDU2376Average distance(树形dp|树上任意两点距离和的平均值)
思路: 引:如果暴力枚举两点再求距离是显然会超时的.转换一下思路,我们可以对每条边,求所有可能的路径经过此边的次数:设这条边两端的点数分别为A和B,那 么这条边被经过的次数就是A*B,它对总的距离和的 ...
- 【非原创】codeforces 1060E Sergey and Subway 【树上任意两点距离和】
学习博客:戳这里 本人代码: 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 con ...
- HDU 5723 Abandoned country 【最小生成树&&树上两点期望】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5723 Abandoned country Time Limit: 8000/4000 MS (Java/ ...
- JAVA 计算地球上任意两点(经纬度)距离
/** * 计算地球上任意两点(经纬度)距离 * * @param long1 * 第一点经度 * @param lat1 * 第一点纬度 * @param long2 * 第二点经度 * @para ...
- caioj 1237: 【最近公共祖先】树上任意两点的距离 在线倍增ST
caioj 1237: [最近公共祖先]树上任意两点的距离 倍增ST 题目链接:http://caioj.cn/problem.php?id=1237 思路: 针对询问次数多的时候,采取倍增求取LCA ...
- php根据地球上任意两点的经纬度计算两点间的距离 原理
地球是一个近乎标准的椭球体,它的赤道半径为6378.140千米,极半径为6356.755千米,平均半径6371.004千米.如果我们假设地球是一个完美的球体,那么它的半径就是地球的平均半径,记为R.如 ...
随机推荐
- 关于使用freemarker导出word
java使用FreeMarker导出word 一. 先做一个word模板 二. 将该word文件另存为xml格式(注意是另存为,不是直接改扩展名) 三. 打开xml文件把要 ...
- 条款39: 避免 "向下转换" 继承层次
基类指针不能调用派生类的独有的成员,即使基类指针指向派生类对象,因为编译器是根据指针的静态类型来确定调用对象在内存中占据的空间的.此时可以使用static_cast来转换,但不要这么做,因为向下转换难 ...
- 创建.m文件一片空白的错误解决方式
今天写代码,想继承一个类,突然发现创建的类文件一片空白,如图 之后各种调试发现都解决不了问题,以为是装了xcode6 beta2 版本号的问题,结果发现事实上是我创建错了 我创建的是 watermar ...
- Codeforces Round #272 (Div. 2)C. Dreamoon and Sums 数学推公式
C. Dreamoon and Sums Dreamoon loves summing up something for no reason. One day he obtains two int ...
- J - 玩游戏
小A和小B玩游戏,初始的时候小A给小B一组包含n个数的数组.他们按如下的规则进行: 每次小B得到一组数,他把这组数的和加到自己的分数里面(他的初始分数是0),然后他把这组数还给小A. 如果小A得到的这 ...
- 利用POI抽取word中的图片并保存在文件中
利用POI抽取word中的图片并保存在文件中 poi.apache.org/hwpf/quick-guide.html 1.抽取word doc中的图片 package parse; import j ...
- JAVA Swing 事件监听
EventListner 接口 它是一个标记接口,每一个监听器接口扩展.这个类定义在java.util包. 类声明 以下是声明java.util.EventListener接口: public int ...
- android apk 防止反编译技术第三篇-加密
上一篇我们讲了apk防止反编译技术中的加壳技术,如果有不明白的可以查看我的上一篇博客http://my.oschina.net/u/2323218/blog/393372.接下来我们将介绍另一种防止a ...
- $(document).ready 与 window.onload的区别?
$(document).ready = function(){}; DOM树加载完成时执行,此时文件不一定都已加载完成. window.onload = function(){}; DOM树加载完成 ...
- 慕课网3-10编程练习:简单的flex布局
小伙伴们,我们学习了伸缩容器的一些属性,接下来使用我们所学的伸缩容器属性完成下面的效果图. 完成效果: 任务 1.先将容器设置为伸缩容器 2.在垂直方向上对齐,行与行之间的空白距离一样 3.在水平方向 ...