HDU 5723 Abandoned country(kruskal+dp树上任意两点距离和)
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树上任意两点距离和)的更多相关文章
- HDU 5723 Abandoned country 【最小生成树&&树上两点期望】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5723 Abandoned country Time Limit: 8000/4000 MS (Java/ ...
- HDU 2376 树形dp|树上任意两点距离和的平均值
原题:http://acm.hdu.edu.cn/showproblem.php?pid=2376 经典问题,求的是树上任意两点和的平均值. 这里我们不能枚举点,这样n^2的复杂度.我们可以枚举每一条 ...
- HDU2376Average distance(树形dp|树上任意两点距离和的平均值)
思路: 引:如果暴力枚举两点再求距离是显然会超时的.转换一下思路,我们可以对每条边,求所有可能的路径经过此边的次数:设这条边两端的点数分别为A和B,那 么这条边被经过的次数就是A*B,它对总的距离和的 ...
- hdu6446 网络赛 Tree and Permutation(树形dp求任意两点距离之和)题解
题意:有一棵n个点的树,点之间用无向边相连.现把这棵树对应一个序列,这个序列任意两点的距离为这两点在树上的距离,显然,这样的序列有n!个,加入这是第i个序列,那么这个序列所提供的贡献值为:第一个点到其 ...
- 最小生成树 kruskal hdu 5723 Abandoned country
题目链接:hdu 5723 Abandoned country 题目大意:N个点,M条边:先构成一棵最小生成树,然后这个最小生成树上求任意两点之间的路径长度和,并求期望 /************** ...
- HDU 5723 Abandoned country 最小生成树+搜索
Abandoned country Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- HDU 5723 Abandoned country(落后渣国)
HDU 5723 Abandoned country(落后渣国) Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 ...
- HDU 5723 Abandoned country(最小生成树 + 树形DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5723 [题目大意] n座城市,m条路径,求解: 1.最短的路径和,使得n座城市之间直接或者间接连通 ...
- HDU 5723 Abandoned country (最小生成树 + dfs)
Abandoned country 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ...
随机推荐
- spring中使用@PostConstruct和@PreConstruct注解
1.@PostConstruct说明 被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,类似于Serclet的inti()方法.被@PostCo ...
- windows 2008R2系统程序运行提示无法定位程序输入点ucrtbase.terminate
1.用python写了个脚本,打成exe程序,在一些机器上正常运行,再另外一些机器上运行提示 无法定位程序输入点ucrtbase.terminate 应该是缺少库文件支持 2.网上搜了下.https: ...
- jenkins使用(ubuntu16.0环境)
本文总结了使用jenkins过程.大部分是网上链接,以后自已查看使用. ssh远程链接服务器 检查是否开启ssh ps -ef|grep ssh 1.安装ssh 2.开启root用户 3.充许ro ...
- Beautiful Soup库基础用法(爬虫)
初识Beautiful Soup 官方文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc/# 中文文档:https://www.crumm ...
- 1T硬盘获3T体验 彻底解决NVR存储时间短的问题
随着高清技术的进步,现在300W和400W的IPC越来越普及,但同时带来了更多的成本及存储便利问题.“硬盘存了7天就满了”.“同样大小的硬盘,存储时间越来越短”......为啥你的NVR不能存更长的时 ...
- Power Designer 转C#实体类方法
1.打开Power Designer菜单 Tools,选择如图 2.弹出方框中选择PD安装目录下的如图地址 3.object language选择正确目录后,可选如图语言,如C#.再填写name ...
- solr字段压缩属性compressed新版本已经移除
solr字段压缩属性compressed新版本已经移除 可能是考虑到压缩意义不大还减少搜索效率,所以去掉了.而且好像没有替代属性.
- 6.HTML+CSS制作一双眼睛
效果地址:https://codepen.io/flyingliao/pen/oOLodJ?editors=1100 其它动画效果地址:1.https://scrimba.com/c/cJ8NPpU2 ...
- orcal 安装失败解决办法
若安装失败
- Idea中类上有叉的解决方法
idea中类的头上出现X解决办法 ctrl+alt+s 在弹出的菜单上选择Compiler下的Excludes 右边会有 移除掉,点击ok, 重启idea就可以了