Problem Description
An abandoned country has n(n≤100000) villages which are numbered from 1 to n. Since abandoned for a long time, the roads need to be re-built. There are m(m≤1000000) roads to be re-built, the length of each road is wi(wi≤1000000). Guaranteed that any two wi 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.

Input
The first line contains an integer T(T≤10) which indicates the number of test cases.

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,wi, the length of a road connecting the village i and the village j is wi.

Output
output the minimum cost and minimum Expectations with two decimal places. They separated by a space.

Sample Input
1
4 6
1 2 1
2 3 2
3 4 3
4 1 4
1 3 5
2 4 6

Sample Output
6 3.33

题意

n个城市m条路,国王想花最少的钱使得任意两个城市连通,最后问你国王任意两个点的距离期望

题解

第一问直接跑个最小生成树

第二问树上任意两点期望,可以发现两两点总数是n*(n-1)/2种情况,然后只需要dp出任意两点距离和,再用距离和/情况数

当n=1的时候直接输出0.00,然后n*(n-1)可能会爆所以得*1LL

代码

 #include <bits/stdc++.h>
using namespace std; #define ll long long const int maxn=1e5+;
const int maxm=1e6+; int n,m;
int f[maxn];
vector< pair<int,ll> >G[maxn];
ll dp[maxn],sum[maxn],ans;
struct edge
{
int u,v;
ll w;
bool operator<(const edge& D)const{
return w<D.w;
}
}edges[maxm];
void dfs(int cur,int fa)
{
sum[cur]=;
for(int i=;i<G[cur].size();i++)
{
int son=G[cur][i].first;
ll w=G[cur][i].second;
if(fa==son)continue;
dfs(son,cur);
sum[cur]+=sum[son];
dp[cur]+=dp[son]+sum[son]*(n-sum[son])*w;
}
}
int find(int x)
{
return f[x]==x?x:f[x]=find(f[x]);
}
void kruskal()
{
for(int i=;i<=n;i++)f[i]=i;
sort(edges,edges+m);
int cnt=;
for(int i=;i<m;i++)
{
int u=edges[i].u;
int v=edges[i].v;
ll w=edges[i].w;
int fu=find(u);
int fv=find(v);
if(fu!=fv)
{
f[fu]=fv;
G[u].push_back({v,w});
G[v].push_back({u,w});
ans+=w;
if(++cnt==n-)return;
}
}
}
int main()
{
int t,u,v;
ll w;
scanf("%d",&t);
while(t--)
{
ans=;
scanf("%d%d",&n,&m);
for(int i=;i<m;i++)
{
scanf("%d%d%lld",&u,&v,&w);
edges[i]={u,v,w};
}
kruskal();
dfs(,);
if(n==)printf("%lld 0.00\n",ans);
else printf("%lld %.2f\n",ans,dp[]*1.0/(1LL*n*(n-)/));
for(int i=;i<=n;i++)
{
dp[i]=sum[i]=;
G[i].clear();
}
}
return ;
}

HDU 5723 Abandoned country(kruskal+dp树上任意两点距离和)的更多相关文章

  1. HDU 5723 Abandoned country 【最小生成树&&树上两点期望】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5723 Abandoned country Time Limit: 8000/4000 MS (Java/ ...

  2. HDU 2376 树形dp|树上任意两点距离和的平均值

    原题:http://acm.hdu.edu.cn/showproblem.php?pid=2376 经典问题,求的是树上任意两点和的平均值. 这里我们不能枚举点,这样n^2的复杂度.我们可以枚举每一条 ...

  3. HDU2376Average distance(树形dp|树上任意两点距离和的平均值)

    思路: 引:如果暴力枚举两点再求距离是显然会超时的.转换一下思路,我们可以对每条边,求所有可能的路径经过此边的次数:设这条边两端的点数分别为A和B,那 么这条边被经过的次数就是A*B,它对总的距离和的 ...

  4. hdu6446 网络赛 Tree and Permutation(树形dp求任意两点距离之和)题解

    题意:有一棵n个点的树,点之间用无向边相连.现把这棵树对应一个序列,这个序列任意两点的距离为这两点在树上的距离,显然,这样的序列有n!个,加入这是第i个序列,那么这个序列所提供的贡献值为:第一个点到其 ...

  5. 最小生成树 kruskal hdu 5723 Abandoned country

    题目链接:hdu 5723 Abandoned country 题目大意:N个点,M条边:先构成一棵最小生成树,然后这个最小生成树上求任意两点之间的路径长度和,并求期望 /************** ...

  6. HDU 5723 Abandoned country 最小生成树+搜索

    Abandoned country Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  7. HDU 5723 Abandoned country(落后渣国)

    HDU 5723 Abandoned country(落后渣国) Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 ...

  8. HDU 5723 Abandoned country(最小生成树 + 树形DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5723 [题目大意] n座城市,m条路径,求解: 1.最短的路径和,使得n座城市之间直接或者间接连通 ...

  9. HDU 5723 Abandoned country (最小生成树 + dfs)

    Abandoned country 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ...

随机推荐

  1. jdk1.7安装和配置

    jdk1.7 安装引用 https://jingyan.baidu.com/article/ff41162596a77912e4823716.html

  2. 根据CPU核心数确定线程池并发线程数

    一.抛出问题 关于如何计算并发线程数,一般分两派,来自两本书,且都是好书,到底哪个是对的?问题追踪后,整理如下: 第一派:<Java Concurrency in Practice>即&l ...

  3. 【转】使用VisualStudio完成自动化C++代码生成和编译工作(GacUI)

    使用VisualStudio完成自动化C++代码生成和编译工作(GacUI)     GacUI终于进入制作dll的阶段了.昨天上传了一个新的工程,在Vczh Library++3.0(E:\Code ...

  4. docker 进程监控 Dumb-Init进程信号处理 --转自https://blog.csdn.net/tiger435/article/details/54971929

    随着docker及Kubernetes技术发展的越来越成熟稳定,越来越多的公司开始将docker用于生产环境的部署,相比起物理机上直接部署,多了一层docker容器的环境,这就带来一个问题:进程信号接 ...

  5. <ROS> 通讯方式之 Action

    Ros 官网介绍 http://wiki.ros.org/actionlib 一个简易的action教程,package -- example_action_server. action文件夹内存放 ...

  6. js延迟

    function sleep(numberMillis) { var now = new Date(); var exitTime = now.getTime() + numberMillis; wh ...

  7. MFC文件处理

    计算机室如何管理自身所存放着的大量的信息的呢?windows的磁盘管理程序为我们提供了一套严密而又高效的信息组织形式--硬盘上的信息是以文件的形式被管理的. 面向存储的文件技术 什么是文件?计算机中, ...

  8. React开发笔记

    项目环境搭建 使用create-react-app CSS使用styled-components yarn add styled-components 引入reset.css样式 import { c ...

  9. Linux 开机、重启和用户登录注销、用户管理

    关机&重启命令 shutdown shutdown -h now:表示立即关机 shutdown -h 1:表示1分钟后关机 shutdown -r now:立即重启 halt 就是直接使用, ...

  10. python——列表入门

    学习列表先分析一段程序: list = ['zx', 'xkd', 1997, 2018] list1=list+[1,2,3]#列表拼接 list2=[list,list1] print('嵌套的列 ...