HDU 5723 Abandoned country 最小生成树,树上期望

Abandoned country

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


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


题意:给一个图,计算这个图构成的最小生成树边权总和。以及在这颗最小生成树上任意选两个点之间距离的期望值。

最小生成树这个直接上模版就行了。

通过模拟观察发现:期望 = 每条边的边权*每条边被经过的次数/C(n,2);

这个期望,暴力每个点dfs计算次数复杂度n方,肯定是不行的。我们知道每条边被走的次数是固定的。后面再瞎画几个图手动模拟下发现每条边被经过次数=这条边两头结点的数量的乘积。这个数量可以任意跑一个节点记录这个点儿子的数量,然后n-son[u]就是另一头结点的数量。

这下思路就明显了

1.由原图跑一遍最小生成树记录一个ans

2.由最小生成树边用邻接表建一个新图。(我用vector<pair<int,LL>>被超时,换用结构体邻接表才过了)

3.任意找个点计算每个点儿子的数量

3.计算每条边左右儿子乘积/C(n,2)。


CODE

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL N = 1e5+10;
struct node{    ///跑最小生成树用的结构体
    LL st;
    LL en;
    LL w;
    LL f;
}E[N*10];

LL n,m;
LL anslen;      ///最小生成树边的长度
double ansq;    ///期望
LL top;         ///邻接表建图用
LL son[N];      ///每个点儿子结点的数量
double t;       ///计算期望时用,t = C(n,2);
LL fa[N];       ///并查集父亲编号
LL head[N];     ///邻接表建图头结点
bool vis[N];    ///第一次dfs用
bool vis1[N];   ///第二次dfs用

struct NODE{    ///有了最小生成树需要重新建图,邻接表
    LL en;
    LL w;
    LL next;
}G[N*10];

void Init(){          ///初始化
    anslen = 0;
    ansq = 0;
    top = 0;
    for(LL i = 0;i <= n;i++){
        fa[i] = i;
        vis1[i] = false;
        head[i] = -1;
        son[i] = 0;
        vis[i] = false;
    }
}

bool cmp(node a,node b){
    return a.w < b.w;
}

void add(LL u,LL v,LL w,LL i){  ///最小生成树建边
    E[i].st = u;
    E[i].en = v;
    E[i].w = w;
    E[i].f = 0;
}

void add1(LL u,LL v,LL w){      ///新图建边
    G[top].en = v;
    G[top].w = w;
    G[top].next = head[u];
    head[u] = top++;
}

LL Find(LL x)
{
    if(fa[x] == x) return fa[x];
    else return fa[x] = Find(fa[x]);
}

void kruskal()    ///最小生成树
{
    for(LL i = 1;i <= m;i++){
        LL fx = Find(E[i].st);
        LL fy = Find(E[i].en);
        if(fx != fy){
            fa[fx] = fy;
            E[i].f = 1;
            anslen += E[i].w;
        }
    }
}

void dfs1(LL u){         ///第一次dfs跑出每个点儿子的数量
    vis[u] = true;
    son[u] = 1;
    for(LL i = head[u];i != -1;i = G[i].next){
        LL v = G[i].en;
        if(vis[v]) continue;
        dfs1(v);
        son[u] += son[v];
    }
}

void dfs2(LL u){         ///第二次计算期望
    vis1[u] = true;
    for(LL i = head[u];i != -1;i = G[i].next){
        LL v = G[i].en;
        if(vis1[v]) continue;
        ansq += (double)son[v]*(double)(n-son[v])*(double)G[i].w/t;
        dfs2(v);
    }
}

int main(void){
    LL T;
    scanf("%I64d",&T);
    while(T--){
        scanf("%I64d%I64d",&n,&m);
        Init();
        for(LL i = 1;i <= m;i++){
            LL u,v;
            LL w;
            scanf("%I64d %I64d %I64d",&u,&v,&w);
            add(u,v,w,i);
        }
        sort(E+1,E+1+m,cmp);       ///排序后kruskal
        kruskal();
        for(LL i = 1;i <= m;i++){  ///构建新图
            if(E[i].f){
                LL st = E[i].st;
                LL en = E[i].en;
                add1(st,en,E[i].w);
                add1(en,st,E[i].w);
            }
        }
        dfs1(1);
        t = double(n*(n-1)/2.0);
        dfs2(1);
        printf("%I64d %.2f\n",anslen,ansq);
    }
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值