hdu 5723 Abandoned country(2016 Multi-University Training Contest 1——最小生成树+深搜)

本文探讨了如何利用最小生成树算法解决特定图论问题,即寻找连接所有节点的最低成本路径,并计算任意两点间行走的期望长度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接: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
  
  
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
 
题目大意:有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 long

vector<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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值