hdu 5723 Abandoned country

Abandoned country

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2586    Accepted Submission(s): 643


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
  
  
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
 

Author
HIT
 

Source
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:   5733  5732  5731  5730  5729 
 
思路:

     给定一张图,求最小生成树,并求在图中任取两点,两点间路径代价的期望值。

     也就是求最小生成树中:边权*左子树的大小*右子树的大小的和再除以n*(n-1);

     先求最小生成树,并在寻找树的过程中,保留最小生成树上的边,用于后续计算期望。采用dfs的方式,任意从树上一点出发,计算该节点所在的子树上的节点数x,并由总数减去x得到边另一侧的节点数。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef __int64 ll;
struct node
{
    ll st,en,len;
}E[1000005];
struct Edge
{
    ll en,next,len;
}edge[1000005];
ll num,head[100005],fa[100005],n,m,expect;
void init()
{
    num=0;expect=0;
    memset(head,-1,sizeof(head));
    memset(E,0,sizeof(E));
    memset(edge,0,sizeof(edge));

}
void add(ll st,ll en,ll len)
{
    edge[num].en=en;
    edge[num].len=len;
    edge[num].next=head[st];
    head[st]=num++;
}
bool cmp(node &a,node &b)
{
    return a.len<b.len;
}
ll find(ll x)
{
    if(x==fa[x]) return x;
    return fa[x]=find(fa[x]);
}
ll kruskal()
{
    ll cnt=0,ans=0;
      for(ll i=0;i<=n;i++)
        fa[i]=i;
    for(ll i=0;i<m;i++)
    {
        ll fx=find(E[i].st);
        ll fy=find(E[i].en);
        if(fx!=fy)
        {
            ans+=E[i].len;
            fa[fx]=fy;
            cnt++;
            add(E[i].st,E[i].en,E[i].len);
            add(E[i].en,E[i].st,E[i].len);
            if(cnt==n-1) break;
        }
    }
    return ans;
}
ll dfs(ll x,ll fa)
{
    ll tmp,res=1;
    for(ll i=head[x];i!=-1;i=edge[i].next)
    {
        if(edge[i].en!=fa)
        {
            tmp=dfs(edge[i].en,x);
            expect+=1.0*(n-tmp)*tmp*edge[i].len;
            res+=tmp;
        }
    }
    return res;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        init();
        scanf("%I64d%I64d",&n,&m);
        for(ll i=0;i<m;i++)
        {
            scanf("%I64d%I64d%I64d",&E[i].st,&E[i].en,&E[i].len);
        }
        sort(E,E+m,cmp);
        ll ans=kruskal();
        dfs(1,-1);
        double ans1=2.0*expect/((n-1)*n);
        printf("%I64d %.2lf\n",ans,ans1);
    }
    return 0;
}


    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值