hdu 5723 Abandoned country 最小生成树 期望
2016-07-20 09:25
337 查看
Abandoned country
题目连接:
http://acm.hdu.edu.cn/showproblem.php?pid=5723Description
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
14 6
1 2 1
2 3 2
3 4 3
4 1 4
1 3 5
2 4 6
Sample Output
6 3.33Hint
题意
给你一个无向图,保证边权都不相同,让你求一棵最小生成树。然后说有一个人在这棵树上瞎选起点和终点,问这个人走路的期望是多少。
题解:
典型的两道傻逼题杂糅在一起的题……最小生成树跑kruskal
期望的话,考虑边(u,v,c),那么对答案贡献就是size[u] * (n-size[u]) * c * n(n-1)/2
代码
#include<bits/stdc++.h> using namespace std; const int maxn = 1000005; int n,m; int fa[maxn]; double ans2; vector<pair<int,int> >E[maxn]; int fi(int u){ return u != fa[u] ? fa[u] = fi( fa[u] ) : u; } void uni(int x,int y){ int p=fa[x],q=fa[y]; if(p==q)return; fa[p]=q; } struct node{ int x,y,z; }p[maxn]; bool cmp(node a,node b){ return a.z<b.z; } void init(){ ans2=0; for(int i=0;i<maxn;i++)fa[i]=i; memset(p,0,sizeof(p)); for(int i=0;i<maxn;i++)E[i].clear(); } int dfs(int x,int f){ int sz = 0; for(int i=0;i<E[x].size();i++){ int v = E[x][i].first; if(v==f)continue; int tmp = dfs(v,x); sz+=tmp; ans2 = ans2 + 1.0 * tmp * (n-tmp) * E[x][i].second; } return sz+1; } int main(){ int t; scanf("%d",&t); while(t--){ init(); scanf("%d%d",&n,&m); for(int i=1;i<=m;i++){ scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].z); } sort(p+1,p+1+m,cmp); long long ans = 0; for(int i=1;i<=m;i++){ int p1=fi(p[i].x),q1=fi(p[i].y); if(p1==q1)continue; uni(p1,q1); ans+=p[i].z; E[p[i].x].push_back(make_pair(p[i].y,p[i].z)); E[p[i].y].push_back(make_pair(p[i].x,p[i].z)); } dfs(1,0); ans2 = ans2 * 1.0 * 2.0 * 1.0 / (1.0* n) / (n - 1.0); printf("%I64d %.2lf\n",ans,ans2); } }
相关文章推荐
- 今天早上刚刚碰到的一个问题oracle数据归档已满,只能进行内部连接,ORA-00257 archiver error. 错误的处理方法
- Linux文件目录结构
- 常用的Linux命令
- [主席树区间覆盖 线段树] UNR #1 火车管理
- 如何理解BGP同步规则-
- 如何在beans.xml中添加多个dao以及mapper.xml
- HashTable、HashMap、HashSet
- vi编辑器的使用
- 用于HDMI接口的ESD保护技术
- hadoop的简单安装部署过程
- APUE 7-13 longjmp对各类对象的影响
- hdoj 2504 又见GCD
- CMap如何key为字符串,值为int
- Select2下拉选项库 部分积累
- Android 基于注解的框架
- MongoDB配置参数详解:
- uvaLive 2701 Find The Multiple
- [下界最小流] UNR #1 奇怪的线段树
- Select2下拉选项库 部分积累
- APACHE-CXF demo