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. sharepoint rest api 创建文档库 文件夹

    function createFolder() { var requestHeaders = { "Accept": "application/json;odata=ve ...

  2. R自带数据包

    datasets(R自带数据包) 作者:王彦博 作品来源:百度百科 precip #长度为70的命名向量 euro #欧元汇率,长度为11,每个元素都有命名 landmasses #48个陆地的面积, ...

  3. 选课 树形DP+多叉树转二叉树+dfs求解答案

    问题 A: 选课 时间限制: 1 Sec  内存限制: 128 MB 题目描述 大 学里实行学分.每门课程都有一定的学分,学生只要选修了这门课并考核通过就能获得相应的学分.学生最后的学分是他选修的各门 ...

  4. Qt WebEngine版本要求

    WebEngine是Qt5.4之后加入的新特性,用Qt WebEngine取代之前的Qt Webkit http://wiki.qt.io/QtWebEngine windows版本 windows版 ...

  5. 几种 vue的数据交互形式

    var that=this get请求 that.$http.get("1.txt").then(function(result){ console.log(result) thi ...

  6. 遍历Map和List的几种方法和性能比较

    public static void main(String[] args) { Map<String, String> map = new HashMap<String, Stri ...

  7. struts2框架下的一个简单的ajax例子

    举个例子 jsp页面: <%@ page language="java" import="java.util.*" pageEncoding=" ...

  8. 最近快速的过了一遍php基础语法

    把in_array() 写成is_array() ;结果自己坑了自己一万:打脸一万下,先记账上

  9. HDOJ 1260 DP

    Tickets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  10. 包装类、数组、string类浅析及练习

    String s1 = "abc"; String s2 = "abc"; System.out.println(s1==s2); //返回true Strin ...