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. 移动端响应式布局+rem+calc()

    1.媒体查询:@media only screen and (max-width: ) {},在最初做pc端时,使用各种媒体查询,因为pc的屏幕分辨率总共就几种,不嫌麻烦的重复使用类名.有很大的缺陷就 ...

  2. SpringMVC中使用Swagger2整合

    Swagger2是什么 Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件. Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 W ...

  3. php5.6在yum下安装gd库

    yum install php-gd --enablerepo=remi,remi-php56 php.ini配置文件中增加 extension=gd.so 重启web服务器即可

  4. iis部署wcf服务过程

    一.在iis网站中添加wcf服务,一直添加到web.config目录即可 二.点击基本设置-->连接为-->特定用户.填写登入电脑的用户名和密码. 三.点击身份验证 四.控制面板,设置防火 ...

  5. 在vs2010中显示代码的行数

    1.打开VS2010,然后"工具" → "选项" 2.在选项页面,点击"文本编辑器"→"所有语言",在显示里将[行号]选 ...

  6. Dubbo高级特性实践-泛化调用

    引言 当后端Java服务用Dubbo协议作为RPC方案的基础,但部分消费方是前端Restful的PHP服务,不能直接调用,于是在中间架设了Router服务提供统一的基于HTTP的后端调用入口. 而Ro ...

  7. ASP.NET MVC Autofac依赖注入的一点小心得(包含特性注入)

    前言 IOC的重要性 大家都清楚..便利也都知道..新的ASP.NET Core也大量使用了这种手法.. 一直憋着没写ASP.NET Core的文章..还是怕误导大家.. 今天这篇也不是讲Core的 ...

  8. Web自动化之Headless Chrome编码实战

    API 概览 && 编码Tips 文档地址 github Chrome DevTools Protocol 协议本身的仓库 有问题可以在这里提issue github debugger ...

  9. 教你如何取消GCD任务

    GCD 是一种非常方便的使用多线程的方式.通过使用 GCD,我们可以在确保尽量简单的语法的前提下进行灵活的多线程编程.在 "复杂必死" 的多线程编程中,保持简单就是避免错误的金科玉 ...

  10. rownumber和rowid伪劣用法

    select rownum ,deptno,dname loc from  dept; select deptno,dname,loc from dept where rownum=1; select ...