多校1.Abandoned country1001

5 篇文章 0 订阅
1 篇文章 0 订阅

点击打开链接

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
 



题意: 给边求最小生成树,并且求任意两点的距离平均值(和/Cn2)

思路:因为点数太多只能用kruskal先求出最小生成树(同时保存好每个点与之相连的点和边权)。再用dfs递归出每条边左边右边的结点数(边的贡献度nl*nr=该边被经过的次数)还用到了dp,dp公式是以点为中心,dp[i] = 与该点相连的边(除了与父亲节点相连的边)的贡献度和*边权。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>

using namespace std;

const int MAX_V = 100000 + 10;

int n,m;
int father[MAX_V],sum[MAX_V];
double dp[MAX_V];

struct node
{
    int fr, to, va;
    node(){};
    node(int x, int y, int z):fr(x),to(y),va(z){}
};

struct Node
{
    int to, va;
    Node(){};
    Node(int x, int y) : to(x),va(y){}
};

vector<Node> vet[MAX_V];
vector<node> edge;

bool comp(node a, node b)
{
    return a.va < b.va;
}

int ffather(int x)
{
    if(father[x] == x)
        return x;
    else
        return father[x] = ffather(father[x]);
}

long long Kruskal()
{
    sort(edge.begin(), edge.end(), comp);
    for(int i = 1; i <= n; i++)
        father[i] = i;
    long long add = 0;
    for(int i = 0; i < m; i++)
    {
        int fr = edge[i].fr, to = edge[i].to, va = edge[i].va;
        fr = ffather(fr);
        to = ffather(to);
        if(fr == to)
            continue;
        father[fr] = to;
        add += (long long)va;
        vet[edge[i].fr].push_back(Node(edge[i].to, va));
        vet[edge[i].to].push_back(Node(edge[i].fr, va));
    }
    return add;
}

void dfs(int root, int father)
{
    sum[root] = 1;                                                   //一边的点
    for(int i = 0; i < (int)vet[root].size(); i++)
    {
        
        Node temp = vet[root][i];
        if(temp.to == father)                                        //排除掉父亲结点
            continue;
        dfs(temp.to,root);
        sum[root] += sum[temp.to];
        dp[root] += dp[temp.to] + ((double)sum[temp.to]*(n-sum[temp.to])) * temp.va;
    }

}





int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d", &n, &m);
        edge.clear();                                            //初始化..
        for (int i = 0, a, b, c; i < m; i++)
        {
            scanf("%d%d%d", &a, &b, &c);
            edge.push_back(node(a, b, c));
        }
        memset(dp,0,sizeof(dp));
        for(int i = 1;i <= n;i++)vet[i].clear();                   // 不能忘阿...
        printf("%I64d ",Kruskal());
        dfs(1,0);
        long long s = (long long)n * (n - 1) / 2;
        printf("%.2f\n",dp[1]/(double)s);
    }
    return 0;
}







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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值