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. Qlik报表开发见解

    因为项目需要,最近去做了Qlik Sense报表开发,学习了Qlik报表的开发方法和一些基础的开发模式,以下是我对Qlik报表开发的一些见解,个人水平有限,欢迎大神指导. 1.Qlik Sense的函 ...

  2. asp.net core 中灵活的配置方式

    asp.net core支持外部文件和命令行参数方式来配置系统运行所需要的配置信息,我们从下面两个常用场景来具体说下具体使用方法. 一.监听地址及端口配置 1,命令行方式 asp.net core系统 ...

  3. laravel如何向视图传递变量的方法

    第一种: public function about(){ $name = "cai" return view("sites.about")->with( ...

  4. SQLyog简介

    一.软件简介 SQLyog 是一个快速而简洁的图形化管理MYSQL数据库的工具,它能够在任何地点有效地管理你的数据库.SQLyog是业界著名的Webyog公司出品的一款简洁高效.功能强大的图形化MyS ...

  5. npm 的用法

    当用npm 安装依赖时如果加上  --save  就会自动把依赖模块添加到package.json中  别人下载时直接npm install 加载后就可以了

  6. vsftpd配置虚拟用户

    #安装vsftpd yum -y install vsftpd #创建本地ftp账户 groupadd ftpuser useradd -g ftpuser -s /sbin/nologin ftpu ...

  7. VB6之写注册表

    难免会遇到写注册表的情况,写了个实用点的RegWrite函数.为了减少代码量,用WScript.Shell取代了API来实现. 使用方式就在注释中了,就不再过多解释了.PS:注释比实现代码要丰富多了, ...

  8. database.properties数据源

    jdbc.driver_class=oracle.jdbc.driver.OracleDriverjdbc.connection.url=jdbc:oracle:thin:@localhost:152 ...

  9. 函数的上下文就是函数里面的this是谁

    规律1:函数用圆括号调用,函数的上下文是window对象 比如小题目: function fun(){ var a = 888; alert(this.a); //实际上访问的是window.a } ...

  10. 快学Scala之继承

        ## 1. 继承 Scala语言通过 extends 关键字来继承类. 那么继承一个类有什么好处呢? 子类除了拥有继承自超类的方法和字段(即为val(常量), var(变量)所定义的), 还可 ...