hdu 5723 Abandoned country(2016 Multi-University Training Contest 1——最小生成树+深搜)
来源:互联网 发布:淘宝地垫 编辑:程序博客网 时间:2024/05/28 15:25
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5723
Abandoned country
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2737 Accepted Submission(s): 673
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
Author
HIT
Source
2016 Multi-University Training Contest 1
解题思路:先采用最小生成树的方法进行建树,可以输出第一个答案,最短路。接着,求期望,任意两个点都要走一次,那么我们就可以统计每条边走过的次数。
下面介绍走过次数的方法:
走过次数=左子树*右子树的点,用搜索的方法来统计左右子树的个数。
那么分子的值s+=次数*权值,分母的值就是Cn2。
详见代码。
/*给了一个图,求最小生成树,然后在n个点任意选两个点。问在这Cn2中选择中,每种选择的两个点的距离的和和Cn2的比值就是期望值!实际上对于每条边就是边权*左子树的大小*右子树的大小的和!*/#include<iostream>#include<algorithm>#include<cstring>#include<vector>#include<cstdio>using namespace std;#define N 1000005#define ll long longvector<pair<int,int> >V[N];//存放线段树生成之后的路struct node{ int x,y,w;} p[N];int n,m,fa[N];double ans;bool cmp(node a, node b){ return a.w<b.w;}int Find(int x){ return x != fa[x] ? fa[x] = Find( fa[x] ) : x;}bool Union(int x,int y){ x=Find(x); y=Find(y); if(x!=y) { fa[x]=y; return 1; } return 0;}int Dfs(int now,int pre){ int tm=0; for(int i=0; i<V[now].size(); i++) { if(V[now][i].first==pre) continue; int cm=Dfs(V[now][i].first,now); tm+=cm; ans+=1.0*cm*(n-cm)*V[now][i].second; } return tm+1;}int main(){ int t; ll s=0; scanf("%d",&t); while(t--) { s=ans=0; scanf("%d%d",&n,&m); for(int i=0; i<=n; i++) { fa[i]=i; V[i].clear(); } for(int i=0; i<m; i++) scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].w); sort(p,p+m,cmp); for(int i=0; i<m; i++) { if(Union(p[i].x,p[i].y)==1) { s+=p[i].w; V[p[i].x].push_back(make_pair(p[i].y,p[i].w)); V[p[i].y].push_back(make_pair(p[i].x,p[i].w)); } } Dfs(1,0); ans=ans*2.0/(n)/(n-1); printf("%lld %.2lf\n",s,ans); } return 0;}
0 0
- hdu 5723 Abandoned country(2016 Multi-University Training Contest 1——最小生成树+深搜)
- 2016 Multi-University Training Contest 1-1001---HDU 5723 Abandoned country(DFS+最小生成树)
- 【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(最小生成树)
- 最小生成树+DFS求任意两点间平均路径长度 ——Abandoned country (HDU 5732)( 2016Multi-University Training Contest 1 1001 )
- 2016 Multi-University Training Contest 1 hdu 5723 Abandoned country【Kruskal+Dfs】好题
- HDU 5723 Abandoned country(2016 Multi-University Training Contest 1的1001题)
- (HDU 5723)2016 Multi-University Training Contest 1 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
- 2016 Multi-University Training Contest 1 1001 Abandoned country
- 【HDU5723 2016 Multi-University Training Contest 1A】【最小生成树 + 简单算贡献】 Abandoned country 最小生成树的链长之和
- 2016暑假多校合练第一场Multi-University Training Contest 1 1001 Abandoned country
- 【HDU】5723 Abandoned country(2016 Multi-University)
- 【2016 Multi-University Training Contest 1】【hdu5723】Abandoned Countries
- HDU 5723 Abandoned country 最小生成树
- Notification
- DOM—动态添加table
- poj2686 状压dp
- HDU 4273-G - Rescue-三维凸包求重心
- Android如何通过shareduserid获取系统权限
- hdu 5723 Abandoned country(2016 Multi-University Training Contest 1——最小生成树+深搜)
- HDD is Outdated Technology
- 从入门到精通:让人又爱又恨的注解(一)
- Andriod File 路径转换为Uri
- 嵌入式系统上C++调用C语言接口代码链接时找不到C函数问题处理
- 【CodeForces】610A - Pasha and Stick(水)
- UVA - 11044 Searching for Nessy
- HDU 1312 Red and Black 红与黑 搜索 dfs bfs
- Android_实现获取手机内apk并分享的功能