2016 Multi-University Training Contest 1

Abandoned country

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


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


解析:

求图的最小生成树,在最小生成树的基础上,求最小期望长度,实际上就是求每一条边的贡献度。

注意强制转换的过程。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 1000000+10;
struct node {
    int u, v;
    LL val;
    node (){}
    node (int _u, int _v, int _val){
	u = _u;
   	v = _v;
	val = _val;
    }
    bool operator < (const node &a) const 
    {
        return val<a.val;
    }
};
LL val[maxn];
node e[maxn];
int su[100000+10];
int fa[100000+10];
vector<node>p[100000+10];
void init() {
    for (int i=0; i<100000+10; i++)
        fa[i] = i, p[i].clear();
    memset(su, 0, sizeof(su));
    memset(val, 0, sizeof(val));
}

void DFS(int u, int pre) {
    su[u] = 1;
    for (int i=0; i<p[u].size(); i++) {
        int v = p[u][i].v;
        if (v == pre)
            continue;
        val[v] = p[u][i].val;
        DFS(v, u);
        su[u] += su[v];
    }
}

int find(int x) {
    return x==fa[x]?x:fa[x] = find(fa[x]);
}

int main() {
    int T;
    scanf("%d",&T);
    while (T--) {
        int n, m;
        init();
        scanf("%d%d", &n, &m);
        for (int i=0; i<m; i++)
            scanf("%d%d%I64d", &e[i].u, &e[i].v, &e[i].val);
        sort(e, e+m);
        LL sum=0;
        for (int i=0; i<m; i++) {
            int v = find(e[i].v);
            int u = find(e[i].u);
            if (u != v) {
                fa[u] = v;
                sum += e[i].val;
                p[e[i].u].push_back((node){e[i].u, e[i].v, e[i].val});
                p[e[i].v].push_back((node){e[i].v, e[i].u, e[i].val});
            }
        }
        DFS(1, -1);
        LL ans = 0;
        for (int i=2; i<=n; i++)
            ans += (LL)(n-su[i])*(LL)su[i]*val[i];
        LL t = (LL)n*(n-1)/2;
        printf("%I64d %.2lf\n",sum, (double)(ans)/(double)(t));
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值