Abandoned country

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3449    Accepted Submission(s):
846

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

题意:求最小生成树,再求任意两点距离的期望。

最小生成树用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 ;
}
Sample Output
6 3.33
 
Author
HIT
 
Source

HDU_5723_最小生成树+任意两点距离的期望的更多相关文章

  1. HDU 5723 Abandoned country(kruskal+dp树上任意两点距离和)

    Problem DescriptionAn abandoned country has n(n≤100000) villages which are numbered from 1 to n. Sin ...

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

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

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

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

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

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

  5. 【非原创】codeforces 1060E Sergey and Subway 【树上任意两点距离和】

    学习博客:戳这里 本人代码: 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 con ...

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

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

  7. JAVA 计算地球上任意两点(经纬度)距离

    /** * 计算地球上任意两点(经纬度)距离 * * @param long1 * 第一点经度 * @param lat1 * 第一点纬度 * @param long2 * 第二点经度 * @para ...

  8. caioj 1237: 【最近公共祖先】树上任意两点的距离 在线倍增ST

    caioj 1237: [最近公共祖先]树上任意两点的距离 倍增ST 题目链接:http://caioj.cn/problem.php?id=1237 思路: 针对询问次数多的时候,采取倍增求取LCA ...

  9. php根据地球上任意两点的经纬度计算两点间的距离 原理

    地球是一个近乎标准的椭球体,它的赤道半径为6378.140千米,极半径为6356.755千米,平均半径6371.004千米.如果我们假设地球是一个完美的球体,那么它的半径就是地球的平均半径,记为R.如 ...

随机推荐

  1. WebDev.WebServer40.EXE

    http://www.cnblogs.com/tong-tong/archive/2013/05/02/3049428.html 大学玩asp.net时就发现VS在Debug时会起一个web服务,这东 ...

  2. 猫猫学iOS 之CoreLocation反地理编码小Demo输入经纬度得到城市

    猫猫分享,必须精品 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243 一:效果 输入经纬度,能够得到相应的地名 二:思路 跟地里编码差 ...

  3. Java Web文件下载

    Web文件下载有两种.一种是文件在站点文件夹下.在浏览器中直接输入文件路径就可以下载.如http://www.xxx.com/file.zip.第二种是文件不在站点文件夹下或者文件是动态生成的(导出报 ...

  4. I/O虚拟化

    note:这里主要记录我对IO虚拟化的理解,希望这篇文章对想了解虚拟化IO的同学有点帮助.这是我在看论文[vale,a switched ethernet for virtual machines]的 ...

  5. 【转】linux 远程桌面工具NX

    1.在linux服务器上需要安装3个文件,下载地址为: http://www.nomachine.com/download-package.php?Prod_Id=1977 nxclient-3.4. ...

  6. Python类私有方法的陷阱

    引言 Python不像C++.Java.C#等有明白的公共.私有或受保护的keyword来定义成员函数或属性,它使用约定的单下划线"_"和"__"双下划线作为函 ...

  7. 从es中提取全量数据的shell脚本

    [root@hadoop3 xiaole_chk_url]# sh looh.es.res.sh 100 200 1 % Total % Received % Xferd Average Speed ...

  8. Linux设备驱动模型【转】

    本文转载自:http://blog.csdn.net/xiahouzuoxin/article/details/8943863 版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[+ ...

  9. 36.面板Ext.Panel使用

    转自:https://www.cnblogs.com/linjiqin/archive/2011/06/22/2086620.html 面板Ext.Panel使用 概要 1.Ext.Panel概述 2 ...

  10. shopnc学习

    ---恢复内容开始--- 以前没有怎么接触过shopnc,感觉界面挺漂亮的,不过后来自己需要开发一个电商系统,就顺便参考了下,感觉构架垃圾的一塌糊涂.不过平时做这个系统二次开发的业务比较多,所以简单的 ...