最小生成树+树的平均距离-HDU-5723-Abandoned country

Abandoned country

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

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 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
2016 Multi-University Training Contest 1


题意:
有n个点,由m条无向边连通,同时,这m条边各自有一个互不相同的长度wi。现在想知道这个图的MST以及在MST的基础上,任意两点间距的期望值。
0 < n ≤ 100000
0 < m ≤ 1000000
0 < wi ≤ 1000000


题解:
MST很好求,同时因为每条边从长度都不一样,那么MST一定是唯一的。用一个Kruskal求出MST后,找到一个根,并由此DFS,计算每条边要使用的次数,乘以边权加到result里,最后将result除以总搭配数。
对于每条边的使用次数,应该是此边上方点数乘以下方点数乘以2,那么可以只算上下两边点数乘积,最后乘2。具体实施方法,对于一个边,下方点数应该是儿子结点的下方点数加上儿子节点数,递归DFS即可。


//
//  main.cpp
//  HDU-5723-Abandoned country
//
//  Created by 袁子涵 on 16/7/20.
//  Copyright © 2016年 袁子涵. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <queue>
#include <set>
#include <algorithm>
#include <stack>

using namespace std;
const int MAXN=100005;
const int MAXM=1000005;
int t;
long long int n,m,father[MAXN],a,b,fa,fb,root,mst;
double result;
bool vis[MAXN];
typedef struct edge1
{
    long long int to,w;
    edge1(long long int _to,long long int _w):to(_to),w(_w){}
}edge1;
typedef struct edge
{
    long long int u,v,w;
    bool operator <(const edge &r)const
    {
        return w<r.w;
    }
}edge;
long long int find(long long int num)
{
    long long int now=num,root;
    while (father[now]!=now)
        now=father[now];
    root=now;
    now=num;
    while (father[now]!=now) {
        now=father[now];
        father[now]=root;
    }
    return root;
}
vector<edge1>Node[MAXN];
edge Edge[MAXM];
long long int dfs(long long int num,long long int wt)
{
    long long int son=0;
    vis[num]=1;
    for (long long int i=0; i<Node[num].size(); i++)
        if (vis[Node[num][i].to]==0)
            son+=dfs(Node[num][i].to, Node[num][i].w);
    son++;
    result+=(n-son)*son*wt;
    return son;
}
int main(int argc, const char * argv[]) {
    scanf("%d",&t);
    while (t--) {
        mst=result=0;
        scanf("%lld%lld",&n,&m);
        for (long long int i=1; i<=n; i++)
        {
            vis[i]=0;
            Node[i].clear();
            father[i]=i;
        }
        for (long long int i=0; i<m; i++)
            scanf("%lld%lld%lld",&Edge[i].u,&Edge[i].v,&Edge[i].w);
        sort(Edge+0,Edge+m);
        for (long long int i=0; i<m; i++) {
            a=Edge[i].u,b=Edge[i].v;
            fa=find(a),fb=find(b);
            if(fa!=fb)
            {
                Node[a].push_back(edge1(b,Edge[i].w));
                Node[b].push_back(edge1(a,Edge[i].w));
                mst+=Edge[i].w;
                father[fa]=fb;
            }
        }
        for (long long int i=1; i<=n; i++) {
            if (father[i]==i) {
                root=i;
                break;
            }
        }
        dfs(root,0);
        result/=(double)(n*n-n);
        result*=2;
        printf("%lld %.2lf\n",mst,result);
    }
    return 0;
}
1、资源项目源码均已通过严格测试验证,保证能够正常运行;、 2项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行;、 2项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值