2016 Multi-University Training Contest 1 1001 Abandoned country
来源:互联网 发布:淘宝价格怎么设置打折 编辑:程序博客网 时间:2024/06/07 03:57
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5723
题目:
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 integersn,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 .
For each test case, the first line contains two integers
Output
output the minimum cost and minimum Expectations with two decimal places. They separated by a space.
Sample Input
14 61 2 12 3 23 4 34 1 41 3 52 4 6
Sample Output
6 3.33
首先注意到任意两条边的边权是不一样的,由此得知最小生成树是唯一的,最小生成树既然 是唯一的,那么期望其实也就是唯一的,不存在什么最小期望。求完最小生成树之后,接下 来的问题就可以转换成在最小生成树上求任意两点之间距离的平均值,对于每条边,统计所 有的路径用到此边的次数,也就是边的两端的点数之积。那么这条边的总贡献就是次数*边 权。最后得到所有边的贡献之和再除以总路径数n∗(n−1)/2n*(n-1)/2n∗(n−1)/2就是答案。可以OnOnOn求出。任取一点为根dfs,对每个点iii记录其子树包含的点数(包括其自身),设点数为sum[i]sum[i]sum[i],则iii的父亲一侧的点数即为n−sum[i]n-sum[i]n−sum[i]。一边遍历一边统计就行。
并查集写错坑了好久。。手残剁手。。。
#include <iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#include<vector>#define N 100010#define M 1000010#define ll long longusing namespace std;struct node{ int u,v; ll w;}e[M];int n,m,pre[N],sum[N],c[N];vector<int> r[N];bool cmp(node a,node b ){ return a.w<b.w;}int findset(int v){ return v==pre[v]?v:pre[v]=findset(pre[v]);}void dfs(int t,int fa){ sum[t]=1; for(int i=0;i<r[t].size();i++) { int u=r[t][i]; if(u!=fa) { dfs(u,t); sum[t]+=sum[u]; } }}void kruscal(){ sort(e,e+m,cmp); for(int i=1;i<=n;i++) pre[i]=i,r[i].clear(); int t=0; ll ans=0; for(int i=0;i<m;i++) { int u=e[i].u,v=e[i].v; int t1=findset(u); int t2=findset(v); if(t1!=t2) { ans+=e[i].w; pre[t1]=t2; c[t++]=i; r[u].push_back(v); r[v].push_back(u); } if(t==n-1) break; } printf("%lld ",ans); ans=0; dfs(1,-1); for(int i=0;i<t;i++) { int tp=min(sum[e[c[i]].u],sum[e[c[i]].v]); ans+=(ll)tp*e[c[i]].w*(n-tp); } printf("%.2f\n",(double)ans/n/(n-1)*2);}int main(){ int T; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); for(int i=0;i<m;i++) { scanf("%d%d%lld",&e[i].u,&e[i].v,&e[i].w); } kruscal(); }}
0 0
- 2016 Multi-University Training Contest 1 1001 Abandoned country
- 2016 Multi-University Training Contest 1Abandoned country
- 2016 Multi-University Training Contest 1 1001.Abandoned country
- 2016 Multi-University Training Contest 1 Abandoned country
- 【Hdu 5723】Abandoned country【2016 Multi-University Training Contest 1】
- 2016 Multi-University Training Contest 1 1001 Abandoned country (最小生成树+DFS)
- 2016暑假多校合练第一场Multi-University Training Contest 1 1001 Abandoned country
- HDU 5723 Abandoned country(2016 Multi-University Training Contest 1的1001题)
- 2016 Multi-University Training Contest 1-1001---HDU 5723 Abandoned country(DFS+最小生成树)
- 2016 Multi-University Training Contest 1 1001 Abandoned country(最小生成树)
- 2016 Multi-University Training Contest 1 hdu 5723 Abandoned country【Kruskal+Dfs】好题
- hdu 5723 Abandoned country(2016 Multi-University Training Contest 1——最小生成树+深搜)
- (HDU 5723)2016 Multi-University Training Contest 1 Abandoned country(图论)
- 【2016 Multi-University Training Contest 1】【hdu5723】Abandoned Countries
- 最小生成树+DFS求任意两点间平均路径长度 ——Abandoned country (HDU 5732)( 2016Multi-University Training Contest 1 1001 )
- 【HDU5723 2016 Multi-University Training Contest 1A】【最小生成树 + 简单算贡献】 Abandoned country 最小生成树的链长之和
- 2016 Multi-University Training Contest 1
- 2016 Multi-University Training Contest 1
- cublas中执行矩阵乘法运算的函数 首先要注意的是cublas使用的是以列为主的存储方式,和c/c++中的以行为主的方式是不一样的。处理方法可参考下面的注释代码
- uva 10943 How do you add?
- Android HTML 中添加空格
- JavaSE基础(六)
- caffe与Python接口的配置(VC2013 Windows CUDA7.5 Python2.7.12)
- 2016 Multi-University Training Contest 1 1001 Abandoned country
- HDU1065 I Think I Need a Houseboat
- PHP常用的设计模式
- Java多线程面试问题
- 浅谈android中手机联系人字母索引表的实现
- HDU 5735 Born Slippy ( from:2016 Multi-University Training Contest 2 )
- 2015蓝桥杯C组试题 打印大X
- Linux下opencv编译两种方式
- 13道简单算法题