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
Sample Input

Sample Output
3.33
题意:
  给定n个定点,m条边,求最小生成树以及任意两点之间距离的期望。
思路:
  任意两点的期望是(权值)*(这条路被走过的次数)的总和   除以  总共的路径数。
  比如从A到B距离为2,从B到C距离为3。那么从A到B走过2一次,从A到C走过2一次,走过3一次,从B到C走过3一次。
  所以期望为(2*2+3*2)/ 3 = 3.33。
  这里不能求最短路,超时。
  应该用深搜回溯,找出这条路被走过过少次。这样就可以求出期望了。
  这里由于数据量比较大,不能用prim邻接矩阵求最小生成树(vector应该可以),可以用Kruskal求最小生成树。
  坑点:注意最后求期望的时候总共的路径数量n*(n-1)/2数据量比较大,应该用long long或者double存储。
/*
3
0 0
3 3
1 2 1
2 3 2
1 3 5
4 6
1 2 1
2 3 2
3 4 3
4 1 4
1 3 5
2 4 6
*/
#include<cstdio>
#include<algorithm>
#include<vector>
#include<iostream>
#include<string.h>
using namespace std;
vector<pair<int,int> > v[]; struct Edge
{
int f,t,q;
}; int m,n;//n为村庄数,m为街道数
Edge s[];//存储图
long long ans;//存最后的每条路的总和
int pre[];//并查集的祖先数组
int vis[];//标记数组 bool cmp(Edge a,Edge b )//排序函数
{
return a.q<b.q;
} int Find(int x)//找祖先
{
if(x!=pre[x])
{
pre[x]=Find(pre[x]);
}
return pre[x];
} void Merge(int x,int y)//查是否相等
{
int fx=Find(x);
int fy=Find(y);
if(fx!=fy)
pre[fx]=fy;
} long long dfs(int x) //dfs递归搜索
{
vis[x]=;
long long now=,all=;//now记录当前节点直接连接的节点数量 all记录此节点经过搜索后所有的与此节点连接的节点数
int h=v[x].size();
for(int i=; i<h; i++)
{
int b=v[x][i].first;
if(!vis[b])
{
now=dfs(b);
all+=now;
ans+=now*(n-now)*v[x][i].second;//ans记录的是权值*经过的次数
}
}
return all;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
ans=;
memset(vis,,sizeof(vis));
scanf("%d%d",&n,&m);
if(m==||n==)
{printf("0 0.00");continue;} for(int i=;i<=n;i++)
v[i].clear();
for(int i=; i<=n; i++)//并查集的祖先节点的初始化
{
pre[i]=i;
}
for(int j=; j<m; j++)//输入
{
scanf("%d%d%d",&s[j].f,&s[j].t,&s[j].q);
}
sort(s,s+m,cmp);//排序
long long sum=;//sum用来记录最小生成树的长度
for(int j=; j<m; j++)
{
int fx=Find(s[j].f);
int fy=Find(s[j].t);
if(fx!=fy) //如果祖先不相等,那么加入到最小生成树中
{
sum=sum+s[j].q;
Merge(fx,fy);
//加入到动态数组中准备做期望
v[s[j].f].push_back(make_pair(s[j].t,s[j].q));
v[s[j].t].push_back(make_pair(s[j].f,s[j]. q));
}
}
dfs();//深搜回溯计算ans的值
double y=1.0*n*(n-)/;
printf("%lld %.2lf\n",sum,(double)ans/y);
}
return ;
}
 

HDU5723 Abandoned country (最小生成树+深搜回溯法)的更多相关文章

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

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

  2. 深搜+回溯 POJ 2676 Sudoku

    POJ 2676 Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17627   Accepted: 8538 ...

  3. hdu 5648 DZY Loves Math 组合数+深搜(子集法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5648 题意:给定n,m(1<= n,m <= 15,000),求Σgcd(i|j,i&am ...

  4. ****Curling 2.0(深搜+回溯)

    Curling 2.0 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total ...

  5. hdu 5723 Abandoned country 最小生成树 期望

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

  6. The 2016 ACM-ICPC Asia China-Final L World Cup(深搜+回溯 暴力求解)

    题目分析: 对于A,B,C,D四支队伍,两两之间进行一场比赛,获胜得3分,平局得1分,失败不得分,现在对给出的四个队伍的得分,判断能否满足得到这种分数,且方案唯一输出yes,不唯一输出no,不可能则输 ...

  7. hdu 5723 Abandoned country 最小生成树+子节点统计

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

  8. UVA 165 Stamps (DFS深搜回溯)

     Stamps  The government of Nova Mareterrania requires that various legal documents have stamps attac ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5723 n个村庄m条双向路,从中要选一些路重建使得村庄直接或间接相连且花费最少,这个问题就是很明显的求最 ...

随机推荐

  1. oracle备份、还原

    ----第一步:设置空表导出 ----由于Oracle 11G在用EXPORT导出时,空表不能导出.解决方法如下:(11G中有个新特性,当表无数据时,不分配segment,以节省空间) select ...

  2. Mac, Linux中配置Latex中文字体

    对于中文的latex文档,在Linux下一般可以使用系统自带的开源字体:文泉驿(WenQuanYi)来实现,即如下的最小例子,通过xelatex命令来编译即可生成中文文档. \documentclas ...

  3. 10分钟就能学会的.NET Core配置

    .NET Core为我们提供了一套用于配置的API,它为程序提供了运行时从文件.命令行参数.环境变量等读取配置的方法.配置都是键值对的形式,并且支持嵌套,.NET Core还内建了从配置反序列化为PO ...

  4. url地址栏拼接参数写法

    <script> function jiedan_do(elm){ var id=$(elm).attr("a"); window.location="__U ...

  5. ECMAScript 6 中的快捷语法汇总及代码示例

    对于每个 JavaScript 开发人员,快捷语法都是必备技能之一,下面就集中介绍这些快捷语法. 三元运算符 传统写法 const x = 20; let answer; if (x > 10) ...

  6. [leetcode-521-Longest Uncommon Subsequence I]

    Given a group of two strings, you need to find the longest uncommon subsequence of this group of two ...

  7. 关于引入多个jquery冲突的问题(附一个很好用的validate前端验证框架及使用方法)

    废话不多说,进入正题: 如果一个jsp中想要使用两个不同版本的jquery怎么办呢?客官往下看: <script src="${ctxStatic}/jquery/jquery-1.8 ...

  8. v2013调试无法访问此网站 localhost 拒绝了我们的连接请求

    问题描述:          别人给的服务器代码,在本地部署以后调试的,localhost:8080 可以访问,localhost:2524访问不了需要改什么配置吗 解决思路:           这 ...

  9. Java编程思想总结笔记The first chapter

    总觉得书中太啰嗦,看完总结后方便日后回忆,本想偷懒网上找别人的总结,无奈找不到好的,只好自食其力,尽量总结得最好. 第一章  对象导论 看到对象导论觉得这本书 目录: 1.1 抽象过程1.2 每个对象 ...

  10. zlib报“LNK2001:无法解析的外部符号”错误

    这个错误一般是由使用导出dll时未加载对应的lib文件导致的,但是工程在正确配置了lib文件的情况下仍然报这个错误,经查,是由于dll导入工程和dll导出工程的函数调用约定不一致导致的. 一.函数调用 ...