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. laravel5.5更新到laravel5.7

    为什么要更新呢?因为项目用的第三方后台扩展包,有很些bug,不够完美.想要一个漂亮的后台,那个后台只支持5.7. 然后,我就开始更新框架了. 修改后:"php": "&g ...

  2. Cisco路由器配置ADSL上网

    cisco1841#sh run Building configuration... Current configuration : 2970 bytes ! version 12.4 service ...

  3. swift 2.0语法 元组

    import UIKit /*: 元祖 * 可以将多个值保存在一起 * 格式: (数值1, 数值2, 数值3) * 特点: 元祖可以保存不同数据类型的值 * 用途: 在C/OC中如果一个函数想返回多个 ...

  4. intellij idea 写 Helloworld

    http://www.jetbrains.com/idea/webhelp/creating-and-running-your-first-java-application.html Creating ...

  5. 是否能重拾Linux下Init 3的快感?

     对于Windows大多数程序猿(眼下).是否非常怀念Linux下全字符界面的炫酷与优越感? 是否仍然停留在cmd后,将文件拖到dos下简单的操作呢?以下是近期研究在Windows下用全命令行的方 ...

  6. postgis经常使用函数介绍(一)

    概述: 在进行地理信息系统开发的过程中,经常使用的空间数据库有esri的sde,postgres的postgis以及mySQL的mysql gis等等,在本文.给大家介绍的是有关postgis的一些经 ...

  7. openStack aio nova service-list neutron ext-list

  8. Access restriction:The type JPEGCodec is not accessible due to restriction on required library C:\Program Files\Java\jre6\lib\rt.jar 报错

    报错: Access restriction:The type JPEGCodec is not accessible due to restriction on required library C ...

  9. 【转载】基于AFNetWorking3.0的图片缓存分析

    原文出处: Yasin的简书 理论 不喜欢理论的可以直接跳到下面的Demo实践部分 缓存介绍 缓存按照保存位置可以分为两类:内存缓存.硬盘缓存(FMDB.CoreData…).我们常说的网络请求缓存包 ...

  10. ubuntu/linuxmint下java环境变量设置

    1.root权限下使用vi或gedit打开/etc目录下的profile文件,末尾加入环境变量. 1)命令: sudo gedit /etc/profile 2)环境变量个人案例: export JA ...