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(n100000) villages which are numbered from 1 to n. Since abandoned for a long time, the roads need to be re-built. There are m(m1000000) roads to be re-built, the length of each road is wi(wi1000000). 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(T10) 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 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
 
题目大意:有n(n<100000)个地点面m(m<1000000)条路,修每条路都有花费w,求最小花费使每个地点能够互相到达并求出人一两点的花费期望。


解题思路:先采用最小生成树的方法进行建树,可以输出第一个答案,最短路。接着,求期望,任意两个点都要走一次,那么我们就可以统计每条边走过的次数。

下面介绍走过次数的方法:

走过次数=左子树*右子树的点,用搜索的方法来统计左右子树的个数。

那么分子的值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
原创粉丝点击