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.如 ...
随机推荐
- Ubuntu 16.04常用快捷键(转)
注意:在Linux下Win键就是Super键 启动器 Win(长按) 打开启动器,显示快捷键 Win + Tab 通过启动器切换应用程序 Win + 1到9 与点击启动器上的图标效果一样 Win + ...
- 自然语言处理中的Attention Model:是什么及为什么
/* 版权声明:能够随意转载.转载时请标明文章原始出处和作者信息 .*/ author: 张俊林 要是关注深度学习在自然语言处理方面的研究进展,我相信你一定听说过Attention Model(后文有 ...
- smart pointer
smart pointer是一种abstract data type,它可以模仿指针的行为,而且额外提供了一系列诸如自己主动内存管理.边界检查等特性,这些特性是为了在保证效率的基础上降低因为对指针的不 ...
- HDU 1421 搬寝室 (线性dp 贪心预处理)
搬寝室 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- framework/base子目录
framework/base下各子目录 ~/src/aosp_master/frameworks $ tree base/ -L 1 base/ ├── Android.bp ├── Android. ...
- css list menu
选择让page和folder都显示出来
- ref 和out的区别
在C#语言中,参数的传递有两种,一种是值传递,一种是引用传递.ref与out这两种方式都属于引用传递,只是他们的用法稍有不同.下面看几个例子 使用ref的例子 class test { static ...
- 69.资金管理-税率表管理extjs 页面
1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8&quo ...
- Ubuntu下FileZilla的安装(转载)
转自:http://os.51cto.com/art/201103/247564.htm FileZilla是一个免费而且开源的FTP客户端软件,共有两种版本:客户端版本.服务器版本.FileZill ...
- myeclipse配背景色
1:使jsp,html等页面为纯黑色. 2:发现JS的背景颜色还是默认的.而且还是块状的.将它设置背景为黑色的. *效果图: 参考:http://blog.csdn.net/ltqwby/articl ...