题目链接:

Abandoned country

Time Limit: 8000/4000 MS (Java/Others)   

 Memory Limit: 65536/65536 K (Java/Others)

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
题意:
在一个无向图上找一棵最小生成树,再求一下任意两点距离的期望;
思路:
找最小生成树,找期望的时候看每条边对答案的贡献;
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <bits/stdc++.h>
using namespace std;#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));typedef  long long LL;template<class T> void read(T&num) {char CH; bool F=false;for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {if(!p) { puts("0"); return; }while(p) stk[++ tp] = p%10, p/=10;while(tp) putchar(stk[tp--] + '0');putchar('\n');
}const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e6+10;
const int maxn=1e5+10;
const double eps=1e-5;int n,m,head[maxn],p[maxn],num[maxn],cnt;int findset(int x){if(x==p[x])return x;return p[x]=findset(p[x]);}
struct Edge
{int from,to,next,val;
}edge[N];int cmp(Edge a,Edge b)
{return a.val<b.val;
}
struct node
{int to,val;
};
vector<pair<int,int> >ve[maxn];
void add_edge(int s,int e,int va)
{edge[cnt].from=s;edge[cnt].to=e;edge[cnt].next=head[s];edge[cnt].val=va;head[s]=cnt++;
}LL dis=0;int dfs(int cur,int fa,int val){int len=ve[cur].size();num[cur]=1;For(i,0,len-1){int x=ve[cur][i].first,va=ve[cur][i].second;if(x==fa)continue;num[cur]+=dfs(x,cur,va);}dis=dis+(LL)num[cur]*(n-num[cur])*val;return num[cur];}
int main()
{int t;read(t);while(t--){cnt=0;dis=0;read(n);read(m);For(i,0,n){head[i]=-1;p[i]=i;ve[i].clear();}For(i,1,m){int u,v,w;read(u);read(v);read(w);add_edge(u,v,w);}sort(edge,edge+m,cmp);LL sum=0;For(i,0,m-1){int fa=findset(edge[i].from),fb=findset(edge[i].to);if(fa!=fb){p[fa]=fb;sum=sum+edge[i].val;ve[edge[i].from].push_back({edge[i].to,edge[i].val});ve[edge[i].to].push_back({edge[i].from,edge[i].val});}}dfs(1,0,0);printf("%lld %.2lf\n",sum,dis*1.0/(0.5*(n-1)*n));}return 0;
}

  

转载于:https://www.cnblogs.com/zhangchengc919/p/5687291.html

hdu-5723 Abandoned country(最小生成树+期望)相关推荐

  1. Abandoned country

    A Abandoned country 最小生成树 + 树上任意点对距离之和 树上任意点对距离之和: 计算时考虑每一个边,它的贡献值为它的 左端点以左的点的个数 * 右端点以右的点的个数 * 该边的权 ...

  2. HDU-5723 Abandoned country

    Problem Description An abandoned country has n(n≤100000) villages which are numbered from 1 to n. Si ...

  3. hdu 1301 Jungle Roads 最小生成树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 The Head Elder of the tropical island of Lagrish ...

  4. hdu 5483 Nux Walpurgis(最小生成树+dfs)

    题目链接:hdu 5483 Nux Walpurgis 解题思路 先求一下最小生成树.然后枚举起点,遍历整棵树,维护树边能被替换的最小权值. 代码 #pragma comment(linker, &q ...

  5. HDU 3058 Generator [AC自动机+期望DP]

    给出M个短串,这些短串由前N个大写字母组成.然后随机的按字符生成字符串,每次生成1~N个字符的概率是相等的,当生成串包含任意一个指定短串时,就停止生成.问生成串的长度的期望是多少. 首先建立Trie图 ...

  6. HDU 1863 畅通工程 最小生成树

    思路: 比较典型的最小生成树的题目了..在这里用求最小生成树的经典算法K(Kruskal)算法和P(Prim)算法.我的 K 算法用的是结构体来存图,P 算法用的是邻接矩阵来存图,K算法的复杂度是O( ...

  7. hdu 1232 畅通工程 最小生成树 并查集

    1232的连接:http://acm.hdu.edu.cn/showproblem.php?pid=1232 #include <iostream>#include <cstdio& ...

  8. 2019杭电多校第七场 HDU - 6656 Kejin Player——概率期望

    题意 总共有 $n$ 层楼,在第 $i$ 层花费 $a_i$ 的代价,有 $pi$ 的概率到 $i+1$ 层,否则到 $x_i$($x_i \leq 1$) 层.接下来有 $q$ 次询问,每次询问 $ ...

  9. hdu 5886 Tower Defence 树形期望dp 雪漫防守战

    题意: 风暴斗篷现在要攻打雪漫城.雪漫城有n(n≤100000)个哨塔防守,哨塔之间有n-1条路相连,(构成一棵树).现在预测到风暴斗篷要进攻某一条路,然后这棵树就一分为二,现在要得到分开后的最长路. ...

最新文章

  1. js 输出二维数组的最大值
  2. Fedora安装Texlive2013时出现Can't locate Digest/MD5.pm的解决方法
  3. 关于mybatis返回前端日期格式化问题
  4. SAP JCO connector 例子
  5. HDFS的访问方式之HDFS shell的常用命令
  6. oracle 更新记录语句,Oracle语句自动判断是要更新记录还是要插入记录
  7. ITK:从体积生成切片
  8. C#中使用资源文件保存图片和皮肤文件的使用
  9. 《SAP入门经典(第4版•修订版)》——2.5 4种视角相互结合
  10. String.Format数字格式化输出 {0:N2} {0:D2} {0:C2}
  11. linux列举网卡,linux下快速列出局域网中所有主机名(计算机名)的脚本
  12. 教师教学质量评价系统c语言,教师课堂教学评价大全_浅谈C语言课堂教学方法
  13. C# 读取txt文本数据
  14. 如何使用分区工具实现无损分区大小调整?
  15. Lucene之Field常用类型
  16. 经济危机离你并不遥远!
  17. tenacity -- Python中一个专门用来retry的库
  18. PayPal贝宝集成
  19. Selenium(一)12.Actions的常用方法
  20. 单片机教学打铃控制器C语言

热门文章

  1. 不小心点了计算机一键还原怎么操作,电脑一键还原在哪里?电脑怎样一键还原系统...
  2. 移动硬盘加上密码_树莓派+Seafile+移动硬盘搭建私有云
  3. R语言多元统计包简介:各种假设检验 统计方法 聚类分析 数据处理
  4. R中因子分析的得分计算
  5. 计算机组装的虚拟仿真实验报告,组装计算机的虚拟实验室
  6. python连接数据库必须要提供用户名和密码_5.9---python连接数据库实现登录注册
  7. 7z001怎么解压在安卓手机上面_安卓手机怎么设置网易企业邮箱
  8. 人脸对齐(十九)--Regressing a 3D Face Shape from a Single Image
  9. 循环链表(线性表的链式存储)---C语言版
  10. python的copy模块是哪个模块_每周一个 Python 模块 | copy