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 ...
随机推荐
- C# 以共享只读方式打开被其它程序占用的文件
iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read, Sys ...
- 子数组的最大异或和---Trie
异或的运算法则为:0⊕0=0,1⊕0=1,0⊕1=1,1⊕1=0(同为0,异为1),这些法则与加法是相同的,只是不带进位,所以异或常被认作不进位加法. 前缀树详解:https://www.cnblog ...
- Linux 开机启动 php socket
问题 php socket 服务在服务器重启后无法自动启动,需要添加开机启动脚本.有以下问题 开机延迟3分钟后,再启动socket服务 socket服务有3个模块需要按照先后顺序启动 registe ...
- py-day3-4 python 匿名函数
# 匿名函数 lamdba name = 'xiaoma' f = lambda x:x+'jun' res = f(name) print('匿名函数的运行结果:',res) 匿名函数的运行结果: ...
- python网页爬虫开发之五-反爬
1.头信息检查是否频繁相同 随机产生一个headers, #user_agent 集合 user_agent_list = [ 'Mozilla/5.0 (Windows NT 6.1; WOW64 ...
- wordpress 解决文章内http链接问题
1. 登录Wordpress后台, 常规设置 > 里面把站点URL 修改成 https开头 2. 登录phpmyadmin , 执行替换链接的SQL 替换wordpress配置的链接地址 (可 ...
- 论文 | YOLO(You Only Look Once)目标检测
论文:You Only Look Once: Unified, Real-Time Object Detection 原文链接:https://arxiv.org/abs/1506.02640 背景介 ...
- nodejs模块循环引用讲解
CommonJS 模块的重要特性是加载时执行,即脚本代码在require的时候,就会全部执行.一旦出现某个模块被"循环加载",就只输出已经执行的部分,还未执行的部分不会输出. 让我 ...
- win10 64 + VS2010 + Opencv 2.4.9 + HIKVISION(海康)
海康相机型号:DS-2CD2512F-IS 参考连接http://blog.csdn.net/wanghuiqi2008/article/details/31404571 先上效果图 其中,在连接时遇 ...
- java.lang.NoClassDefFoundError: org/apache/tomcat/util/res/StringManager
一个比较老的web项目, IDEA 导入后不能用, 出现了各种问题, 但是, 别人用eclipse 导入就不会有问题, 我折腾了半天, 还是各种问题, 真是郁闷了. 哎, 承认很难配置吧, ...